high precision timer using AT89S8252

I am trying to implement a high precision timer using AT89S8252 using the following
code:

void configureTimer0(void)
{
EA = 0;
TMOD &= 0xf0;
TMOD |= 0x01;
ET0 =1;
TH0 = TH_VAL
TL0 = TL_VAL
TR0= 1;
EA = 1;
}

// timer 0 interrupt handle
void timer0 (void) interrupt 1
{
TH0 = TH_VAL;
TL0 = TL_VAL;
P2_1 = !P2_1;
switch(val)
{
case 1: // code
break;
case 2: // code
break;
}
}

void main(void)
{
configureTimer0();
while(1);
}

The problem occurs when I try to insert any code inside the Timer 0 interrupt handle, such as a blinking LED ar a simple switch
statement. The code inside interrupt handle was as simple as possible.
In order to keep a constant rate, I have to use a digital osciloscope to adjust TH_VAL and TL_VAL.

Is there any other way to implement this ? Do I have to constantly calibrate TH_VAL and TL_VAL after inserting any piece of code inside the timer interrupt handle ?


Best regards,

Andre

Parents
  • Hi,

    Do you mean I forgot to set TR= 0 and TR=1 ?
    Is it better to set TR, TH and TL at the end of the ISR ?

    void handler(void) interrupt 1
    {
    // my code
    TR=0;
    TH0=TH0_VALUE;
    TL0=TL0_VALUE;
    TR=1;
    }
    

    I have seen lot of examples doing this. How can I calculate the code running time ? I am new to 8051 programming.

    Regards,

    Andre

Reply
  • Hi,

    Do you mean I forgot to set TR= 0 and TR=1 ?
    Is it better to set TR, TH and TL at the end of the ISR ?

    void handler(void) interrupt 1
    {
    // my code
    TR=0;
    TH0=TH0_VALUE;
    TL0=TL0_VALUE;
    TR=1;
    }
    

    I have seen lot of examples doing this. How can I calculate the code running time ? I am new to 8051 programming.

    Regards,

    Andre

Children
More questions in this forum