Solution to 51 microcontroller clock accuracy error

Publisher:alpha11Latest update time:2023-06-15 Source: elecfans Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

A few days ago, I made an electronic clock using STC89C52 microcontroller. After a period of experiments, I found that there is an error in the time accuracy, and one minute is about 4 seconds slower.


This is amazing. Ten minutes will be 40 seconds slower. In one day, it will be no less than half 96 minutes! ! !


The crystal oscillator frequency of this microcontroller is 11.0592mhz, which is the same as the crystal oscillator frequency of most clocks, so there should be no problem.


Consider the source code the day after tomorrow:


In the timer interrupt function:


void t0(void) interrupt 1 using 0 

  {

  tcount++;

    if(tcount==4000)

  {tcount=0;

    second++;

if(second==60)

  {second=0;

   minute++;

    if(minute==60)

{minute=0;

  hour++;


  if(hour==24)

   {hour=0;

   }

}

  }

P1=~P1;

  }

}

The number after interrupt is simply the code name of the interrupt service function.

0 represents external interrupt 0

1 represents the timer counter 0 interrupt

2 represents external interrupt 1

3 represents the timer counter 1 interrupt

4 represents serial port interrupt

So interrupt 1 means that after the timer counter generates an interrupt, it will be executed in this function.

 


The code is actually quite simple, every 4000 interrupts will occur for 1 second.


etc...


The interruption every 4000 times is 1 second, so the reason is probably here.


Try comparing tcount==3600 with the stopwatch and find that every minute is about 1 second faster.


Bingo! The problem does lie in the value of tcount. Change the value of tcount to 3700. Comparing it with the stopwatch, we find that there is no error in seconds, and the error should be in milliseconds.


void t0(void) interrupt 1 using 0 

  {

  tcount++;

    if(tcount==3700)

  {tcount=0;

    second++;

if(second==60)

  {second=0;

   minute++;

    if(minute==60)

{minute=0;

  hour++;


  if(hour==24)

   {hour=0;

   }

}

  }

P1=~P1;

  }

}

 


This solves the problem of clock accuracy.


Reference address:Solution to 51 microcontroller clock accuracy error

Previous article:Design of aircraft engine intelligent measurement and display system using AT89S51 microcontroller
Next article:Solution to 51 microcontroller clock accuracy error

Recommended ReadingLatest update time:2024-11-16 09:43

TINY4412 bare metal program, clock operation
In fact, the IROM code of Exynos 4412 has already set the PLL. We can print out the PLL register values ​​set by IROM through the serial port. These values ​​are printed out like this (excerpted from "Complete Manual of Embedded Linux System Development_Based on 4412__Volume 1" by Teacher Wei Dongshan): CLK_SRC_CPU
[Microcontroller]
TINY4412 bare metal program, clock operation
Solution to the internal clock switching problem of STM8S
/* ********************************************************************************************************************************** * Name: Solution to the internal clock switching problem of STM8S * Author  : MingMing * Release : 2013/12/28 * Update  : 2013/12/29 * Email: clint.wang@foxmail.com ********************
[Microcontroller]
Solution to the internal clock switching problem of STM8S
How to use external clock in ATmega16 simulation in proteus
When simulating atmega16 in proteus, there are always problems when selecting external clock. The red English in the above picture says that the CKSEL fuse bit is specified as an external oscillator, but the system external clock frequency is not determined. After modifying the clock in this way, you can use the e
[Microcontroller]
How to use external clock in ATmega16 simulation in proteus
Freescale MCU clock initialization and configuration
The system clock can select internal or external clock, the clock frequency can be multiplied, the bus clock is 1/2 of the system clock, the timer clock can select the clock source, the clock source can select the bus clock or external clock, etc. (the bus clock is selected in my program), and it can be divided. If
[Microcontroller]
MCU 4-bit digital clock simulation program (timer mode 2, key press)
To learn the dynamic digital tube display of single-chip microcomputer, digital clock is the best case. This simulation case has the following features.         1. Use timer 0 and mode 2 to reduce the error of timer; 2. The time adjustment and minute adjustment buttons have the function of continuous addition, which i
[Microcontroller]
MCU 4-bit digital clock simulation program (timer mode 2, key press)
Parsing of STM32 clock code and pointer jump of startup function
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; //HSE means using external clock, HSI means using internal clock   RCC_OscInitStruct.HSEState = RCC_HSE_ON; // Turn on the external clock   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;    RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; //Clock source s
[Microcontroller]
stm32f103 FreeRTOS tick clock configuration
The tick clock on stm32 is the basis for the operation of the operating system The tick clock tree of stm32 is as shown below. SysTick can choose HCLK (AHB clock) or HCLK/8 as the running clock The tick timer of stm32 is a 24-bit timer, and there are only four registers for its configuration: STK_CTRL is the contro
[Microcontroller]
U-Boot transplantation on FL2440 (1)----Modify the system clock
1 U-boot source code structure       The picture comes from Wei Dongshan's "Complete Manual of Embedded Linux Application Development"        U-boot download address: ftp://ftp.denx.de/pub/u-boot/       Cross-compilation tool chain 3.3.2 2 U-boot transplantation     1. Create a new directory and file for the fl2
[Microcontroller]
U-Boot transplantation on FL2440 (1)----Modify the system clock
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号