This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Doubts on Timer 0 & Timer 1 of 89C51, Timer 2 of 89C52

For Timer 2 of 89C52, to initialise it, the routines i used are as follow:

void setTimer2 (void)
{
TR2=0;
count20ms=0;
T2CON=0;
RCAP2H=(-40000) >> 8;
RCAP2L=(-40000) & 0x0ff;
TH2=RCAP2H;
TL2=RCAP2L;
TF2=0;
ET2=1;
EA=1;
}

if i were to use Timer 0, is the routine i used below correct??? -->

void setTimer0 (void)
{
TR0 = 0;
count20ms = 0;
TMOD = (TMOD & 0xf0) | 1;
TH0 = (-40000) >> 8;
TL0 = (-40000) & 0x0ff;
TF0 = 0;
ET0 = 1;
EA = 1;
}

If the above routine for Timer 1 initialization is wrong, can someone tell me what is wrong and what i should write???

Last question, i used Timer 2 as a timer (to keep track of something). If i were to use Timer 0 or Timer 1, can i still use it as a timer like Timer 2. If yes, which mode should i used???

thanks...

  • Hi,

    A couple of observations (which may) answer your question or raise further questions without knowing fully how you intend to use these functions, but here goes:-

    1. From your variable name count20ms I assume that you are trying to count 20ms delays for timer 0 and timer 2 but because of how you have used this variable if setTimer2 or setTimer0 are used later then it could have the effect of destroying your count value (if this is important) because it will be reset to 0 - is this what you want?, but to answer if the timer reload value(s) are correct I need to know the frequency that you are running the processor at.

    2. Personally I would of written (within setTimer0 function) TMOD |= 1; so that if you later use timer 1 you wont get caught out by resetting the upper nibble in TMOD.

    3. In both timer functions I would NOT include EA=1; but would possibly of put them after initialising both timers eg.

     setTimer2();
     setTimer0();
     EA = 1;
    

    4. I would probably include TR2=1; and TR0=1; in setTimer2 and setTimer0 respectivly to start them running if you havent included them elsewhere in your code.

    Hopefully this should help you a little but I think you will be able to find further info within previous forum posts on timers and example code as well.

    Mark :-)