Initial Calculation Formula of Single Chip Microcomputer Timer
First, 51 MCU Timer Initial Value Calculation1. Method One
Void main(void)
{
S1 = 1;
TMOD = 0x01; // Using mode 1 of timer T0
TH0 = (65536 - 46083) / 256; // Set the high 8-bit initial value of timer T0
TL0 = (65536 - 46083) % 256; // Set the low 8-bit initial value of timer T0
}
The interrupt service function for timer T0 is as follows:
Void Time0(void) interrupt 1 using 0 // Timer T0's interrupt number is 1, using the first set of working registers
{
Count++; // Each time an interrupt occurs, increment the count
if (count == 20) // If the number of interrupts reaches 20
{
count = 0; // Reset the count to 0
}
s++; // Increment seconds by 1
}
While reading a program online, I came across the question: how is the initial timer value 46083 calculated? Typically, when using AT89C2051, for a 50ms timing, the formula would be TH0 = (65536 - 50000) / 256. It seems that a 12MHz crystal is used, and 20 interrupts make up 1 second.
2. Method Two
Calculating the initial value for a 10ms timer:
1) Crystal frequency: 12MHz
Divide 12MHz by 12 to get 1MHz. So one second equals 1,000,000 machine cycles. For 10ms, it’s 10,000 machine cycles.
65536 - 10000 = 55536 (0xD8F0)
TH0 = 0xD8, TL0 = 0xF0
2) Crystal frequency: 11.0592MHz
Divide 11.0592MHz by 12 to get 921,600Hz. So 10ms equals 9,216 machine cycles.
65536 - 9216 = 56320 (0xDC00)
TH0 = 0xDC, TL0 = 0x00
3. Method Three
Calculating the initial value for a 50ms timer:
1) Crystal frequency: 12MHz
12MHz divided by 12 gives 1MHz. 50ms equals 50,000 machine cycles.
65536 - 50000 = 15536 (0x3CB0)
TH0 = 0x3C, TL0 = 0xB0
2) Crystal frequency: 11.0592MHz
11.0592MHz divided by 12 gives 921,600Hz. 50ms equals 46,080 machine cycles.
65536 - 46080 = 19456 (0x4C00)
TH0 = 0x4C, TL0 = 0x00
Usage Instructions
For example, with a 12MHz crystal, there are 1,000,000 machine cycles per second. The timer overflows every 65536 cycles. To minimize the number of overflow interrupts (such as 50ms), we reduce interference with the main program.
During development, different crystal frequencies may be used, such as 11.0592MHz for C51 microcontrollers, which is ideal for serial communication. A 12MHz crystal is easier for timer calculations and is more convenient for plug-and-play use.
At 12MHz, each machine cycle is 1μs. The maximum time intervals for different modes are as follows:
- Mode 0 (13-bit timer): 8.192ms
- Mode 1 (16-bit timer): 65.536ms
- Mode 2 (8-bit timer): 256μs
To calculate the initial value for a 5ms timer at 12MHz:
M = 2^16 - (5 * 10^-3 * 12 * 10^6) / 12 = 60536 = 0xEC78
THx = 0xEC, TLx = 0x78
For 50ms, THx = 0x3C, TLx = 0xB0
For 10ms, THx = 0xD8, TLx = 0xF0
Another way to calculate: When the crystal is 6MHz, each machine cycle is 2μs. For 1ms timing:
(2^16 - x) * 2μs = 1000μs → x = 2^16 - 500 = 65036
At 12MHz, each machine cycle is 1μs. For 50ms timing:
(2^16 - x) * 1μs = 50000μs → x = 2^16 - 50000 = 15536
The internal crystal frequency determines the timer’s operation. The counter counts external pulses on P3.4 (T0) or P3.5 (T1) during a negative edge transition.
When using a 6MHz crystal, the maximum counting frequency is 500kHz. This ensures accurate timing and reliable performance in various applications.
Zooke Connectors Co., Ltd. , https://www.zooke.com