Using stc89c52 microcontroller, the circuit is very simple, using eight-bit common anode digital tube, P0 port is the segment selection of the digital tube, P2 port is the bit selection. Use key1-key3 to adjust the time
Key points:
1. Modularization of functions
2. Interruption
#include#define uchar unsigned char #define uint unsigned int sbit p2_0=P2^0; sbit p2_1=P2^1; sbit p2_2=P2^2; sbit p2_3=P2^3; sbit p2_4=P2^4; sbit p2_5=P2^5; sbit p2_6=P2^6; sbit p2_7=P2^7; sbit key0=P3^2; sbit key1=P3^3; sbit key2=P3^4; sbit key3=P3^5; sbit p17=P1^7; uint t=0; uint key=0; uchar set=0; uchar second=00,minter=55,hour=11; uchar second_1,second_2,minter_1,minter_2,hour_1,hour_2; const uchar tab[]={ 0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf} ; //0~~9 digital table void delay() //Delay subroutine { uint i; for(i=0;i<240;i++); } void time_0(void) interrupt 1 using 0 //Internal timer 0 internal timing 50ms { TH0=0x3c; TL0=0xb0; t++; // t is the number of times the timer is counted } void display(uchar secondd,uchar minterr,uchar hourr) //Subroutine to display the time { second_1=secondd/10; second_2=secondd%10; minter_1=minterr/10; minter_2=minterr%10; hour_1=hourr/10; hour_2=hourr%10; P0=tab[second_2]; p2_0=0; delay(); p2_0=1; P0=tab[second_1]; p2_1=0; delay(); p2_1=1; p2_2=0; P0=0xbf; delay(); p2_2=1; P0=tab[minter_2]; p2_3=0; delay(); p2_3=1; P0=tab[minter_1]; p2_4=0; delay(); p2_4=1; p2_5=0; P0=0xbf; delay(); //The delay is to make — “——” appear, otherwise the time is too fast, — p2_5=1; P0=tab[hour_2]; p2_6=0; delay(); p2_6=1; P0=tab[hour_1]; p2_7=0; delay(); p2_7=1; } void time() { //Has the 1s timing reached? if(t==19) //1s is up, add one second { t=0; second++; if(second==60) // Is it 60 seconds? If so, reset the seconds to zero and add one second to the minutes. If not, exit. { second=0; minter++; if(minter==60) // Is the minute 60? If yes, clear the minute and add one to the hour; if no, exit { minter=0; hour++; if(hour==24) { hour=0; } } } } display(second,minter,hour); //Call the display function to display the time } uint scan_key() //Adjust time button scanning function { //Delay delay(); //Judge if key3 is pressed, return 1 to call the time adjustment function, if it is not zero, return 0 and do not adjust if(key0==0) set=1; if(key0==1) set=0; return set; } void set_time() // adjust time function { uchar m; if(key1==0) delay(); //Adjust the hour, confirm key1 is pressed if(key1==0) hour++; // x hours increment if(hour==24) hour=0; //Hours count to 24, starting from zero for(m=0;m<20;m++) //Loop 20 times { time(); //Call the display function to display the time } if(key2==0) delay(); //Adjust minutes if(key2==0) minter++; if(minter==60) minter=0; delay(); time(); //Call the time function to display the time if (key0 == 0) time(); } void main() //The main function sets the internal timer { TMOD=0X01; TH0=0X10; TL0=0X00; EA=1; ET0=1; TR0=1; P2=0xff; while(1) { if(key0==0) // p determines whether the time adjustment button is pressed, that is, whether the time needs to be adjusted { p17=0; delay(); delay(); delay(); p17=1; delay(); delay(); key=scan_key(); } switch(key) { //Has the 1s timer expired? case 0: time(); break; case 1: set_time();break; default :break; } } }
Previous article:Digital tube dynamic display program
Next article:Digital tube water light display data
Recommended ReadingLatest update time:2024-11-16 15:21
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- I would like to ask you about the problem when starting MINI6410
- MicroPython User Development Guide
- Accidentally leaked company source code to GitHub, fined 200,000 and sentenced to six months in prison!
- EEWORLD University Hall----Easy simulation and control of quadcopters with MATLAB & Simulink
- MSP430 Reference Designs for Medical Applications
- EEWORLD University Hall----MIT Open Course: Circuits and Electronics
- Brief Introduction of Push-Pull Circuit
- Clock system of MSP430 microcontroller
- Signal generator and DA conversion
- Eleven Things to Avoid with Analog Noise