Newbie - TIMERS

Jun 15, 2005 2 Replies

Hello,



I'm quite lost with the configuration of the timers reload.



For example: my 8051 is working at 6MHZ, and I want an interruption on the timer0 each 1 ms, How should I configure TH0 and TL0?



And please, explain me how must be calculated for different interval values cause I want to learn as well.



Thank you.



Don't know about the 8051 (and it probably varies between the dozens of

8051 families - it pays to be specific in questions), but timer peripherals all work in pretty much the same way for simple interrupts. The reference clock (6 MHz here) passes through a divide-by-power-of-two prescaler, which may be fixed or configurable (see your specific chip's datasheet), and clocks a counter. The counter may be 8-bit or 16-bit (or more, for some micros) - I'm guessing you have a 16-bit timer here. The counter either counts up to a set value and interrupts on overflow, or counts down from the set value and interrupts on underflow. Either way, the counter target value is calculated in the same way - take your reference frequency, divide it by the prescaler, and then by your target frequency, to get the counter value. E.g., for a prescaler of 4, this would give you: 6,000,000 / 4 / 1,000 = 1500. Put (1500 div 256) = 5 in the high register, and (1500 mod 256) = 220 in the low register. For some timers, you should use one less that this count value if you are being precise - check your datasheet.

You also have to set up the prescaler, start the timer, enable interrupts, and so on.

Hey.

formatting link
has all the 8051/2 info you will ever need. A timer tutorial is located at
formatting link

mmmKay

======================================================================== Michael Kesti | "And like, one and one don't make | two, one and one make one." mrkesti at comcast dot net | - The Who, Bargain

Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required