Two different period countdown timers that can show or hide seconds (based on 8051)

Publisher:数字小巨人Latest update time:2021-08-04 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Functions implemented by the program:


1. After the experiment box is powered on, the two timers start timing, but no display is given.


2. If the "display seconds" command is sent to 8051, a specified countdown timer will display the seconds.


3. If the "hide seconds" command is sent to 8051, the specified countdown timer will be hidden for the specified number of seconds.


#include <STC89C5xRC.H>

 

#include

 

unsigned char code DIG_CODE[10] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f}; //The corresponding digital tube displays 0~9

 

char buf[30]; //Store the received command

 

int tcount = 0; //Record the number of 5ms intervals passed

 

int sec1 = 15, sec2 = 25; // countdown initial value

 

int sec1_ = 0, sec2_ = 0; //Real-time changes

 

int en1 = 0, en2 = 0; //Two timer seconds display flags, default is not displayed

 

void T0_INT() interrupt 1 //timer0 interrupt, triggered every 5ms

{

TR0 = 0; // turn off timer0

TH0 = 0xEC;

TL0 = 0x78; //65536 - 5000 = 60536

if(tcount % 4 == 0)

{

if(en1 == 1)

{

//Display the single digit (first countdown timer)

P2 = 0; // P2 = 0 -> (P24, P23, P22) = (0, 0, 0) -> the first number from the right lights up

P0 = DIG_CODE[sec1_ % 10];

}

tcount++;

}

else if(tcount % 4 == 1)

{

if(en1 == 1)

{

//Display the ten digits (first countdown timer)

P2 = 1 << 2; //P2 = 0000 0100 -> (P24, P23, P22) = (0, 0, 1) -> the second digit from the right lights up

P0 = DIG_CODE[sec1_ / 10];

}

tcount++;

}

else if(tcount % 4 == 2)

{

if(en2 == 1)

{

//Display the single digit (second countdown timer)

P2 = 100 << 2; //P2 = 0001 0000 -> (P24, P23, P22) = (1, 0, 0) -> the fifth number from the right lights up

P0 = DIG_CODE[sec2_ % 10];

}

tcount++;

}

else if(tcount % 4 == 3)

{

if(en2 == 1)

{

//Display the tens digit (second countdown timer)

P2 = 101 << 2; //P2 = 0001 0100 -> (P24, P23, P22) = (1, 0, 1) -> the sixth digit from the right lights up

P0 = DIG_CODE[sec2_ / 10];

}

tcount++;

}

if(tcount == 200)

{

tcount = 0;

if(-- sec1_ == -1)

{

sec1_ = sec1;

}

if(-- sec2_ == -1)

{

sec2_ = sec2;

}

}

TR0 = 1; //Restart timer0

}

 

int main()

{

int i = 0;

P2 = 111 << 2; //Select the first number from the left

P0 = 0; // Digital tube off

TMOD = 0x21;

SCON = 0x50; //Set serial communication format

TH0 = 0xEC;

TL0 = 0x78; //65536 - 5000 = 60536 = EC78H

TH1 = 0xE6;

TL1 = 0xE6;

IE = 0x82;

TR0 = 1; // turn on timer0

TR1 = 1; // turn on timer1

while(1)

{

if(RI == 1) //If data is received

{

RI = 0;

buf[i++] = SBUF; //Receive data from the serial port

buf[i] = ''; //Manually add the end of string mark

if(i == 29) //Prevent array access out of bounds

{

i = 0;

}

if(buf[i - 1] == 'E') // reach the end

{

i = 0;

if(strcmp(buf, "Show countdown 1st.E") == 0) //Show countdown 1st.E command

{

en1 = 1;

}

else if(strcmp(buf, "Hide countdown 1st.E") == 0) //Hide countdown 1st.E command

{

en1 = 0;

}

else if(strcmp(buf, "Show countdown 2nd.E") == 0) //Show countdown 2nd.E command

{

en2 = 1;

}

else if(strcmp(buf, "Hide countdown 2nd.E") == 0) //Hide countdown 2nd.E command

{

en2 = 0;

}

}

}

}

return 0;

}


Reference address:Two different period countdown timers that can show or hide seconds (based on 8051)

Previous article:Dual countdown timer based on 8051 (Version 1.0)
Next article:8051 MCU implements a single countdown timer with modifiable initial value (and command to start and stop)

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号