# How to find out delay time for ISR

Discussion in 'Microcontrollers, Programming and IoT' started by vead, Sep 15, 2017.

473
14
Nov 27, 2011
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: Sep 15, 2017
2. ### electronicsLearner77

174
1
Jul 2, 2015
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.

473
14
Nov 27, 2011
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

4. ### electronicsLearner77

174
1
Jul 2, 2015
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.

473
14
Nov 27, 2011
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 ?

6. ### electronicsLearner77

174
1
Jul 2, 2015
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.