Timer Calculation

Dear Experts, I am Ranjit, working on Atmel At89c52 microcontroller. I have an issu regarding the Timers of the controller

1)I want to generate a delay of 1 seconds.

2) I dont know what value to be loaded to the TH and TL register of the TIMER.

3) I need to know how to calculate these value(which are to be loaded to TH and TL register).

I am in need of the calculation

Please guide me.

Regards

Ranjit Gupta

Reply to
ranjitgupta
Loading thread data ...

First of all, you need to tell us what clocking source you are using. If crystal, what values. If internal RC, what settings. For RC, expect timing to be off 20 to 30%. Then setup the prescaler to the timer and bring out your calculator.

Reply to
linnix

Well, it doesn't really work that way for that chip. Yes, we certainly need to know the clock rate, but that particular processor has no prescaler, and the only clock option is a crystal (or external clock source).

So the way to set up a delay with this processor is to use one of its three available timers (timer 2 is a good choice) in 16-bit auto-reload mode (mode

2), with the reload values (TH2 and TL2) set to a value that will increment to 0 in the desired time (1 second in this case). There are several on-line timer calculator programs that ease the task of calculating the reload values (though it's not very difficult). One is at
formatting link
A commonly confusing part of working with the timers on this processor is that they count up, not down, so the reload value needs to be 65535 *minus* the desired number of clock ticks.

The big problem, though, is that the maximum time you can get is pretty short, even with the timer in 16-bit mode. For example, running at 12 mHz with a standard part (like the one the OP specified), the maximum delay time is 65.535 milliseconds - nowhere near the desired 1-second period. So in the interrupt service routine for this timer there needs to be another value that's incremented when the timer overflows, and counts up (or down) to (from) a value that will result in the desired 1-second period.

What I usually do is set up the timer to 10 mSec (55536, 0xD8F0) and have it increment a byte in the ISR. In the main background loop, I check this byte; whenever it's non-zero, I add it to a byte local to the main loop and reset the timer-incremented one back to zero. When the local byte reaches

100 or greater, I perform any processing that's supposed to occur at one-second intervals, then subtract 100 from the local byte. This way, it's ok for the main loop to take several 10 mSec intervals to complete (which is depressingly easy to have happen on a stock 8051) before servicing the timer, and the resulting period still stays constant. (Relatively, that is - there can still be some jitter when the 1-second event actually is performed, but the overall number of servicings in any longer period of time, like an hour, will still be correct).

-- Mark Moulding

Reply to
Mark Moulding

First, there is a clock and a divider. Say the clock is 4MHz, and the divider is 1024, then you get a timer increment every 1024 * 250ns. Now in oder to reach 1s, you have to let the timer count

1s / (1024 * 250ns), which is in the order of 4000. I don't have a calculator at hand. Now, since the timer counts up until overflow, you have to reload the time with 65536-4000.

Rene

--
Ing.Buero R.Tschaggelar - http://www.ibrtses.com
& commercial newsgroups - http://www.talkto.net
Reply to
Rene Tschaggelar

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.