51 MCU 1 second timing C language program (through debugging)

Publisher:rnm888Latest update time:2016-09-28 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
#include // The crystal oscillator is 12MHz,

#include
#include
#include

   /***************************
* Define hardware pin connections
****************************/
sbit p1_0=P1^0;             
 /*****************************
* Define global variables
***********************/
// Byte data intrCounter; // Number of timer interrupts
// Byte data beginNum; // Time to start countdown
 //Byte data counterBack; // Store the number of interrupts in it for later use
unsigned char intrCounter;
unsigned char beginNum; 
unsigned char counterBack;
void IT0_Init(void);
void Timer0_Overflow();
void GetCounter(void);
void main()
{
 p1_0=1;
 GetCounter() ;Change once every second;
 p1_0=!p1_0;;
}
/*********************************
* GetCounter
*
* Description: Get the preset countdown time
*******************************/
void GetCounter(void)
{
//beginNum=1;//When all switches are not turned, the countdown is 1 second, which is better than setting it to 0 seconds
intrCounter=20;//The number of interrupts corresponding to each second is 20 times
}
/********************************
* IT0_Init
* Description: Initialize the status of timer T0
********************************/
void IT0_Init(void)
{
TMOD=0x01;//Set T0 to work in mode 1
TH0=0x3C;
TL0=0xAF;//These two registers store the starting value of the counter. It is found that the sum of these two values ​​is exactly 50ms after overflow
ET0=1;//Enable T0 interrupt to overflow
EA=1;//Enable total interrupt
TF0=0;//Clear overflow bit
TR0=1;//Enable T0
}

/******************************
* Timer0_Overflow() interrupt 1
* Description: Interrupt overflow service routine, using interrupt mode 1, it is best not to add "using" to select the register group to avoid conflicts with the registers used by the system in the main program

*********************************/
void Timer0_Overflow() interrupt 1
{
static unsigned char second=20;//Use 20 interrupts to determine 1 second
TH0=0x3C;
TL0=0xAF;
second--;
intrCounter--;

if (second==0) //Operation every second
{
   
    second=20; //Reassign the counter per second
   }
   if (intrCounter==0)
   {
    TR0=0; //Close T0 counter
   }

Reference address:51 MCU 1 second timing C language program (through debugging)

Previous article:51 baud rate calculation formula
Next article:Timing calculation method of single chip microcomputer timer

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号