A simple calendar clock system is designed using 89C51. The hardware of the clock system is mainly composed of a timing circuit controlled by a single-chip microcomputer, auxiliary circuits such as reset, a key circuit, a digital tube display circuit, and a power supply system. The calendar clock can display year, month, hour, minute, and second; the year, month, hour, and minute can be set. The timing control circuit is controlled by the AT89C51 single-chip microcomputer; the key circuit includes time settings; the time display circuit consists of 7 digital tubes; the power supply system consists of a low-power rectifier filter voltage regulator circuit, which outputs a DC voltage of 5 V to supply power to the main circuit and display circuit. The system block diagram is shown in Figure 1.
During the timing process, the system uses the 89C51's own timer T0 as the clock reference. The accuracy of the timer interrupt is directly related to the accuracy of the entire system. Therefore, obtaining an accurate timing clock signal becomes the key to the system. There are two programmable 16-bit timers/counters in the MCS-51 single-chip microcomputer. In this system design, the AT89C51 timer T0 is used and works in mode 1, with a crystal oscillator frequency of 12MHz.
1 T0 timing interrupt
The circuit logic structure of timer/counter T0 working mode 1 is shown in Figure 2. The TO timing characteristic function register is composed of TL0 (low 8 bits) and TH0 (high 8 bits). The special function register TMOD controls the working mode of the timing register; TCON is used to control the start and stop counting of timers T0 and T1, and manage the overflow flags of timers TO and T1. At the beginning of the program, TL0 and TH0 need to be initialized and programmed to define their working mode and control the counting of T0 and T1. In the design of the system, the timing unit is based on s, and the daily error is required to be ≤10 s. If it is done in a loop, it cannot meet the accuracy requirements. The selection of a 12 MHz crystal can obtain an accuracy of lμs. After analysis, it is determined to use mode l of timer 0. In this mode, timer 0 is a 16-bit timer, that is, the maximum timing value is jFFFFH. Each timing cycle of the 12 MHz crystal is 1 μs, and the maximum timing that can be achieved is FFFFH%1 μs=65635 μs. Even if the maximum value is used, it is impossible to achieve 1 s at a time. In the design, 1 timing of 20 ms is used, and 50 timing interrupts are used to obtain 1 s. The timing value of the 20 ms timer interrupt is: FFFFH-20 ms/1 μs=B1DFH.
2 Program testing and adjustment
Use C language to implement the following code on the Keil uVision3 platform:
#include
#define uehar unsigned char
uehar data MScond=0; //ms
uchar data Scond=0; //s
uchar data Minure=0; //rain
uchar data Hollr=0; //h
void main(void){
EA=1; //Allow CPU interrupt
ET0=1; //Timer 0 interrupt open
TMOD=0xl; //Set timer 0 to mode 1
TH0=0xBl:
TL0=0xDF" //Set the time value to 20 000 μs(20 ms)
TR0=1; //Start timing
while(1);
}
void Time0(void)interrupt 1 using 1
{TH0=0xBl; //20 ms breakpoint (1)
TL0=0xDF; //Set the time value
MScond=MScond+1;
if(MScond==50)
{MScond=0;
Scond=Scond+1;
if(Seond==60)
{Scond=0;
Minute=Minute+1; //Breakpoint (2)
if(Minute==60)
{Minute=0;
Hour=Hour+1; //d, time breakpoint (3)
if(Hour==24)
{Hour=0;}}}}
First debug the accuracy of every 20 ms interrupt, set the debug crystal oscillator to 12 MHz in the options, set a breakpoint at (1) and run again, then record the time of each interrupt, as shown in Figure 3. It takes 551 μs in initialization, and the impact of this item should be considered for each interrupt time. In actual processing, the difference between two interrupt times can be used as the interrupt time interval of the timer. [page]
Through testing, the first time is 0.020 568 00 s, the second is 0.040 580 00 s, and the third is 0.060 59Z 00 s. It can be seen that each interruption is 12 μs longer than the timing value. If the breakpoint is set at (2) and the Logic Analyzer tool is used, the time of the first minute interrupt is 60.036 57 s and the time of the second interrupt is 120.072 57 s, so the actual time per minute is 60.036 s. Then the breakpoint is set at (3) and the time of the first hour interrupt is 3 602.160 576 s and the time of the second hour interrupt is 7 204.320 576 s. The actual time of the hour is 3 602.16 s, as shown in Figure 4.
Why do these errors occur? By analyzing the assembly source code of the interrupt program, it is found that two statements are used when the interrupt program is pushed into the stack: PUSH ACC and PUSH PSW. It takes 4 machine cycles to execute the stack instruction, plus 2 machine cycles to reload TH0 and TL0, 2 machine cycles to increase the count value by 1, and about 4 machine cycles to return from the interrupt, a total of about 12 machine cycles. In order to eliminate the influence of these factors, it is necessary to subtract 12 machine cycles when setting the count value of T0, and add 12 (0CH) to the calculated initial value B1DFH to obtain: B1DFH+12=B1EBH as the new timer initial value. The modified program is:
#include
#define uchar unsigned char
uchar data MScond=0;//ms
uehar data Seond=0; //s
uchar data Minute:0; //rain
uchar data Hour=0; //h
void main(void){
EA=1; //Allow CPU interrupt
ET0=1; //Timer 0 interrupt is turned on
TMOD=Oxl; //Set timer 0 to mode 1
TH0=0xBl;
TL0=OxEB; //Set the time value to 20 000 μs (20 ms) minus 12 μs
TR0=1; //Start timing
while(1);
void Time0(void)interrupt 1 using 1
{TH0=0xBl; //20 ms breakpoint (1)
TL0=0xDF' //Set time value
MSeond=MScond+1:
if(MSeond==50)
{MScond=0;
Seond=Seond+1 ;
if(Scond==60)
{Scond=0;
Minute=Minute+1; //Breakpoint (2)
if(Minute==60)
{Minure=0;
Hour=Hour+1; I/d, time breakpoint (3)
if(Hour==24)
{Hour=0;}}}}
Re-debug the program, still set the debug crystal oscillator to 12 MHz in the options, re-test the actual time of the 20 ms timer, set a breakpoint at (1) and run, and re-record the time of each interrupt, as shown in Figure 5. The initialization time is 556 μs. To eliminate its influence, the time interval between two interruptions is used as the reference clock actually obtained by the timer.
The time of the first interruption is 0.020 556. O s, the second is 0.040 556 000 s, and the third is 0.060 556. O s. It can be seen that the interval between each interruption is exactly 20 ms. If the breakpoint is set at (2) and the Logle Analyzer tool is used, the time of the first interruption is 60.000 57 s, the second is 120.000 57 s, and the interval is exactly 60 s. If the breakpoint is set at (3), the time of the first interruption is 3 600.000 578 s, the time of the second interruption is 7 200.000 578 s, and the time interval is 3 600 s. The test results are shown in Figure 6, which can fully meet the needs of system design.
3 Summary
The accuracy of the system can be improved by analyzing and correcting the timer errors. Of course, the above analysis is implemented under the ideal crystal frequency in a soft environment. In reality, errors may occur due to factors such as crystal deviation. In this test, the main program did not perform other processing, but in the calendar design, the interrupt of timer T1 is also involved to complete the processing of the scanning display circuit, and the external interrupt adjusts the clock, plus some alarm functions, which will inevitably affect the timing accuracy of T0. In addition, the more statements there are in the interrupt program, the more machine cycles are occupied. Therefore, in the design, the analysis tool of Keil uVIsion3 should be fully utilized to obtain accurate clock signals by adjusting the initial count value multiple times. This is of great significance for applications that require accurate clock signals.
Keywords:Keil
Reference address:Using Keil Cx51 to achieve accurate timing of T0
During the timing process, the system uses the 89C51's own timer T0 as the clock reference. The accuracy of the timer interrupt is directly related to the accuracy of the entire system. Therefore, obtaining an accurate timing clock signal becomes the key to the system. There are two programmable 16-bit timers/counters in the MCS-51 single-chip microcomputer. In this system design, the AT89C51 timer T0 is used and works in mode 1, with a crystal oscillator frequency of 12MHz.
1 T0 timing interrupt
The circuit logic structure of timer/counter T0 working mode 1 is shown in Figure 2. The TO timing characteristic function register is composed of TL0 (low 8 bits) and TH0 (high 8 bits). The special function register TMOD controls the working mode of the timing register; TCON is used to control the start and stop counting of timers T0 and T1, and manage the overflow flags of timers TO and T1. At the beginning of the program, TL0 and TH0 need to be initialized and programmed to define their working mode and control the counting of T0 and T1. In the design of the system, the timing unit is based on s, and the daily error is required to be ≤10 s. If it is done in a loop, it cannot meet the accuracy requirements. The selection of a 12 MHz crystal can obtain an accuracy of lμs. After analysis, it is determined to use mode l of timer 0. In this mode, timer 0 is a 16-bit timer, that is, the maximum timing value is jFFFFH. Each timing cycle of the 12 MHz crystal is 1 μs, and the maximum timing that can be achieved is FFFFH%1 μs=65635 μs. Even if the maximum value is used, it is impossible to achieve 1 s at a time. In the design, 1 timing of 20 ms is used, and 50 timing interrupts are used to obtain 1 s. The timing value of the 20 ms timer interrupt is: FFFFH-20 ms/1 μs=B1DFH.
2 Program testing and adjustment
Use C language to implement the following code on the Keil uVision3 platform:
#include
#define uehar unsigned char
uehar data MScond=0; //ms
uchar data Scond=0; //s
uchar data Minure=0; //rain
uchar data Hollr=0; //h
void main(void){
EA=1; //Allow CPU interrupt
ET0=1; //Timer 0 interrupt open
TMOD=0xl; //Set timer 0 to mode 1
TH0=0xBl:
TL0=0xDF" //Set the time value to 20 000 μs(20 ms)
TR0=1; //Start timing
while(1);
}
void Time0(void)interrupt 1 using 1
{TH0=0xBl; //20 ms breakpoint (1)
TL0=0xDF; //Set the time value
MScond=MScond+1;
if(MScond==50)
{MScond=0;
Scond=Scond+1;
if(Seond==60)
{Scond=0;
Minute=Minute+1; //Breakpoint (2)
if(Minute==60)
{Minute=0;
Hour=Hour+1; //d, time breakpoint (3)
if(Hour==24)
{Hour=0;}}}}
First debug the accuracy of every 20 ms interrupt, set the debug crystal oscillator to 12 MHz in the options, set a breakpoint at (1) and run again, then record the time of each interrupt, as shown in Figure 3. It takes 551 μs in initialization, and the impact of this item should be considered for each interrupt time. In actual processing, the difference between two interrupt times can be used as the interrupt time interval of the timer. [page]
Through testing, the first time is 0.020 568 00 s, the second is 0.040 580 00 s, and the third is 0.060 59Z 00 s. It can be seen that each interruption is 12 μs longer than the timing value. If the breakpoint is set at (2) and the Logic Analyzer tool is used, the time of the first minute interrupt is 60.036 57 s and the time of the second interrupt is 120.072 57 s, so the actual time per minute is 60.036 s. Then the breakpoint is set at (3) and the time of the first hour interrupt is 3 602.160 576 s and the time of the second hour interrupt is 7 204.320 576 s. The actual time of the hour is 3 602.16 s, as shown in Figure 4.
Why do these errors occur? By analyzing the assembly source code of the interrupt program, it is found that two statements are used when the interrupt program is pushed into the stack: PUSH ACC and PUSH PSW. It takes 4 machine cycles to execute the stack instruction, plus 2 machine cycles to reload TH0 and TL0, 2 machine cycles to increase the count value by 1, and about 4 machine cycles to return from the interrupt, a total of about 12 machine cycles. In order to eliminate the influence of these factors, it is necessary to subtract 12 machine cycles when setting the count value of T0, and add 12 (0CH) to the calculated initial value B1DFH to obtain: B1DFH+12=B1EBH as the new timer initial value. The modified program is:
#include
#define uchar unsigned char
uchar data MScond=0;//ms
uehar data Seond=0; //s
uchar data Minute:0; //rain
uchar data Hour=0; //h
void main(void){
EA=1; //Allow CPU interrupt
ET0=1; //Timer 0 interrupt is turned on
TMOD=Oxl; //Set timer 0 to mode 1
TH0=0xBl;
TL0=OxEB; //Set the time value to 20 000 μs (20 ms) minus 12 μs
TR0=1; //Start timing
while(1);
void Time0(void)interrupt 1 using 1
{TH0=0xBl; //20 ms breakpoint (1)
TL0=0xDF' //Set time value
MSeond=MScond+1:
if(MSeond==50)
{MScond=0;
Seond=Seond+1 ;
if(Scond==60)
{Scond=0;
Minute=Minute+1; //Breakpoint (2)
if(Minute==60)
{Minure=0;
Hour=Hour+1; I/d, time breakpoint (3)
if(Hour==24)
{Hour=0;}}}}
Re-debug the program, still set the debug crystal oscillator to 12 MHz in the options, re-test the actual time of the 20 ms timer, set a breakpoint at (1) and run, and re-record the time of each interrupt, as shown in Figure 5. The initialization time is 556 μs. To eliminate its influence, the time interval between two interruptions is used as the reference clock actually obtained by the timer.
The time of the first interruption is 0.020 556. O s, the second is 0.040 556 000 s, and the third is 0.060 556. O s. It can be seen that the interval between each interruption is exactly 20 ms. If the breakpoint is set at (2) and the Logle Analyzer tool is used, the time of the first interruption is 60.000 57 s, the second is 120.000 57 s, and the interval is exactly 60 s. If the breakpoint is set at (3), the time of the first interruption is 3 600.000 578 s, the time of the second interruption is 7 200.000 578 s, and the time interval is 3 600 s. The test results are shown in Figure 6, which can fully meet the needs of system design.
3 Summary
The accuracy of the system can be improved by analyzing and correcting the timer errors. Of course, the above analysis is implemented under the ideal crystal frequency in a soft environment. In reality, errors may occur due to factors such as crystal deviation. In this test, the main program did not perform other processing, but in the calendar design, the interrupt of timer T1 is also involved to complete the processing of the scanning display circuit, and the external interrupt adjusts the clock, plus some alarm functions, which will inevitably affect the timing accuracy of T0. In addition, the more statements there are in the interrupt program, the more machine cycles are occupied. Therefore, in the design, the analysis tool of Keil uVIsion3 should be fully utilized to obtain accurate clock signals by adjusting the initial count value multiple times. This is of great significance for applications that require accurate clock signals.
Previous article:Data Acquisition System Based on Printer Parallel Interface
Next article:The structure of MCS-51 series microcontroller
- Popular Resources
- Popular amplifiers
Recommended Content
Latest Microcontroller Articles
- 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)
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- 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
MoreDaily News
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- 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
Guess you like
- The differential voltage measured by the oscilloscope when the RS485 bus is idle is ±3.7V, and the periodic waveform with a frequency of 50hz
- Why would the filter added to the final stage cavity be burned?
- 【Beetle ESP32-C3】GNSS test
- Arteli AT32 uses GPIO to simulate HDMI CEC case
- Analyze the cause of tms320c6455 burning
- Beacon navigation sound positioning and recognition - microphone array
- How to make a boost circuit so that the output is equal to the input voltage?
- SoC chip debugging experience
- Self-powered switch realizes ZigBee transmission
- CLUE development board information