 Login Join Maker Pro

### Network # How to find out delay time for ISR

Nov 27, 2011
473
Hello
If Interrupt service routine run 200 per seconds how to calculate timer higher value TH0 and timer lower value TL0 v ?
Code:
#include<reg51.h> /*AT89c51*/
sbit led1 = P1^0;         //LED connected to P0 of port 1
sbit led2 = P1^1;         //LED connected to P1 of port 1

void timer(void) interrupt 1         //interrupt no. 1 for Timer 0
{
led1=~led1;         //toggle LED on interrupt
TH0= ?;        // what will TH0
TL0=?;          // what will TL0
}
main()
{
TMOD = 0x01;         // mode1 of Timer0
TH0 = ?;        // initial values loaded to timer
TL0 = ?;
IE = 0x82;        // enable interrupt
TR0 = 1;        //start timer
while(1);        // do nothing
}

Last edited:

#### electronicsLearner77

Jul 2, 2015
233
200 per seconds is 5ms, so you require a delay of 5ms. If you are operating with a crystal of 12 MHz then your clock source to the timer will be 1Mhz or 1us per tick. So for 5ms or 500us the number of ticks is 500. Hence the value to be loaded into the timer registers is 65536 - 500 = 65036 = 0xFE0C.
TL0 = 0x0C;
TH0 = 0xFE.
Based on your clock source TH0 and TL0 will vary.

• Nov 27, 2011
473
200 per seconds is 5ms, so you require a delay of 5ms. If you are operating with a crystal of 12 MHz then your clock source to the timer will be 1Mhz or 1us per tick. So for 5ms or 500us the number of ticks is 500. Hence the value to be loaded into the timer registers is 65536 - 500 = 65036 = 0xFE0C.
TL0 = 0x0C;
TH0 = 0xFE.
Based on your clock source TH0 and TL0 will vary.
how did you calculate this value 200 per seconds is 5ms
time delay = machine cycles * 1.085uS
does it mans 200 times is machine cycles

#### electronicsLearner77

Jul 2, 2015
233
I only assumed you want 200 counts for 1 sec, so 1/200 = 5ms. But the concept is find out how much delay you want for your application. Based on that you calculate the Timer register values.

Nov 27, 2011
473
I only assumed you want 200 counts for 1 sec, so 1/200 = 5ms. But the concept is find out how much delay you want for your application. Based on that you calculate the Timer register values.
generally we know the delay time and we need to find out how many time loop will run to achieve delay time. For example how do we know that how many time loop will run for 10 ms delay ?

#### electronicsLearner77

Jul 2, 2015
233
I am not sure if i have understood your problem. Delays can be acheived in 2 ways. Suppose you want a delay of 5ms then
1. For 5ms calculate the Timer register values and load them directly.
2. Calculate the timer register values for 1ms. Then inside the 1ms interrupt declare a counter variable. If that variable increments by 5 means, it has achieved 1ms * 5 = 5ms delay.

Replies
15
Views
450
Replies
10
Views
824
Replies
21
Views
2K
Replies
48
Views
3K
Replies
8
Views
2K