Using timer 0 to write a stopwatch (comments)

Publisher:EtherealMelodyLatest update time:2022-08-11 Source: csdn Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The stopwatch time is set by the timer, and the set time is displayed through the common cathode digital tube.


#include

 

#define uchar unsigned char

#define uint unsigned int

uchar code table [] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40}; // Common cathode segment code table

 

uchar code table_SMG[] = {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07}; //bit selection

//------------------------Variable area------------------------

 

uchar count ; // count variable

uchar Time_T,Time_f,Time_s; //Contains time variables

//------------------------Function area------------------------

 

void Delay(uint t);//delay function

void Init_ET_0(); //Timer initialization function 50ms

void Display_SMG(uchar pos , uchar date); //Single digital tube display function (bit selection, segment selection)

void Display_Time(); //Time display function


void Delay(uint t) //delay function

{

while(t--);

}

 

void Init_ET_0() //Timer initialization function 50ms

{

TMOD = 0x01; //Timer 0 working mode 1

EA = 1; // Enable general interrupt

ET0 = 1; //Turn on the timer switch

TR0 = 1; //Start the timer

TH0 = (65536 - 50000)/256; //Assign initial value to the high eight bits

TL0 = (65536 - 50000)%256; //Assign initial value to the lower eight bits

}

 

void Servoic_ET_0() interrupt 1 //Timer service function

{

TH0 = (65536 - 50000)/256; //Reinstall the upper eight bits

TL0 = (65536 - 50000)%256; //Reinstall the lower eight bits

count++; //add once every 50ms

if(count == 20) // add 20 times to 1 second

{

Time_s++; //Increase once every 1 second

count = 0; //Release the variable value to make it timely for 50ms

if(Time_s == 60) //When it reaches 60S, add 1

{

Time_f++; // add 1 to the minute

Time_s = 0; //Set s to zero

if(Time_f == 60) //When it reaches 60 minutes, add 1

{

Time_T++; // add 1 to the time

Time_f = 0; //set time to zero

if(Time_T == 24) //When it reaches 24, set the time variable to zero

{

Time_T = 0; //Set the time variable to zero

}

}

}

}

}

 

void Display_SMG(uchar pos , uchar date) //Single digital tube display function (bit selection, segment selection)

{

P2 = table_SMG [pos]; //Position selection

P1 = date ;//segment selection

Delay(100); // Delay for a short time for dynamic display

}

 

void Display_Time() //Time display function

{

Display_SMG(7 , table [Time_s%10]); //Display the seconds

Display_SMG(6 , table [Time_s/10]); //Display the tens of seconds (recursive below)

Display_SMG(5 , table [10]); // separator

Display_SMG(4 , table [Time_f%10]);

Display_SMG(3 , table [Time_f/10]);

Display_SMG(2, table[10]);//separator

Display_SMG(1 , table [Time_T%10]);

Display_SMG(0 , table [Time_T/10]);

}

 

 

void main()

{

Init_ET_0(); //Timer 0 initialization

while(1)

{

Display_Time(); //Continuously refresh the display time

}

}


The simulation diagram is as follows

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5qSw5ZWK5qSw5ZOm,size_20,color_FFFFFF,t_70,g_se,x_16

Reference address:Using timer 0 to write a stopwatch (comments)

Previous article:Generate square wave using timer and adjust square wave frequency through interrupt
Next article:Using the timer to make the microcontroller output square waves

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号