Functions implemented by the program:
1. After the experimental box is powered on, the current time is not displayed.
2. After sending "Start.E" to 8051, the electronic clock starts working and displays the current time (hours, minutes, seconds).
3. After sending "Stop.E" to 8051, the electronic clock stops working and hides the current time.
#include <STC89C5xRC.H>
#include unsigned char code DIG_CODE[10] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f}; //The corresponding digital tube displays 0~9 char buf[30]; //Store received data int tcount = 0; //Record the number of 2ms intervals passed int hour = 23, minute = 59, second = 55; void T0_INT() interrupt 1 //timer0 interrupt, triggered every 2ms { TR0 = 0; // turn off timer0 TH0 = 0xF8; TL0 = 0x30;//65536 - 2000 = 63536 = F830H if(tcount % 6 == 0) { // Seconds digit P2 = 0; //The first number from the right is bright P0 = DIG_CODE[second % 10]; tcount ++; } else if(tcount % 6 == 1) { //Tens of seconds P2 = 1 << 2; //The second number from the right is lit P0 = DIG_CODE[second / 10]; tcount ++; } else if(tcount % 6 == 2) { //Minutes digit P2 = 2 << 2; P0 = DIG_CODE[minute % 10]; tcount ++; } else if(tcount % 6 == 3) { //Tens of minutes P2 = 3 << 2; P0 = DIG_CODE[minute / 10]; tcount ++; } else if(tcount % 6 == 4) { //Hour digit P2 = 4 << 2; P0 = DIG_CODE[hour % 10]; tcount ++; } else if(tcount % 6 == 5) { //Tens of hours P2 = 5 << 2; P0 = DIG_CODE[hour / 10]; tcount ++; } if(tcount == 500) //2ms * 500 = 1000ms = 1s -> 1s time is up { tcount = 0; second ++; if(second == 60) { second = 0; minute ++; if(minute == 60) { minute = 0; hour ++; if(hour == 24) { hour = 0; } } } } TR0 = 1; //Restart timer0 } int main() { int i; P2 = 111 << 2; P0 = 0; // All digital tubes are off TMOD = 0x21; SCON = 0x50; //Set serial communication format TH0 = 0xF8; TL0 = 0x30; TH1 = 0xE6; TL1 = 0xE6; IE = 0x82; TR0 = 0; //Do not start timer0 yet TR1 = 1; // Turn on timer1 to generate baud rate while(1) { if(RI == 1) //Receive data { RI = 0; buf[i++] = SBUF; //Receive data from the serial port buf[i] = ''; //Manually add the end of string mark if(i == 29) //Prevent array access out of bounds { i = 0; } if(buf[i - 1] == 'E') // reach the end { i = 0; if(strcmp(buf, "Start.E") == 0) //Start.E command-> start the clock { TR0 = 1; //Start timer 0 } else if(strcmp(buf, "Stop.E") == 0) //Stop.E command-> turn off the clock { TR0 = 0; // turn off timer 0 P2 = 111 << 2; P0 = 0; // Clear Ping } } } } return 0; }
Previous article:An electronic clock based on 8051 that can start, stop, display or hide time (Version 1.2)
Next article:Electronic clock based on 8051 (Version 1.0)
Recommended ReadingLatest update time:2024-11-16 07:55
- Popular Resources
- Popular amplifiers
- MCU C language programming and Proteus simulation technology (Xu Aijun)
- Principles and Applications of Single Chip Microcomputers 3rd Edition (Zhang Yigang)
- Principles and Applications of Single Chip Microcomputers and C51 Programming (3rd Edition) (Xie Weicheng, Yang Jiaguo)
- Dual Radar: A Dual 4D Radar Multimodal Dataset for Autonomous Driving
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
- FAQ_How to convert Tmall Genie triples to the format used by ST mesh code
- EEWORLD University Hall----What can be easily charged?
- Share the steps of DSP development
- The long-awaited Digi-Key STM32F7508-DK and MAiX Bit unboxing notes
- It is customary to connect a 120Ω resistor between RS485 interface AB and CAN interface H and L. I only have 100Ω in stock. Can it be replaced?
- [NXP Rapid IoT Review] Online IDE Experience
- Let's take a look at the GNU C __attribute__ mechanism
- i.MX6ULL Terminator uses Yocto file system to develop QT-QT project compilation and testing
- Introduction to low-power Bluetooth technology solutions for lithium battery intelligent protection boards
- Sony employee stole $154 million in public funds and exchanged them for Bitcoin: when it was recovered, it appreciated to over $180 million