There are two ways to solve this problem.
The first method: You can calculate the number of machine cycles used to re-initialize the timer during interrupt processing, and remove these machine cycles from the initial value you calculated as compensation. This method is very accurate only in that it can be executed on time after each timer interrupt, but in fact no one can predict when the interrupt will be executed, so this method can only be as accurate as possible.
The second method is to calculate a coincidental initial value so that TL0 is exactly equal to 0x00. In this way, after each interrupt overflow, TL0 starts counting from 0x00. Even if the interrupt is not executed, TL0 will continue to count. Using this, in the interrupt processing function, you only need to reassign TH0 and ignore TL0. The following is a sample program:
//Timer T0 clock parameters
unsigned char T0_S = 0; //Seconds
unsigned char T0_M = 0; //minutes
unsigned char T0_H = 0; //time
unsigned char T0_Cycle = 0; //Number of cycles
//This program uses a 22.1184MHz crystal oscillator, with a timing of 25ms each time, and 40 cycles taking exactly 1s.
void Timer0_Init(void) //T0 initialization function
{
TMOD = 0x01; //Set T0 working mode 1
TH0 = 0x4c; //(65536-46080)/256, set the initial value to 46080, crystal oscillator 22.1184MHz,
//Each machine cycle is 0.5425 microseconds, timing is 25ms
TL0 = 0x00; //(65536-46080)%256, TL0 is exactly = 0x00
IE |= 0x82; //Enable interrupt
TR0 = 1; //T0 starts timing
}
//
void Timer0(void) interrupt 1
{
TH0 = 0x4c; //Reassign TH0
//TL0 = 0x00; //Do not assign a value to TL0, let it continue counting
TF0 = 0; //Timer overflow clears to 0
T0_Cycle++;
if(T0_Cycle == 40) //Loop 40 times, 25ms each time, timer 1s
{
T0_Cycle = 0 ;
T0_S++ ;
if(T0_S == 60)
{
T0_S = 0;
T0_M++;
if(T0_M == 60)
{
T0_M = 0;
T0_H++;
if(T0_H == 24)
{
T0_H = 0;
}
}
}
}
}
In the above program, even if the initialization of T0 interrupt will delay several machine cycles, it does not matter if the interrupt is not executed in time, because the count of TL0 is not affected. However, there is one situation that must be paid attention to, although the possibility of this situation is not great. If T0 interrupt is not responded to for a long time, TL0 overflows again, and the error of this method is large.
After testing, the second method is relatively accurate. The clock runs 10 seconds faster in a day. This error should be a problem with the crystal oscillator itself. If it is a problem with the timer, it should be slower, not faster.
Previous article:Electronic clock made by 51 single chip microcomputer
Next article:About the accuracy of 51 single chip microcomputer electronic clock
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Brief Analysis of Automotive Ethernet Test Content and Test Methods
- How haptic technology can enhance driving safety
- Let’s talk about the “Three Musketeers” of radar in autonomous driving
- Why software-defined vehicles transform cars from tools into living spaces
- How Lucid is overtaking Tesla with smaller motors
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- About the problem of AGND and DGND of microcontroller
- "Summer Benefits to Celebrate Dragon Boat Festival" Selected Resources for the Grab-a-Hot Event
- Reshaping the engineer experience: Tektronix invites you to attend the global launch of the new 2-series MSO at 2 pm on June 8 (today)!
- How to draw multiple boards with the same functional schematics on one PCB
- Quickly build the Hongmeng development environment for BearPi HM Nano
- Variable memory depth and segmented memory of LOTO oscilloscope
- [SAMR21 new gameplay] 21. Touchio
- STM32 learning the ninth post can not turn off the PWM pin
- I have a SOT23-6 package with a silk screen model number.
- 【CH579M-R1】+PWM breathing light and serial communication experiment