Which Timer to Use on AT89S52 -1ms Timer Interrupt ?

Thread Starter

Embededd

Joined Jun 4, 2025
131
Hey mate,

I want to set up a 1ms timer interrupt in AT89S52. I noticed in the datasheet that it has 3 timers — Timer 0, 1, and 2. From what I understand, Timer 2 seems to be the best choice for this, mainly because it supports auto-reload and capture features.

It feels like it would give better accuracy compared to the other timers, but I’m not exactly sure why or how it's more accurate. Am I thinking in the right direction here, or does it not really matter for something like a 1ms interrupt?
 

Papabravo

Joined Feb 24, 2006
22,058
Details matter. What is the crystal frequency and what is the resolution of the input to the timer? Is the number of ticks equal to 1 msec an integer with a reasonable value, i.e. not close to a limiting boundary? What other timer needs do you have for your application?
 

Thread Starter

Embededd

Joined Jun 4, 2025
131
Details matter. What is the crystal frequency and what is the resolution of the input to the timer? Is the number of ticks equal to 1 msec an integer with a reasonable value, i.e. not close to a limiting boundary? What other timer needs do you have for your application?
Right now, I’m just experimenting, my goal is to configure the AT89S52 timer registers to generate an interrupt every 1 MS. Once that’s working, I want to try building a simple real-time clock (without using an external RTC) just for fun and to learn more.

I’m using a 11.0592 MHz crystal, and I was leaning toward Timer 2 because of its auto-reload feature just wondering if it might give more accurate or stable timing compared to other.
 

Papabravo

Joined Feb 24, 2006
22,058
Right now, I’m just experimenting, my goal is to configure the AT89S52 timer registers to generate an interrupt every 1 MS. Once that’s working, I want to try building a simple real-time clock (without using an external RTC) just for fun and to learn more.

I’m using a 11.0592 MHz crystal, and I was leaning toward Timer 2 because of its auto-reload feature just wondering if it might give more accurate or stable timing compared to other.
The use of the 11.0592 MHz crystal is useful for baud rate generation but not so much for 1 ms. timer interrupts. In this case you need to choose which application is more important. You can have accurate baud rate generation or accurate time interrupts but not both.
 

Thread Starter

Embededd

Joined Jun 4, 2025
131
The use of the 11.0592 MHz crystal is useful for baud rate generation but not so much for 1 ms. timer interrupts. In this case you need to choose which application is more important. You can have accurate baud rate generation or accurate time interrupts but not both.
that makes sense now! Thanks for pointing that out.

In my current setup, I’m not using UART at all. I’m just planning to display the time directly on a 16x2 JHD LCD, so accurate timing is my main goal for now.

But you raised a really good point… what if later I want to send real-time data over UART instead of the LCD?
In that case, I’d definitely need accurate baud rate generation too.

So just curious if I wanted both accurate 1-ms interrupts and reliable UART communication, which timer would you recommend for each task? Like, is there a best combo, say Timer 2 for timekeeping and Timer 1 for UART baud?
 

MrChips

Joined Oct 2, 2009
34,628
8 MHz and 16 MHz quartz crystals are popular values for MCU oscillators. I don’t know about AT89S52. I would have to check the datasheet. Usually, UART modules don’t need a timer. They have their own baud clock dividers.

Crystal oscillators are very stable but not accurate for time-of-day clocks. If you want to maintain accurate time, use the AC 50 or 60 Hz line frequency, depending on the utility services in your country.
 

Papabravo

Joined Feb 24, 2006
22,058
8 MHz and 16 MHz quartz crystals are popular values for MCU oscillators. I don’t know about AT89S52. I would have to check the datasheet. Usually, UART modules don’t need a timer. They have their own baud clock dividers.

Crystal oscillators are very stable but not accurate for time-of-day clocks. If you want to maintain accurate time, use the AC 50 or 60 Hz line frequency, depending on the utility services in your country.
In the case of basic members of the 8051 family the system clock is used for baud rate generation. If you want to start with accurate timers then I suggest a 12 MHz. crystal. To get acceptable serial communication you have to be satisfied with a lower baud rate such as 2400 or 4800. These rates are certainly fast enough for keyboards and multiline LCD displays. Painting a 25 line by 80 character terminal screen is painfully slow below 9600.

ETA: I see that process improvements allow crystal frequencies significantly higher than the original Intel designs. Doing things with timers and serial communication has a sensitive dependence on the choice of the operating frequency. That is where you should start.
 
Last edited:

MrChips

Joined Oct 2, 2009
34,628
UART communications between two processors do not require exact baud clocks. A few percent difference is tolerated.

Since 8 MHz crystal is a common choice, two MCUs running with 8 MHz crystals will differ by a few ppm, i.e. better than ±0.001%.
 

Papabravo

Joined Feb 24, 2006
22,058
UART communications between two processors do not require exact baud clocks. A few percent difference is tolerated.

Since 8 MHz crystal is a common choice, two MCUs running with 8 MHz crystals will differ by a few ppm, i.e. better than ±0.001%.
It is somewhat dependent on the speed youare trying to achive and the discrete rates you can achieve. 115,200 can be quite difficult if the choices are 2% apart.
 

Rf300

Joined Apr 18, 2025
72
Why do you need a 1 ms timer tick only for a clock. This tick frequency is used in OSes for more powerful processors than an 8051. Wha about using a 10.4167 ms (1/(96 Hz)) tick. This can easily be derived from a 11.0592 MHz oscillator, you simpy count to 96 inside your interrupt routine and you have 1 second for your clock.

Another issue you have to think about: A standard 50 ppm oscillator will lead to an error in your displayed time of up to 30 s per week!
 

Papabravo

Joined Feb 24, 2006
22,058
There are two main documents we need to refer to in getting a handle on the problem:
  1. doc1919.pdf by Atmel documents the specifics of the AT89S52
  2. doc4316.pdf by Atmel documents the 8051 hardware generally
The SFR maps in these two documents have some differences which must be taken into account.
  1. doc4316.pdf has an SFR @0x8F called CKCON which is not defined in the doc for the AT89S52
  2. doc1919.pdf has SFR registers named T2MOD, AUXR1, WDTRST, DP1L, and DP1H which are not defined in the 8051 Hardware Manual
  3. The hardware manual talks about SFR register for baud rate generation, BRG and BRL, which do not seem to be accounted for in either document in the respective SFR maps.
From that, I surmise for the time being, that we must use Timer 1 or Timer 2 for baud rate generation in the AT89S52

The datasheet for the part says we can use any crystal from DC to 33 MHz. Having the oscillator frequency be a multiple of 12 provides convenient instruction time in multiples of 1 microsecond. If we choose 12 MHz for our crystal, then we can do 9600 baud serial communications with decent accuracy by setting the T2 capture registers to 0xFFD9 giving a baud rate of 9615.38

Timer 1 can then be used for a 1 millisecond timing interrupt with a 12 MHz. crystal but you may have to reload the timer value and correct for it in the interrupt routine. You may be able to use the auto-reload feature in 8-bit operation since the input clock is divided by 6.

I think 115,200 baud is out of the question without going to a higher oscillator frequency which will greatly increase you power consumption.
 
Top