①When the machine is turned on, it is in running mode and displays the time normally.
②There are 3 buttons in total, namely mode button, function button 1 and function button 2. Press the mode button to switch between the 4 modes of "running time/time adjustment/date display and adjustment/alarm display and adjustment".
③In running mode, press function button 1/function button 2 to stop the alarm.
④In time adjustment mode, function button 1 adjusts the time up and function button 2 adjusts the time down.
The simulation schematic is as follows
The microcontroller source program is as follows:
#include #define uchar unsigned char uchar code tab[12]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xbf}; //The common anode digital tube displays the field code of "0~9", "off" and "-" //uchar code tab[]={0x3f,0x06,05b,0x4f,0x66,0x6d,07d,0x07,0x7f,0x6f,0x00,0x40}; //Common cathode digital tube uchar code wetab[8]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; //bit selection uchar data dis[8]={0x00,0x00,0x0b,0x00,0x00,0x0b,0x00,0x00}; //Display data buffer, hour ten, unit digit, minute ten, unit digit, second ten, unit digit are initially 0, 0x0b is "-" code uchar data timedat[3]={0x00,0x00,0x00}; //Count values for seconds, minutes, and hours respectively (record the number of K1 and K2) uchar data ms50=0x00, num=0x00, n1=0x00, n2=0x00; //Define the timer number variable, adjust the mode variable, press the plus 1 key number variable, press the minus 1 key number variable sbit key0=P1^0 ; //Adjust key--mode sbit key1=P1^1; //Add 1 key sbit key2=P1^2 ; //minus 1 key //--------------------------------------- void delay1ms( unsigned int z ) //define delay 1ms function { unsigned int x, y ; for ( x=z; x>0; x-- ) for ( y=120; y>0 ; y-- ) ; } //--------------------------------------- //Key processing function. If no key is pressed, the clock will run normally. When the K0 key is pressed once, the clock will pause and enter the hour adjustment state; press the K0 key once again, enter the minute adjustment state; press the K0 key once again, and return to the normal time adjustment state. For the K1 and K2 keys, if the clock is running normally, pressing the K1K2 keys will not work; if the clock enters the hour or minute adjustment state, press the K1 key to add 1 to the hour or minute, and the hour will return to 0 when it is added to 24, and the minute will return to 0 when it is added to 60. Press the K2 key to subtract 1 from the hour or minute, and the hour will return to 23 when it is reduced to a negative value, and the minute will return to 59 when it is reduced to a negative value. void keyscan( void ) {EA=0; //Disable interrupt if(timedat[0]==0&timedat[1]==0&timedat[2]==7) {P1=0xff;} if (key0==0) { delay1ms(10); while (key0==0); num++; TR0=0; ET0=0; // Each time the adjustment key K0 is pressed, the mode variable num is increased by 1 if (num>=3) {num=0; ET0=1; TR0=1; } // Start T0 time calibration } if (num!=0) //The pattern variable is not equal to 0 { if (key1==0) // K1 key is pressed { delay1ms(10); while (key1==0); timedat[num]++; //Record the number of times key K1 is pressed if (num==2) n1=24; else n1=60; //The pattern variable is equal to hour 2--then it will be late by one day or 60 minutes if (timedat[num]>=n1) { timedat[num]=0; } } } if (num!=0) { if (key2==0) { delay1ms(10); while (key2==0); timedat[num]--; //Record the number of times key K2 is pressed if (num==2) n2=23;else n2=59; if (timedat[num]<=0) {timedat[num]=n2; } } } EA=1; } //--------------------------------------- // Function for displaying digital tubes. The time is displayed by calculating and displaying the seconds first, then the seconds, then the minutes, then the minutes, then the tens, then the hours, then the hours, then the tens. void display( void ) { uchar k; if(timedat[0]==0&timedat[1]==0&timedat[2]==7) {P1=0xff;} dis[0]=timedat[0]%10; dis[1]=timedat[0]/10; //seconds dis[3]=timedat[1]%10; dis[4]=timedat[1]/10; // points dis[6]=timedat[2]%10; dis[7]=timedat[2]/10; //time for ( k=0; k<8; k++ ) { P0=tab[ dis[k] ]; P2=wetab[k]; delay1ms(1); P2=0x00; } } //--------------------------------------- //Main function, the time is displayed by calculating the units of seconds first, then the tens of seconds; then the units of minutes, then the tens of minutes; then the units of hours, then the tens of hours. The timer interrupt first detects whether 1 second has arrived → if 1 second has arrived, the seconds unit will increase by 1; if 1 second has not arrived, it detects whether 1 minute has arrived → if 1 minute has arrived, the minutes unit will increase by 1; if it has not arrived, it detects whether 1 hour has arrived → if 1 hour has arrived, the hours unit will increase by 1; if it has not arrived → then display the time. void main( void ) { TMOD=0x01; //T0 timing 50ms, mode 1 EA=1; ET0=1; TR0=1; //Open interrupt, enable T0 interrupt, start T0 TH0=0x3c; TL0=0xb0; while (1) { display( ) ; keyscan( ) ; } } //Timer/Counter T0 interrupt service function. The timer interrupt first detects whether 1 second has arrived → if 1 second has arrived, the second unit increases by 1; if 1 second has not arrived, detect whether 1 minute has arrived → if 1 minute has arrived, the minute unit increases by 1; if it has not arrived, detect whether 1 hour has arrived → if 1 hour has arrived, the hour unit increases by 1; if it has not arrived → the time is displayed. void timer0(void) interrupt 1 { ET0=0; TR0=0; //T0 interruption is not allowed and it is not started TH0=0x3c; TL0=0xb0; TR0=1; //Start T0 ms50++; P1=0xf7; if(timedat[0]==0&timedat[1]==0&timedat[2]==7) {P1=0xff;} if (ms50==20) { ms50=0x00; timedat[0]++; //The variable m returns to 0, and the second increases by 1 if (timedat[0]==60) { timedat[0]=0;timedat[1]++; //After the seconds reach 60, it returns to 0 and the minutes increase by 1 if (timedat[1]==60) { timedat[1]=0;timedat[2]++; //return to 0 after 60, and add 1 if (timedat[2]==24) { timedat[2]=0; } } } //Returns to 0 after time reaches 24 } ET0=1; //Enable T0 interrupt }
Previous article:Single chip microcomputer DS18B20 temperature measurement and control system
Next article:51 single chip microcomputer buzzer plays Twinkle Twinkle Little Star Two Tigers Ode to Joy
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Detailed explanation of intelligent car body perception system
- How to solve the problem that the servo drive is not enabled
- Why does the servo drive not power on?
- What point should I connect to when the servo is turned on?
- How to turn on the internal enable of Panasonic servo drive?
- What is the rigidity setting of Panasonic servo drive?
- How to change the inertia ratio of Panasonic servo drive
- What is the inertia ratio of the servo motor?
- Is it better for the motor to have a large or small moment of inertia?
- What is the difference between low inertia and high inertia of servo motors?
- Today's award-winning live broadcast: 5G and edge computing development and technology applications
- [AT-START-F403A Evaluation] 4. QR code scanning test
- MOSFET driver and MOSFET matching design
- How to select common mode inductors and differential mode inductors for circuit boards to suppress EMC and EMI
- Eliminate distractions
- STM32L151C8T6 wake-up problem from standby mode
- Learn how Vicor power modules address the conversion challenges of battery-powered robots
- 01_Basic principles of static timing analysis and timing analysis model
- Help!!! ADS1256 has abnormal status during testing
- A brief discussion on the causes of blistering on the copper electroplating board surface of the circuit board