51 MCU T0 as precise clock

Publisher:龙爱泉也Latest update time:2016-05-31 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
To use T0 as an accurate clock, the number of interrupts must be reduced in order to achieve the highest possible accuracy. Therefore, mode 1 is chosen, which can count up to 65536 times. However, after mode 1 is interrupted, the timer needs to be re-initialized, which will delay several machine cycles and make it difficult to ensure the accuracy of the clock.

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.

Reference address:51 MCU T0 as precise clock

Previous article:Electronic clock made by 51 single chip microcomputer
Next article:About the accuracy of 51 single chip microcomputer electronic clock

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号