Double countdown timer based on 8051 (version 1.1)

Keywords: IE

Version 1.1 is more complete than version 1.0.

Functions of the program:

1. After the test box is powered on, the two countdown timers are not displayed and both are in the suspended state.

2. After sending "Show countdown 1st.E" to 8051, the first countdown shows the number of seconds (initially 0, then 15).

3. After sending "Show countdown 2nd.E" to 8051, the second countdown shows the number of seconds (initially 0, then counting down from 25).

4. After sending "Start countdown 1st.E" to 8051, the first countdown starts to count down.

5. After sending "Start countdown 2nd.E" to 8051, the second countdown starts to count down.

6. After sending "Hide countdown 1st.E" to 8051, the first countdown will hide seconds.

7. After sending "Hide countdown 2nd.E" to 8051, the second countdown will hide seconds.

8. After sending "Stop countdown 1st.E" to 8051, the first countdown will pause.

9. After sending "Stop countdown 2nd.E" to 8051, the second countdown will pause.

10. After sending "Show both.E" to 8051, two countdown timers display seconds.

11. After sending "Hide both.E" to 8051, two countdown timers hide seconds.

12. After sending "Start both.E" to 8051, two countdowns begin to count down.

13. After sending "Stop both.E" to 8051, two countdowns pause the countdown.

#include <STC89C5xRC.H>

#include <string.h>

unsigned char code DIG_CODE[10] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f};//Corresponding nixie tube display 0 ~ 9

char buf[30];//Store received commands

int tcount = 0;//Record the number of 5ms interval sections

int sec1 = 15, sec2 = 25;//Countdown initial

int sec1_ = 0, sec2_ = 0;//Real time change

int t1 = 0, t2 = 0;

char en1 = 0, en2 = 0;//Two timer seconds display flag, default is not displayed

char en1_ = 0, en2_ = 0;//Start stop flag of two timers, default to stop

void T0_INT() interrupt 1 //Timer 0 interrupt, triggered every 5ms
{
	TR0 = 0;//Close timer0
	TH0 = 0xEC;
	TL0 = 0x78;//65536 - 5000 = 60536
	if(tcount % 4 == 0)
	{
		if(en1_ == 1)//If countdown timer 1 starts
		{
			//Show digits (first countdown)
			P2 = 0;// P2 = 0 - >
			if(en1 == 1)//If countdown timer 1 shows seconds
			{
				P0 = DIG_CODE[sec1_ % 10];
			}
			else
			{
				P0 = 0;//Don't show numbers
			}
			t1 ++;
		}
		else//If countdown timer 1 is suspended
		{
			//Show digits (first countdown)
			P2 = 0;// P2 = 0 - >
			if(en1 == 1)//If countdown timer 1 shows seconds
			{
				P0 = DIG_CODE[sec1_ % 10];
			}
			else
			{
				P0 = 0;//Don't show numbers
			}
		}
		tcount ++;
	}
	else if(tcount % 4 == 1)
	{
		if(en1_ == 1)//If countdown timer 1 starts
		{
			//Display TENS (first countdown)
			P2 = 1 << 2;//P2 = 0000 0100 - >
			if(en1 == 1)//If countdown timer 1 shows seconds
			{
				P0 = DIG_CODE[sec1_ / 10];
			}
			else
			{
				P0 = 0;//Don't show numbers
			}
			t1 ++;
		}
		else//If countdown timer 1 is suspended
		{
			//Display TENS (first countdown)
			P2 = 1 << 2;//P2 = 0000 0100 - >
			if(en1 == 1)//If countdown timer 1 shows seconds
			{
				P0 = DIG_CODE[sec1_ / 10];
			}
			else
			{
				P0 = 0;//Don't show numbers
			}
		}
		tcount ++;
	}
	else if(tcount % 4 == 2)
	{
		if(en2_ == 1)//If countdown timer 2 starts
		{
			//Show digits (second countdown)
			P2 = 100 << 2;//P2 = 0001 0000 - > (p24, p23, P22) = (1, 0, 0) - > the fifth number on the right is on
			if(en2 == 1)//If countdown timer 2 shows seconds
			{
				P0 = DIG_CODE[sec2_ % 10];
			}
			else 
			{
				P0 = 0;//Don't show numbers
			}
			t2 ++;
		}
		else//If countdown timer 2 is suspended
		{
			//Show digits (second countdown)
			P2 = 100 << 2;//P2 = 0001 0000 - > (p24, p23, P22) = (1, 0, 0) - > the fifth number on the right is on
			if(en2 == 1)//If countdown timer 2 shows seconds
			{
				P0 = DIG_CODE[sec2_ % 10];
			}
			else 
			{
				P0 = 0;//Don't show numbers
			}
		}
		tcount ++;
	}
	else if(tcount % 4 == 3)
	{
		if(en2_ == 1)//If countdown timer 2 starts
		{
			//Display TENS (second countdown)
			P2 = 101 << 2;//P2 = 0001 0100 - > (p24, p23, P22) = (1, 0, 1) - > the sixth number on the right is on
			if(en2 == 1)//If countdown timer 2 shows seconds
			{
				P0 = DIG_CODE[sec2_ / 10];
			}
			else
			{
				P0 = 0;//Don't show numbers
			}
			t2 ++;
		}
		else//If countdown timer 2 is suspended
		{
			//Display TENS (second countdown)
			P2 = 101 << 2;//P2 = 0001 0100 - > (p24, p23, P22) = (1, 0, 1) - > the sixth number on the right is on
			if(en2 == 1)//If countdown timer 2 shows seconds
			{
				P0 = DIG_CODE[sec2_ / 10];
			}
			else
			{
				P0 = 0;//Don't show numbers
			}
		}
		tcount ++;
	}
	if(tcount == 200)
	{
	 	tcount = 0;
	}
	if(t1 == 100)
	{
		t1 = 0;
		if(-- sec1_ == -1)
		{
			sec1_ = sec1;
		}
	}
	if(t2 == 100)
	{
		t2 = 0;
		if(-- sec2_ == -1)
		{
			sec2_ = sec2;
		}
	}
	TR0 = 1;//Reopen timer0
}

int main()
{
	int i = 0;
	P2 = 111 << 2;
	P0 = 0;//Digital tube out
	TMOD = 0x21;
	SCON = 0x50;//Format serial communication
	TH0 = 0xEC;
	TL0 = 0x78;//65536 - 5000 = 60536 = EC78H
	TH1 = 0xE6;
	TL1 = 0xE6;
	IE = 0x82;
	TR0 = 1;//Open timer0
	TR1 = 1;//Open timer1
	while(1)
	{
		if(RI == 1)//If data is received
		{
			RI = 0;
			buf[i ++] = SBUF;//Receive data from serial port
			buf[i] = '\0';//Manually add string end flag
			if(i == 29)//Prevent array access from exceeding bounds
			{
				i = 0;
			}
			if(buf[i - 1] == 'E')//Reaching the end
			{
				i = 0;
				if(strcmp(buf, "Show countdown 1st.E") == 0)//Show countdown 1st.E command
				{
					en1 = 1;//Display the seconds of countdown timer 1
				}
				else if(strcmp(buf, "Hide countdown 1st.E") == 0)//Hide countdown 1st.E command
				{
					en1 = 0;//Seconds to hide countdown 1
				}
				else if(strcmp(buf, "Show countdown 2nd.E") == 0)//Show countdown 2nd.E command
				{
					en2 = 1;//Display the seconds of countdown timer 2
				}
				else if(strcmp(buf, "Hide countdown 2nd.E") == 0)//Hide countdown 2nd.E command
				{
					en2 = 0;//Seconds to hide countdown 2
				}
				else if(strcmp(buf, "Start countdown 1st.E") == 0)//Start countdown 1st.E command
				{
					en1_ = 1;//Countdown timer 1 starts countdown
				}
				else if(strcmp(buf, "Stop countdown 1st.E") == 0)//Stop countdown 1st.E command
				{
					en1_ = 0;//Countdown timer 1 pause countdown
				}
				else if(strcmp(buf, "Start countdown 2nd.E") == 0)//Start countdown 2nd.E command
				{
					en2_ = 1;//Countdown timer 2 starts countdown
				}
				else if(strcmp(buf, "Stop countdown 2nd.E") == 0)//Stop countdown 2nd.E command
				{
					en2_ = 0;//Countdown 2 pause countdown
				}
				else if(strcmp(buf, "Start both.E") == 0)//Start both.E command
				{
					en1_ = 1;//Countdown timer 1 starts countdown
					en2_ = 1;//Countdown timer 2 starts countdown
				}
				else if(strcmp(buf, "Stop both.E") == 0)//Stop both.E command
				{
					en1_ = 0;//Countdown timer 1 pause countdown
					en2_ = 0;//Countdown 2 pause countdown
				}
				else if(strcmp(buf, "Show both.E") == 0)//Show both.E command
				{
					en1 = 1;//Display the seconds of countdown timer 1
					en2 = 1;//Display the seconds of countdown timer 2
				}
				else if(strcmp(buf, "Hide both.E") == 0)//Hide both.E command
				{
					en1 = 0;//Seconds to hide countdown 1
					en2 = 0;//Seconds to hide countdown 2
				}
				else if(buf[0] == 'S' && buf[1] == 'e' && buf[2] == 't' && buf[3] == ' ' && buf[4] == 'i' && buf[5] == 'n'
						&& buf[6] == 'i' && buf[7] == 't' && buf[8] == ' ' && buf[9] == 'v' && buf[10] == 'a' && buf[11] == 'l'
						&& buf[12] == 'u' && buf[13] == 'e' && buf[14] == ' ' && buf[15] == '1' && buf[16] == 's' && buf[17] == 't'
						&& buf[18] == ' ' && buf[19] == 'a' && buf[20] == 's' && buf[21] == ' ' && buf[24] == '.')//Set init value 1st as **.E command
				{
					sec1 = (buf[22] - 48) * 10 + (buf[23] - 48);//Modify the initial value of countdown timer 1
				}
				else if(buf[0] == 'S' && buf[1] == 'e' && buf[2] == 't' && buf[3] == ' ' && buf[4] == 'i' && buf[5] == 'n'
						&& buf[6] == 'i' && buf[7] == 't' && buf[8] == ' ' && buf[9] == 'v' && buf[10] == 'a' && buf[11] == 'l'
						&& buf[12] == 'u' && buf[13] == 'e' && buf[14] == ' ' && buf[15] == '2' && buf[16] == 'n' && buf[17] == 'd'
						&& buf[18] == ' ' && buf[19] == 'a' && buf[20] == 's' && buf[21] == ' ' && buf[24] == '.')//Set init value 2nd as **.E
				{
					sec2 = (buf[22] - 48) * 10 + (buf[23] - 48);//Modify the initial value of countdown timer 2
				}
			}
		}
	}
	return 0;
}

 

Posted by allinurl on Wed, 23 Oct 2019 10:34:16 -0700