Using Keil Cx51 to achieve accurate timing of T0

Publisher:EnigmaticCharmLatest update time:2015-04-23 Source: eechinaKeywords:Keil Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
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

Previous article:Data Acquisition System Based on Printer Parallel Interface
Next article:The structure of MCS-51 series microcontroller

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号