programming 8051

Discussion in 'Programmer's Corner' started by mdsaad, May 17, 2006.

  1. mdsaad

    Thread Starter New Member

    Nov 2, 2005
    8
    0
    Hi there,

    I’ve got this PWM program. Can anyone help me to describe how its work?
    Thank you for the feedback :)



    #include <reg51.h>
    sbit output = P3^7;


    void timer0_isr(unsigned char a) interrupt 1;

    void timer1_isr(unsigned char B) interrupt 3;


    void timer0_isr(unsigned char a) interrupt 1 {

    output=1;
    TL0 = a;
    TR0=1;

    }


    void timer1_isr(unsigned char B) interrupt 3 {

    output =0;
    TL1 = b;
    TR1 = 1;

    }


    void ISR_initialization (void){
    IP = 0x00;
    // IE = 0x82;

    ET0 = 1;
    ET1 = 1;

    TMOD = 0x11;

    TH0 = 0xFF; //
    TH1 = 0xFF;

    TF0 =0;
    TF1 =0;

    }


    void main (){

    ISR_initialization ();

    }
     
  2. Papabravo

    Expert

    Feb 24, 2006
    9,901
    1,723
    You did not say if it works or not.

    I have never seen a case where parameters can be passed to an interrupt routine. I also think you are confused about the behavior of the Timer Run Bits and the Timer Flag Bits. The Timer Run bits are not affected by vectoring to an interrupt. The timer continues to run. It is the Timer Flag bits that need to be cleared in the interrupt routine.

    Also, what happens when main() returns after intializing the timers?

    I seriously doubt that it can work as written.
     
  3. mdsaad

    Thread Starter New Member

    Nov 2, 2005
    8
    0
    As you said, it doesnt work properly. I tried to make another program without using the timer and interrupt but the problem now,when the frequency is low (0 to 30hz) the motor doesn't work. Even if it works, it have a jitter on it. I don't know what happen. I'm using AT89C4051 microcontroller and rotary encoder as the input (EC11B15243BD 11m Size Metal Shaft Encoder ALPS). Supposedly, the output speed is according to the input speed.




    #include <reg51.h>

    sbit motor_output1 = P1^6;
    sbit motor_output2 = P1^3;
    sbit motor_output1Lin1 = P1^4;
    sbit motor_output1Lin2 = P1^2;

    sbit wheel_input = P3^3;


    void counter_interval(void){
    unsigned char i,
    i=0; i=0; i=0; i=0; i=0;
    i=0; i=0; i=0; i=0; i=0;
    }


    void main (void){

    signed int on_time, off_time, x, y, q,u;
    signed int w;
    unsigned int k;
    x=0;
    y=0;

    q=0;
    w=0;


    while(1){

    while(!wheel_input);
    if (wheel_input) {a: w++;

    counter_interval();
    counter_interval();
    counter_interval();
    counter_interval();
    counter_interval();
    counter_interval();
    counter_interval();
    counter_interval();
    counter_interval();
    counter_interval();


    if(!wheel_input) { x = w/100; y=500-x; w=0; goto b;}


    if (wheel_input) goto a;

    }

    b:

    on_time = x;
    off_time= y;
    q=0;

    for(k=0;k<32000;k++){
    q++;
    if (q == on_time) {motor_output1=0;motor_output1Lin1=0;}
    if (q == off_time) { motor_output1=1;motor_output1Lin1=1; q=0;}
    }

    if (wheel_input) while(wheel_input);
    motor_output1=1;motor_output1Lin1=1;


    }

    }




    I hope you'll understand what i mean. Actually, english is not my first language but i'm trying to make it as clear as possible. Thank you.
     
  4. Papabravo

    Expert

    Feb 24, 2006
    9,901
    1,723
    Without a schematic, some information about the shaft encoder and the motor I can't offer much assistance since I don't recognize those parts. Instead of focusing on the code let's go back to the beginning and talk about what you are trying to do.

    What kind of a motor are you using?
    What kind of drive circuit are you using?
    What are you trying to control? Position? Velocity?

    Who is writing the spagetti code with the unstructured gotos, and what is with the repeated calls to a routine that loads zero into a local variable repeatedly? This is quite possibly the worst code I've ever seen!
     
  5. mdsaad

    Thread Starter New Member

    Nov 2, 2005
    8
    0
    Hi,

    I'm sorry for the unstructured program. It would be really helpful if you can tell me what to do. I'm using a DC motor (918D series single ratio metal gearbox 918D100112) and IR 2102 High and Low Side Driver. Basically, the speed of the motor is controlled by the rotary encoder. Here I've attached the schematic of the circuit.

    -->The rotary encoder will detect the speed and give the signal to the microprocessor. Microprocessor will measure the actual speed and generate the PWM signal for both side of MOSFET and motor will drive the actual speed based on the signal.
     
  6. Papabravo

    Expert

    Feb 24, 2006
    9,901
    1,723
    I see that the rotary encoder has a single digital output. What does the signal look like when the motor is moving? Is it one pulse per revolution? What does it do when the motor is stopped?

    What speed do you want the motor have?

    I will try to dig up the datasheets on the motor and the encoder since you did not provide any links for me. How come there are no signal names or pin numbers on your schematic? Do you have any concept of how difficult it is to help you with such minimal information?

    Edit #1:
    On your schematic there is no ground connection to the processor.
    There is no frequency indicated for the crystal.
    Diodes D1, D2, D3 are what kind of diodes?

    Edit #2:
    Capacitors C1 and C2 are misplaced. Vs needs to go to the load. As they are drawn they block Vs from going to the load. You have Vb connected to the load. Please consult the datasheet for the IR2102.

    Edit #3:
    Why does the BSP75N in the lower right hand corner have four leads and a tab while the others have only three leads and a tab? Whatever you're using for schematic capture sure can't get the symbols right.

    Edit #4:
    Since you can run the current in either direction, how do you decide which direction you want the motor to go?


    I give up!
     
  7. raj.konduri

    New Member

    May 19, 2008
    1
    0
    iam sorry because iam not working on that now
     
Loading...