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

ChrisLub
Tue Apr 23 2024, 05:21 pm
Davidbab
Tue Apr 23 2024, 10:41 am
Richardrit
Tue Apr 23 2024, 09:54 am
HenryLaf
Mon Apr 22 2024, 03:50 pm
bleradrar
Mon Apr 22 2024, 06:38 am
ppu-pro_ka
Sun Apr 21 2024, 07:39 pm
Infewow
Sun Apr 21 2024, 06:30 pm
HumanTak
Sun Apr 21 2024, 12:26 pm