Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] 8051 Timer Programming.

Status
Not open for further replies.

Titus Manoj Kumar

Junior Member level 2
Junior Member level 2
Joined
Oct 18, 2013
Messages
24
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Location
Sriperumpudur, Tamil Nadu
Activity points
178
Dear All,

Am working on 8051 timer programming. I have a doubt in that,

TH0 = 0xFC;
TL0 = 0x66;

Here as we know the Timer 0 reg.is assigned a hex value of FC66.

This one is said to be the estimated value for 1ms delay.

How do we calculate the time with regards to hex values.?

Please help.
Thanks.
 

Hi,

usually the descripton of the timer functions you can find in the datasheet.


if you need to calculate hex to decimal and back, look for online calculators.


Klaus
 

Manoj,

Can you specify the timer mode which you are using for this and crystal frequency

Also refer these

**broken link removed**

Google search for "calculator for 8051 family by kaushik". This simple program you can generate 8051 code

bassa
 

Check this tool for result.

Timer0.jpg

Details:

Timer mode 1 = 16 bit timer and timer will overflow @ FFFF+1 (65535+1)
Your timer value = FC66

Total machine cycles need to overflow = FFFF+1 - FC66 = 039A ( 65536 - 64614 = 922)
Total time = Total machine cycles need to overflow * machine cycle time = 922*1.085 = 1000us = 1ms

bassa
 
Baasa,

Thanks much for the help.

I need to control a DC Motor with a period of 3 minutes. So whats the xtal frequency should I use? and what are the values should i load into TH0 and TL0 registers?
 

8051 Timer Functions.

Dear all,

I need to control a DC Motor with a period of 3 minutes using timer function.

So whats the xtal frequency should I use?

And what are the values should i load into TH0 and TL0 registers?

Please do help.

Thanks.
 

Baasa,

I need to control a DC Motor with a period of 3 minutes. So whats the xtal frequency should I use? and what are the values should i load into TH0 and TL0 registers?

To reduce error, it is better to use 12MHz xtal frequency.

There are many ways to generate 3 minutes time delay, configure the timer to 50ms and loop 3600 (20*60*3) times or use software delay loops or find out RTC
 

Re: 8051 Timer Functions.

You can use any xtal but keep in mind that the xtal value will affect the delay ..
8051's instruction cycle time is = Xtal_Frequency / 12 .. So if you use 12Mhz it will make the calculations a bit easier .. Another commonly Xtal value used is 11.0592 Mhz because it is useful when you use serial communication ..
Now, for your delay of 3 minutes, you can do it in many ways .. You can make a delay of 1sec and call it 180 times (by using for loop) ..
 

Hi,

You need us to calculate for you how many ms in 3 minutes are?

Klaus

- - - Updated - - -

Hi,

To reduce error, it is better to use 12MHz xtal frequency.
Mathematically this is true. True if you want to program a software clock.

But if the value for the timer setup is about 50000 then the estimated error is max. +/- 0.5/50000.
This is about +/- 10 ppm. That is in the range of the quartz frequency tolerance.

The max error with 3 minutes is at about +/- 1ms.
I am very sure that this error should give no problem with controlling a dc motor.

Klaus
 

So whats the xtal frequency should I use?

And what are the values should i load into TH0 and TL0 registers?
xtal frequency is always fixed i.e. 11.0592MHZ crystal.
now if you want 3 second delay as the timers delay support max delay of 71.111ms so you will have to loop the timer.
so let us take (50ms delay & loop it 60 times)together 60times again it will give you 3min delay
calculations:
65535-required value loaded in THTL+1=50millsec/1.085microsec (i hope you know this formula)
required value to be loaded in hex is:
TH VALUE:4Ch
TL VALUE:00h
first of all whether you want it to be written in c or asm?

in asm language delay subroutine
Code:
mov tmod,#01h
mov r1,#60
up1:mov r0,#60    //counter to repeat 50ms for 60 times
up:
mov th0,#4ch
mov tl0,#00h
setb tr0  //start timer
here:jnb tf0,here      //wait till timer flag raised
clr tf0
clr tr0
djnz r0,up
djnz r1,up1
ret
in c :
Code:
{
unsigned int y,x;
TMOD=0X01;   //timer0 mode 1
for(y=0;y<=60;y++)
{
for(x=0;x<60;x++)
{
   TH0=0X4C;
TL0=0X00;
TR0=1;
while(TF0==0);      //wait till overflow flag raised
TR0=0;
TF0=0;
}
see atlast o/p in logic analyser of your compiler to verify
 
Last edited:
Hi,

so let us take 50ms delay & loop it 60 times it will give you 3min delay

please re-calculate this, otherwise it may confuse the OP.


Klaus
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top