Discussion in "8051 Discussion Forum" started by    joepotter    Nov 25, 2010.
Thu Nov 25 2010, 02:43 am
#1
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
#2
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
#3
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

Get Social

Information

Powered by e107 Forum System

Downloads

Comments

Bobbyerilar
Thu Mar 28 2024, 08:08 am
pb58
Thu Mar 28 2024, 05:54 am
Clarazkafup
Thu Mar 28 2024, 02:24 am
Walterkic
Thu Mar 28 2024, 01:19 am
Davidusawn
Wed Mar 27 2024, 08:30 pm
Richardsop
Tue Mar 26 2024, 10:33 pm
Stevencog
Tue Mar 26 2024, 04:26 pm
Bernardwarge
Tue Mar 26 2024, 11:15 am