So, 5 seconds after the program starts, it should turn the LED on, yes?Program: I want to set interrupt for 5 seconds. When interrupt happen, LED should be turn ON, otherwise it should be turn off.
Timer is use to generate time delay. I need 5 seconds interrupt time period. lets suppose I am using timer 0 and mode 1
I am stuck to calculate TH0 and TL0 valueSo, 5 seconds after the program starts, it should turn the LED on, yes?
Your calculation is off by 10, probably a typo:
5 sec / 1.085uS per tick = 4608294 ticks.
But the timer is only 16 bits so the most it can hold is 2^16 tiks- 65536 tiks.
65536 * 1.085 uS/tik = 71.1ms.
So you can't get a 5 second interrupt period just using the timer. You have to set the timer to interrupt at some period it can handle, 10ms for example, then COUNT the interrupts with a separate counter.
5 sec / 10ms = 500. So count 500 interrupts to make 5 sec.
Parth, do you understand Hexadecimal? You can think of TH0 and TL0 as two digits of a 256-base number, that's the key.I am stuck to calculate TH0 and TL0 value
suppose I want to set interrupt for 20 ms than how to calculate TH0 and TL0
20,000*1.085= 18433 timer ticks
how to calculate TH0 and TL0 for 20ms ?
Thanks for your detail explanations. Now I understood how to calculate TH0 and TL0Parth, do you understand Hexadecimal? You can think of TH0 and TL0 as two digits of a 256-base number, that's the key.
For example:
That last calculation gives you a clue... except that the timer counts UP, not down. That is, the interrupt is called when the timer overflows from FFFF to 0000. And since the maximum value of the timer is FFFF = 65,535, you need to subtract 18,433 from 65,536 to obtain the starting point of the timer and define the values of TH0 and TL0...
- In decimal (base 10), 18,433 = 1 x 10^4 + 8 x 10^3 + 4 x 10^2 + 3 x 10^1 + 3 x 10^0
- In Hexadecimal (base 16), 18,433 = 4801H = 4 x 16^3 + 8 x 16^2 + 0 x 16^1 + 1 x 16^0
- In base 256, 18,433 = 72 x 256^1 + 1 x 256^0
So, to obtain 18,433 timer ticks before it triggers an interrupt, you need the following calculation:
Timer starts at 65,536 - 18,433 = 47,103
And in base 256, 47,103 = 183 x 256^1 + 255 x 256^0
That means that the values you have to assign to the registers (in decimal) are:
TH0 = 183
TL0 = 255
Got it?
I am little bit confuse. I read your post many times but I couldn't understand. I want to start with simple example. Suppose if program start from 0 us and when it reach at 10 ms, at this point interrupt should be enable. and if interrupt is enable, LED should be turn ON and when Interrupt is disabled, LED should be Turn off. Suppose Interrupt happen for 20 ms means LED will Turn on for 20 ms after that Interrupt will disable and processor will return as what he was doing.The next step would be to detail the flow chart. In particular, when you set up an interrupt driven system, you always do things in this order:
- With interrupts disabled from reset, fully configure the system.
- Clear any pending interrupt flags.
- Start the timers
- Enable the timer interrupt
- Enable global interrupts.
That way you don't get interrupted with a partially configured system.
Since you need 5 sec after startup and the timer interrupts every 20ms(?), you'll need to add a counter in the interrupt routine to count enough of those 20ms interrupts to make 5 sec. At that point, clear the counter and proceed with whatever you were going to do at 5 seconds. Note that by doing this, the 8051's timer becomes a prescaler to the main counter.

I am confuse too. What happened to what you wanted before i.e. turn LED on after 5 seconds? I don't think a 'simple example' is any simpler than just moving on and doing what you want it to do.I am little bit confuse. I read your post many times but I couldn't understand. I want to start with simple example. Suppose if program start from 0 us and when it reach at 10 ms, at this point interrupt should be enable. and if interrupt is enable, LED should be turn ON and when Interrupt is disabled, LED should be Turn off. Suppose Interrupt happen for 20 ms means LED will Turn on for 20 ms after that Interrupt will disable and processor will return as what he was doing.
I want to do two things Generate interrupt at 10 ms after start of program and enable interrupt for 20 ms.
View attachment 136175
I am trying to explain what I want to doI am confuse too. What happened to what you wanted before i.e. turn LED on after 5 seconds? I don't think a 'simple example' is any simpler than just moving on and doing what you want it to do.
The bullet points in my post are the order that the init functions should follow but your flow chart shows almost exactly the opposite.

I can calculate TH0 and TL0 value. I understand how to set interrupt. but I don't know how to set more than one interrupt.OK, then in the program initialization set the timer to the number of counts for 10ms (using the method @cmartinez or I showed you above) then in the interrupt routine reload it to the 20ms value.

I will post complete code with design but I am little bit confuse on your two statementDid you get the basic interrupt working first? If so, post the code. If not, get the basics working before moving on.
But that is getting ahead of yourself and you keep changing what you want to do. The advice I would give is the same as always so I won't repeat it.
After reset, interrupts are disabled as they should be. Configure the system just means to initialize the IO, set the timer values etc. like you would when starting any program.I will post complete code with design but I am little bit confuse on your two statement
What are meaning of these statement related program ?
- With interrupts disabled from reset, fully configure the system.
- Clear any pending interrupt flags
Sorry for jumping on another topics before completing first one

#include<reg51.h>
sbit LED = P1^7;
int main(void)
{
/* Make all ports zero */
P0 = 0x00;
P1 = 0x00;
P2 = 0x00;
P3 = 0x00;
/* Stop Timer 0 */
TR0 = 0;
/*Clear the interrupt flag */
TF0 = 0;
/*Set timer0 in mode 1*/
TMOD = 0x01;
/* 10 ms reloading time */
TH0 = 0x24;
TL0 = 0x00;
/* Enable Timer0 interrupts */
ET0 = 1;
/* Global interrupt enable */
EA = 1;
/* Start Timer 0 */
TR0 = 1;
/* Do Nothing */
while (1)
{
}
}
/* It is called after every 10ms */
void timer(void) interrupt 1
{
LED=~LED; /*toggle LED on interrupt */
TH0=0x48; /* initial values loaded to timer */
TL0=0x01;
}
OK I will clear timer flag in interrupt flag. Yes I have tested it. What's the next step ?@Parth786 That looks pretty good except you need to clear the timer interrupt flag TF0 in the interrupt routine.
Did you run it on the simulator?
Perth, I think you're forgetting that the timer counts up. So for 10ms, the registers value should be 65,536 - 9216 = 56,320. And for 20ms it would be 65,536 - 18,433 = 47,103Problem statement
Interrupt Design
- Set interrupt at every 10 ms
- When interrupt happen blink LED for 20ms
View attachment 136299
Calculation for TH0 and TL0
10 ms = 10000/1.085= 9216 timer tick
Decimal 9216 = 2400 H
TH0 = 24 H
TL0 = 00 H
20 ms = 20000/1.085 = 18433 timer tick
Decimal 18433 = 4801H
TH0 = 48 H
TL0 = 01 H
Program code
C:#include<reg51.h> sbit LED = P1^7; int main(void) { /* Make all ports zero */ P0 = 0x00; P1 = 0x00; P2 = 0x00; P3 = 0x00; /* Stop Timer 0 */ TR0 = 0; /*Clear the interrupt flag */ TF0 = 0; /*Set timer0 in mode 1*/ TMOD = 0x01; /* 10 ms reloading time */ TH0 = 0x24; TL0 = 0x00; /* Enable Timer0 interrupts */ ET0 = 1; /* Global interrupt enable */ EA = 1; /* Start Timer 0 */ TR0 = 1; /* Do Nothing */ while (1) { } } /* It is called after every 10ms */ void timer(void) interrupt 1 { LED=~LED; /*toggle LED on interrupt */ TH0=0x48; /* initial values loaded to timer */ TL0=0x01; }