The stopwatch time is set by the timer, and the set time is displayed through the common cathode digital tube.
#include #define uchar unsigned char #define uint unsigned int uchar code table [] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40}; // Common cathode segment code table uchar code table_SMG[] = {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07}; //bit selection //------------------------Variable area------------------------ uchar count ; // count variable uchar Time_T,Time_f,Time_s; //Contains time variables //------------------------Function area------------------------ void Delay(uint t);//delay function void Init_ET_0(); //Timer initialization function 50ms void Display_SMG(uchar pos , uchar date); //Single digital tube display function (bit selection, segment selection) void Display_Time(); //Time display function void Delay(uint t) //delay function { while(t--); } void Init_ET_0() //Timer initialization function 50ms { TMOD = 0x01; //Timer 0 working mode 1 EA = 1; // Enable general interrupt ET0 = 1; //Turn on the timer switch TR0 = 1; //Start the timer TH0 = (65536 - 50000)/256; //Assign initial value to the high eight bits TL0 = (65536 - 50000)%256; //Assign initial value to the lower eight bits } void Servoic_ET_0() interrupt 1 //Timer service function { TH0 = (65536 - 50000)/256; //Reinstall the upper eight bits TL0 = (65536 - 50000)%256; //Reinstall the lower eight bits count++; //add once every 50ms if(count == 20) // add 20 times to 1 second { Time_s++; //Increase once every 1 second count = 0; //Release the variable value to make it timely for 50ms if(Time_s == 60) //When it reaches 60S, add 1 { Time_f++; // add 1 to the minute Time_s = 0; //Set s to zero if(Time_f == 60) //When it reaches 60 minutes, add 1 { Time_T++; // add 1 to the time Time_f = 0; //set time to zero if(Time_T == 24) //When it reaches 24, set the time variable to zero { Time_T = 0; //Set the time variable to zero } } } } } void Display_SMG(uchar pos , uchar date) //Single digital tube display function (bit selection, segment selection) { P2 = table_SMG [pos]; //Position selection P1 = date ;//segment selection Delay(100); // Delay for a short time for dynamic display } void Display_Time() //Time display function { Display_SMG(7 , table [Time_s%10]); //Display the seconds Display_SMG(6 , table [Time_s/10]); //Display the tens of seconds (recursive below) Display_SMG(5 , table [10]); // separator Display_SMG(4 , table [Time_f%10]); Display_SMG(3 , table [Time_f/10]); Display_SMG(2, table[10]);//separator Display_SMG(1 , table [Time_T%10]); Display_SMG(0 , table [Time_T/10]); } void main() { Init_ET_0(); //Timer 0 initialization while(1) { Display_Time(); //Continuously refresh the display time } } The simulation diagram is as follows
Previous article:Generate square wave using timer and adjust square wave frequency through interrupt
Next article:Using the timer to make the microcontroller output square waves
- 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
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Brief Analysis of Automotive Ethernet Test Content and Test Methods
- How haptic technology can enhance driving safety
- Let’s talk about the “Three Musketeers” of radar in autonomous driving
- Why software-defined vehicles transform cars from tools into living spaces
- How Lucid is overtaking Tesla with smaller motors
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Application of Finite State Machine in Embedded Software
- I'm begging for a solution for medical ultrasonic atomizer!!!
- How to set the parameters of RC snubber circuit of mosfet switch tube in boost circuit
- Why is there a small series resistor on the digital signal line? (DC2222A LTC2500)
- DSP and Always-on Function
- A boss can work in multiple companies at the same time, so why would an engineer be fired if he has work experience in two companies?
- 【CH579M-R1】What is TMOS?
- Why is there a 50mV difference between the two input terminals of an op amp working in negative feedback?
- Award-winning live broadcast: Bluetooth 5.0, Thread, Zigbee, TI 15.4-Stack, multi-protocol management, all explained to you at once
- Programming implementation of volume control for media applications based on touchpad (Hot Wheels)