Design of simple time alarm circuit based on single chip microcomputer

Publisher:剑戟辉煌Latest update time:2012-09-11 Source: 21ic Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Design a simple timer alarm controlled by a single-chip microcomputer. It is required to count down according to the set initial value (1-59 seconds). When the time reaches 0, the digital tube flashes "00" (flashing at 1Hz). The key functions are as follows:
(1) Set key: In the countdown mode, press this key to stop the countdown and enter the setting state; if it is already in the setting state, this key is invalid.
(2) Increase key: In the setting state, each time the increment key is pressed, the initial value increases by 1.
(3) Decrement key: In the setting state, each time the decrement key is pressed, the initial value decreases by 1.
(4) Confirm key: In the setting state, press this key, the single-chip microcomputer counts down according to the new initial value and displays the countdown number. If it is already in the timing state, this key is invalid.
3.1.2 Module 1: System design
(1) Task analysis and overall design ideas
According to the requirements of the topic, the following functions need to be implemented.
Timing function: To realize the timing function, you need to use a timer to count. By setting the initial value of the timer to control the time interval of the overflow interrupt, a variable is used to record the number of timer overflows to achieve the function of timing in 1 second. Then, when the timing reaches 1 second, the countdown counter is reduced by 1. When the countdown counter reaches 0, another flag variable is triggered to enter the flashing state.
Display function: To display the countdown number, the number should be divided into "tens" and "units" by dynamic scanning. If it is in the flashing state, there is no need for dynamic scanning display. You only need to control the bit control line of the common cathode digital tube to realize the extinguishing and lighting of the digital tube.
Switching between keyboard scanning and running mode: After initializing some variables and registers, the main program needs to continuously read the status of the keyboard and dynamically scan the digital tube to display the corresponding numbers. Switching between setting state and timing state is realized according to the key value of the keyboard.
(2) MCU model and required peripheral device models, MCU hardware circuit schematic diagram
MCS-51 series AT89S51 MCU is selected as the microcontroller, and two four-way common cathode digital tubes are selected to form an 8-bit display module. Due to the limited driving capability of the AT89S51 MCU, two 74HC244s are used to drive the bus. One 74HC244 completes the control and drive of the bit control line, and the other 74HC244 completes the 7-segment code output of the digital tube. A 100 ohm resistor is connected in series at each output port to limit the current of the 7-segment digital tube.
Due to the small number of keyboards, independent keys are selected and connected to the P1 port as four key inputs. When no key is pressed, P1.0-P1.3 is high level, and when a key is pressed, the corresponding pins of P1.0-P1.3 are low level. The circuit schematic diagram is shown in Figure 3-1.


Figure 3-1 Schematic diagram of the timing alarm circuit


(3) Program design ideas, MCU resource allocation and program flow
① MCU resource allocation
The P3 port of the MCU is used as the key input, and an independent key is connected to P3.0-P3.3 to form four function keys.
In the timing function, three variables are required to temporarily store the number of timer overflows (T1_cnt), the initial value of the countdown (init_val) and the current countdown seconds (cnt_val). In the
key scanning function, two variables are required, one variable (key_val_new) is used to store the key value of the current scan (if no key is pressed, it is 255), and the other variable (key_val_old) is used to store the key value of the last scan. Only when the values ​​of these two variables are different can it be said that a new key is pressed or popped up, and the new key value is assigned to the key_val_old variable.
In the display function, a set of arrays (code type) needs to be defined, and the value is the 7-segment code of the digital tube corresponding to the numbers 0-9. It is also necessary to define a variable (show_val) to temporarily store the data to be displayed for dynamic scanning display.
In the whole program, a state variable (state_val) is defined to store the current state of the microcontroller.
② Program design ideas
In view of the requirements of the topic, there are three working modes: initial value setting mode, countdown mode, and flashing mode when the time reaches 0. When the variable state_val is 0, it is in countdown mode. When the variable state_val is 1, it is in initial value setting mode. When the variable state_val is 2, it is in flashing mode. The switching of these states depends on which key is pressed and whether the time reaches 0. The state switching diagram is shown in Figure 3-2


Figure 3-2 Status switching


After the microcontroller is reset, it is in countdown mode by default. The timer is started. The timer overflows every 250us. The timer is counted according to the number of timer overflows. When it reaches 1 second, the time counter is reduced by 1. When the "set key" is pressed, the variable state_val changes from 0 to 1, switching to the set mode. The "increment key" and "decrement key" can be used to modify the initial value of the timing. When the "confirm key" is pressed, it returns to the timing mode and starts counting down with the new initial value. When the countdown reaches 0, the variable state_val changes from 1 to 2 and is in a flashing state. In this state, it switches to the timing and setting states respectively according to the key situation.
③Program flow
The main program first needs to initialize the timer parameters and some variables, and then enter a loop structure. In the loop, only two things are always done, one is the keyboard scan, and the other is the dynamic scanning of the digital tube.
After scanning the keyboard, it is based on whether the result of the previous key press is the same as the current key value. If different, it means that a key is pressed or popped up, and the previous key value is updated with the current key value. This design is to prevent a key from being repeatedly judged as a new key press when it is pressed for a long time, so that the currently pressed key is counted as a new key press only when it is released.
Change the value of the variable (state_val) or the initial value of the countdown when setting the state according to the value of the key. The complete main program diagram is shown in Figure 3-3.


Figure 3-3 Flowchart of the main program


In the timer parameters, select the 8-bit automatic loading mode of timer T1, and generate an overflow interrupt every 250us. The interrupt service program is shown in Figure 3-4.


Interrupt service routine flow chart

(4) Software and hardware debugging plan
Software debugging plan: In WeiFu software, in "File New File", create a new C language source program file and write the corresponding program. In the "File New Project" menu, create a new project and include the C language source program file in the project file.
In the "Project Compile" menu, compile the C source file and check for syntax errors and logical errors. After successful compilation, target files with "*.hex" and "*.bin" suffixes are generated.
Hardware debugging plan: In the design platform, connect P3.0-P3.3 of the microcontroller to the corresponding bits of the stand-alone keyboard through plug wires.
After compiling the program file into a target file in WeiFu, run the MCU download program, select the corresponding flash data file, click the "Program" button, and download the program file to the Flash of the microcontroller.
Then, power on and restart the microcontroller to check whether the written program meets the requirements of the question and whether it fully and completely completes the content of the question.
[page]

3.1.3 Programming (C language source program for reference only)
//Crystal oscillator: 11.0592M T1-250 microseconds Buttons P10 P11 P12 P13
/*Variable definition:
show_val: Displayed value 0-59
init_val: Initial value
state_val: State value 0-Counting state; 1-Setting state; 2-Blinking state
shan_val:
key_val1: Value of four buttons 255-No key; 1-Setting key 2-Increase key 3-Decrease key 4-Confirm key
T1_cnt: Timer count overflow number
cnt_val: Countdown value
led_seg_code: Digital tube 7-segment code
*/
#include "reg51.h" //Include file
sbit P1_0=P1^0; //Setting key
sbit P1_1=P1^1; //Increase key
sbit P1_2=P1^2; //Decrease key
sbit P1_3=P1^3; //Confirm
keyunsigned char data shan_val; //On/off status of LED when
flashingunsigned char data cnt_val; //Save the current value of
countdownunsigned int data T1_cnt; //Save the number of timer overflowsunsigned
char data key_val_new,key_val_old;//Store the currently scanned key and the key value pressed last timeunsigned
char data state_val; //State valueunsigned
char data show_val; //Store the number to be displayed on the digital
tubeunsigned char data init_val; //Temporarily store the initial value of
countdownchar code led_seg_code[10]={0x3f,0x06,0x05b,0x04f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
//----------Delay--------------
void delay(unsigned int i) //Approximately delay i*2 microseconds
{ while(--i);}
//-----------Key scan-------------
unsigned char scan_key()
{ unsigned char i;
i=P1&0x0f;
delay(100); //Delay, de-jitterif
(i==(P1&0x0f))
{ if (P1_0==0)
{ i=1; }
else
{ if (P1_1==0)
{ i=2;}
else
{ if (P1_2==0)
{ i=3;}
else
{ if (P1_3==0)
{ i=4;} }
} } }
else
{ i=255; }
return i;
}
//---------Digital tube display---------------
void led_show(unsigned int v)
{
unsigned char i;
if (state_val!=2) //Dynamic scan
{i=v%10; //Get the unit digit of the number to be displayed
P0=led_seg_code[i]; //Convert to 7-segment code
P2=0xfe; //Display the ones digit
delay(15); //Delay
i=v%100/10; //Get the tens digit
P0=led_seg_code[i]; //Convert to 7-segment code
P2=0xfd; //Display the tens digit
delay(5); //Delay
}
else
{ P0=led_seg_code[0]; //Flashing state
if (shan_val)
{ P2=0xff; } //Turn off the digital tube
else
{ P2=0xfc; } //Turn on the digital tube
}
}
//----------Timer T1 interrupt service program---------------
void timer1() interrupt 3 //T1 interrupt, 250us interrupt once
{ T1_cnt++;
switch (state_val)
{ case 0:
if(T1_cnt>3999) //If the count>3999, time 1s
{ T1_cnt=0;
if(cnt_val!=0)
{ cnt_val--;}
else
{state_val=2;} //When the timing count reaches 0, switch the state
show_val=cnt_val;
}
break;
case 2:
if(T1_cnt>1999) //If the count>1999, time 0.5s
{ T1_cnt=0; shan_val=!shan_val; } //Blinking state
break;
}
}
//---------Main program----------------
main()
{init_val=59; //Initialize variables
cnt_val=init_val;
show_val=cnt_val;
state_val=0;
key_val_old=255;
T1_cnt=0;
shan_val=0; //Initialize 51's register
TMOD=0x20; //Use T1 to time 8-bit automatic loading timing mode
TH1=0x19; //Overflow once in 250 microseconds; 250=(256-x)*12/11.0592 -> x= 230.4
TL1=0x19;
EA=1; //Open the general interrupt enable
ET1=1; //Open the interrupt enable
TR1=1; //Open the timer T1
while(1)
{ key_val_new=scan_key(); // 255 means no key is pressed
if (key_val_new!=key_val_old)
{ // Only when the key value currently scanned is different from the last scanned
key_val_old=key_val_new;
switch (key_val_new)
{ case 1: //Set key
state_val=1; //In the setting state
TR1=1; //Stop timing
show_val=init_val; //Show the original countdown initial value
break;
case 2: if(state_val==1) //The increment key is only useful in the setting state
{ if (init_val>0) //Change the original countdown initial value
{init_val--; }
else
{init_val=59;}
show_val=init_val;//Show the changed countdown initial value
}
break;
case 3: if(state_val==1) //The minus key is only useful in the setting state
{ if (init_val<59) //Change the original countdown initial value
{init_val++; }
else
{init_val=0;}
show_val=init_val; //Display the changed count initial value
}
break;
case 4: if(state_val!=0) //If it is already in counting mode, the confirmation key does not work
{ cnt_val=init_val; //Assign the initial value to the counting variable
show_val=cnt_val; //Display the number of the counting variable
TR1=1; //Start timer T1
state_val=0; //Switch the state to counting mode
}
break;
}
}
led_show(show_val); //Dynamic scanning
}
}
Reference address:Design of simple time alarm circuit based on single chip microcomputer

Previous article:Introduction to Single Chip Microcomputer C8051F020 and Its Application in Instruments and Meters
Next article:Experience summary of single chip microcomputer hardware design

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号