AT89C5 1 MCU, 30-second countdown timer, LED
There are many ways to design and make a 30-second countdown timer. The 30-second countdown timer introduced in this paper takes AT89C5 1 as the control unit, uses two digital tubes to display the time, and uses three buttons to control the timer's timing start, reset and pause respectively. The initial state of the countdown timer shows "30". When the referee presses the timer button, the countdown starts after 30 seconds. When the timing time is reduced to 0, the timer will give an audible and visual alarm to remind the referee that the timing time is up.
First, the circuit design
The circuit of 30-second countdown timer is mainly composed of power supply circuit, minimum system of single chip microcomputer, key input, display drive circuit and alarm circuit. The control circuit of the 30-second countdown timer is shown in figure 1.
Figure 1 30 second countdown timer circuit schematic diagram
1, press the key to enter
The 30-second countdown timer uses three buttons to complete the functions of starting counting, resetting, pausing/continuing counting, etc.
(1)K 1 key: start button (P3.2).
Press the K 1 key, the counter will start counting down, and the number displayed by the digital tube will start counting down every second from 30. When it reaches zero, the alarm circuit will send out an audible and visual alarm signal. When the counter is in the suspended state, pressing the K 1 key will return to the timing state.
(2)K2 key: reset button (P3.3).
Press the K2 key, no matter what state the counter works in, it will be reset to the preset value of "30" immediately, and press the K2 key to cancel the alarm in the alarm state.
(3)K3 key: pause/timing switch button (P3.4).
When the counter is in the timing state, press this button to pause the timer, and the number displayed by the digital tube will remain unchanged; When the counter is in the pause state, press this key and the counter will return to the timing state; The key is invalid in the initial state.
2. Display drive circuit
The "30-second countdown timer" uses two * * * digital tubes to display the time, and the display mode of the digital tubes is dynamic display. In the display driving circuit, the segment pin of the digital tube is connected to the P 1 port of the single chip microcomputer through a 470 ohm resistor, and the two chip selection pins are connected to the positive 5V power supply through a 90 12, which is controlled by P3.0 and P3. 1.
4. Alarm circuit
When the timing time is reduced to 0, when the display digital tube shows "00", the LED D 1 is controlled by P3.5 to give an optical alarm, and the buzzer is controlled by P3.7 to give an acoustic alarm.
Second, the software programming ideas
1, global variable
The action flow of "30-second countdown timer" is mainly controlled by three global variables. One is the bit variable "act". When "act" is "1", the countdown starts and stops when it is "0". The initial value of "act" is "0", which can be set to "1" or cleared by button operation. The second global variable is the char variable "time", which stores the countdown time. When the countdown time is 0, an audible and visual alarm will be given. The initial value of the variable "time" is 30. When "act" is 1, the timer interrupt service routine will be kept at 0 every 1 minus 1, when it is reduced to 0. Press the Reset key to reset the Time to 30. The third global variable is the int variable "t", which records the number of times the response timing was interrupted by 0. According to the initialization definition, Timer0 works in 1 mode and sends an interrupt request every1ms. The control program only turns on Timer0 interrupt, so there will be no more advanced interrupt than Timer0 interrupt, so every request will be answered immediately. After the response, add 1 to the global variable "t" in the interrupt service program, and record the response interruption times. 1000 times, every response is 1 second. The initial value of the variable "t" is 0, and the interrupt service program is added with 1. When "t" is 2000, it is cleared by the interrupt service program. In the key driver, when the start key, the reset key and the pause/start key are pressed, the "t" is cleared to 0, so as to start counting from 0 ms
2. Control flow
The main program is mainly used to detect the global variable "time" and send out "sound and light alarm" when "time" is 0. Key driver, display driver and "time" operation are all carried out in timer 0 interrupt service program. The control flow is shown in Figure 2.
Fig. 2 control flow chart
Third, the software program design
1, digital tube driver
The two digital tubes of the timer dynamically display the timing time "time" (global variable), LED 1 displays ten digits "time" and LED2 displays one digit "time".
(1) defines the segment code data port and chip selection signal.
According to the actual circuit, the data port of the segment code defined in C5 1 is P 1, and the two chip selection signals are P3.0 and P3. 1 respectively. Defined as follows:
# Define segment P 1
sbit wei 1=p3^0;
sbit wei2=p3^ 1;
(2) define the font code
The LED displays the numbers 0~9 and the completely destroyed font code table in the array zixing[]. Font code is a fixed table, and adding the keyword "code" when defining it means that this table is stored in the program memory.
Unsigned character code zixing[]=
{
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff
};
(3) Define the display variables of LED 1 and LED2.
In order to increase the portability of the driver, the author defines display variables for LED 1 and LED2. The display variable is the external interface of this driver, and the external program can change the value displayed by the digital tube by changing the value of the display variable. Defined as follows:
Unsigned character led_str[2]={ 10,10};
The Led_str[0] directly corresponds to LED 1, and the LED _ STR [1] directly corresponds to LED2. In this project, the calculation of global variable time is divided into led_str[0] and led_str[ 1] by a special subroutine.
void js()
{
led _ str[ 1]= time/ 10% 10;
led _ str[0]= time % 10;
}
(4) Digital tube driver
The nixie tube driver "void Chu Shi (char i)" is called and executed in the timed interrupt service program. According to the definition of initialization program, the timed interrupt service program is executed once every1ms. In the timed interrupt service program, the global variable "t" is used to record the times of entering the service program, and when it reaches 2000 times, "t" is cleared by the timed interrupt service program.
The parameter "char i" of the digital tube driver is used to determine whether LED 1 or LED2 is currently lit. When the parameter is "0", LED 1 is on, and when the parameter is "1", LED2 is on. If we want to light LED 1 when we enter the timed interrupt service program for even times, and light LED2 when we enter the timed interrupt service program for odd times, we can use the program call statement "Chu Shi (t% 2);" Easy to implement.
After entering the digital tube driver, first call the subfunction js () to calculate the current led_str[0] and led_str[ 1]. Next, put out both digital tubes to prevent afterglow. Finally, light the digital tube that needs to be lit and send out the font code. The driver code is as follows:
Invalid Chu Shi (char i)
{
js(); //Calculate display variables
Segment = 0xff// Remove afterglow
Wei 1 = I; wei2=! Me; //Confirm the movie selection
Segment = Zixing [led _ str [I]]; //Send font code
}
2. Key driving factors
Key driver is divided into two parts: key recognition and key function execution. The key function can be executed when the key is pressed or after the key is lifted, and the key function is designed to be executed after the key is lifted.
(1) defines the critical I/O address.
According to the actual circuit, three keys (start key, reset key and pause/start key) are respectively connected to P3, P3.3 and P3.4 pins of P3 port. In order to obtain the key value conveniently, P3 port is also defined as "iokey", which can be defined in the program as follows:
# Define Joki P3
sbit key 1=p3^2;
sbit key2=p3^3;
sbit key3=p3^4;
(2) Key driving process
The general process of key recognition is as follows: I/O write "1"→ judge whether a key is pressed → delay shaking → determine the key value → wait for the key to be lifted → execute the key function. Two static variables "ts" and "kv" are defined in the key driver, which are used to delay debounce and store key values respectively.
(3) Delayed debounce
The static variable "ts" is used to delay debounce. In the timer interrupt service program, the key driver is executed every 1 millisecond. When a key "ts" is pressed and 1 is added, if a key is not pressed, it will be cleared. If the key is pressed continuously for 20 milliseconds, it is detected that the key is pressed when the key driver is executed continuously for 20 times. At this time, the static variable "ts" accumulates to 20, and it can be confirmed that the key is effectively pressed.
In order to prevent the cumulative overflow of "ts" when the key is kept pressed, the value of "ts" can be kept at 20 or some value greater than 20, such as 2 1.
(4) take the key value
After confirming that a key is pressed, you can get the key value by reading the I/O port status of the key. In order to read the pin states of P3.2, P3.3 and P3.4, and shield the influence of other pins of P3 port, the read value can be sent to the static variable "kv" bit by bit or as high as1100011b (0xE3).
The static variable "kv" stores the key value of the key, and the value of kv is 0 after no key is pressed or raised. Kv =11110111b (0xfb) when the start key is pressed, and kv =/kloc-when the reset key is pressed. When the pause/start key key3 is pressed, kV =11kloc-0/111b (0xef).
(5) Execute the key function
When the key is driven for the first time after being lifted, the static variable "kv" stores the key value finally obtained when the key is pressed, and the key value is used as a parameter to call the key actuator "Actkey (kV);" Key functions can be performed. After the call, the kv value will be cleared immediately to ensure that the key function is performed once. The driver code is as follows:
Void key ()
{
Static unsigned character kv = 0;;
Static unsigned character ts = 0;;
key 1 = 1; key 2 = 1; key 3 = 1;
If (! (key 1 & amp; Key 2&key 3))
{
ts++;
if(ts & gt; = 20)ts = 20; //A key was pressed.
If (ts==20)
Kv = iokey | 0xe3// Get the key value
}
other
{//No key was pressed or raised.
act key(kv);
ts = 0;
kv = 0;
}
}
The function actkey(kv) is used to perform corresponding operations according to the key value "kv". When "kv" is equal to 0xFB, it means that the start key key 1 is pressed, and the function actkey(kv) assigns the global variable act to "1". When "kv" is equal to 0xF7, it means that the reset key key2 is pressed, and the function actkey(kv) resets the global variable "time" to "30". When "kv" equals 0xEF, it means that the pause/start key is pressed, and the function actkey(kv) inverts the global variable act. Every time the button is pressed, the global variable "t" will be cleared, so that whenever the timer is reset or started, the number of times to enter the timing interrupt will be counted from 0, otherwise the 1 second timing will be inaccurate. The program code is as follows:
Void actkey (unsigned character k)
{
Switch (k)
{
case 0x FB:act = 1; t = 0; Break;
Case 0xf7: Time = 30; t = 0; Break;
case 0x ef:act = ~ act; t = 0; Break;
}
}
Four. Concluding remarks
In this paper, two LED digital tubes and three independent buttons are encapsulated by object-oriented programming idea in the programming process. When calling its driver in the timed interrupt service program, the programmer only needs to operate its interface: array "led_str[2]" and function "actkey(unsigned char k)", and the function can be changed without directly programming the hardware, thus enhancing the universality and portability of the software.