Discussion in "8051 Discussion Forum" started by    joepotter    Nov 25, 2010.
Thu Nov 25 2010, 02:43 am
Hi
I want to make an Interrupt with 1 seconde delay. I dont know how I can setup the THO, and TL0 values .
The timer : 65536 -(1seconde/1,852micro-seconde) is negativ .
How I can resolve this problem. ??

Thanks
Thu Nov 25 2010, 03:14 am
This is a common question.
Set a timer to interrupt at 50mS intervals.
Count 20 interrupts and you have one second.

Look for similar posts in the 8051 forum and you will find code examples.
joepotterbagusdj like this.
Thu Nov 25 2010, 11:03 am
hi joepotter
see this

general formula for timer is

(65536-xx)*machine cycle= delay u want
where xx is a 16 bit no having value of TH0 and TL0

when timer run in 16 bit mode with 11.0592 mhz max delay
that can be generated is 72 ms almost.

now let say u use 12MHz crystal then Machine cycle is calculated
as for every crystal value

(crystal value / 12)= frequency

(1 / frequency) = Machine cycle

now for 12 MHz
12 Mhz/12= 1MHz
( 1 / 1MHz ) = 1us

(65536-xx)*1us= 50 ms
(65536-xx)=50,000
xx=15536
in hex it is
xx=0x3CB0
U can use window calculator for conversion from decimal to hexadecimal
now
TH0=0X3C
TL0=0XB0

it generate a 50 ms delay @ 12 MHz crystal
declare a variable and enable timer interrupt increment counter on each interrupt after 50 ms
when count=20 it means delay is 1 sec
20*50ms=1000ms=1sec
sample code
```#include<reg51.h>

unsigned char count;
void timer () interrupt 1
{
TH0=0X3C;
TL0=0XB0;
count++;
}
void main (void)
{
count=0;
TMOD=0X01;
TH0=0X3C;
TL0=0XB0;
IE=0X82;      // enable timer 0 interrupt
TR0=1;          //run timer 0
while (1)
{
if (count==20)
count=0;
// it mean delay is 1 sec
}
}
```

[ Edited Thu Nov 25 2010, 11:11 am ]
Tags 8051 timer8051 timer calculationtimer example 8051

### Recent

RobertGug
Tue Jun 18 2024, 04:38 pm
Jasonabepe
Tue Jun 18 2024, 04:24 pm
Thomcyday
Tue Jun 18 2024, 08:38 am
WalterWib
Tue Jun 18 2024, 05:44 am
SialisshopWat
Mon Jun 17 2024, 09:22 pm
Thomasraima
Mon Jun 17 2024, 07:17 pm
EriksaLaf
Mon Jun 17 2024, 03:13 pm
Anthonyjinee
Mon Jun 17 2024, 06:23 am