Interrupts and timers implement a 24-hour clock. The program is as follows:
#include
#define PORTLEN P0
sbit bit_select = P2^0;
sbit seg_select = P2^1;
unsigned char src[16] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
unsigned char qrc[8] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
unsigned char second1[8];
unsigned char shi = 0;
unsigned char fen = 0;
unsigned char miao = 0;
void timer0_init(void)
{
EA = 1; // Enable general interrupt
TMOD |= 0x01; //Set working mode
TH0 = (65536 - 20000) / 256;
TL0 = (65536 - 20000) % 256; //Set the initial value of the timer and generate an interrupt every 1ms
ET0 = 1; // Enable timer interrupt
TR0 = 1; //Turn on the timer and start adding 1 every 1ms
} // Turn on the timer and start adding 1. When TH0 = 0xff, TL0 = 0xff, TF0 (the overflow flag of the internal timer of the microcontroller, generally no need to set it manually) is set from 0 to 1, triggering an interrupt, and reset to 0.
Then request an interrupt from the CPU. If the CPU responds, it jumps to the interrupt function, i.e. void timer0_isr() interrupt 1
void timer1_init(void)
{
EA = 1;
TMOD |= 0x10;
TH1 = (65536 - 1000) / 256;
TL1 = (65536 - 1000) % 256;
ET1 = 1;
TR1 = 1;
}
void main()
{
timer0_init();
timer1_init();
while(1);
}
//while(1) is equivalent to an infinite loop. There is a point to note here. The difference between putting the two timers inside and outside the while is explained in detail later.
void display()
{
static unsigned char i = 0;
PORTLEN = 0x0; // Eliminate ghosting
seg_select = 1;
seg_select = 0;
PORTLEN = 0xff;
bit_select = 1;
bit_select = 0;
PORTLEN = second1[i];
seg_select = 1;
seg_select = 0;
PORTLEN = qrc[i];
bit_select = 1;
bit_select = 0;
i++;
if(8 == i)
{
i = 0;
}
}
void timer0_isr() interrupt 1
{
static unsigned char i = 0;
TH0 = (65536 - 20000) / 256;
TL0 = (65536 - 20000) % 256; //Set the initial value of the timer and generate an interrupt every 20ms
i++;
if(50 == i)
{
i = 0;
miao++;
if(60 == miao)
{
miao = 0;
fen++;
if(60 == fen)
{
fen = 0;
shi++;
if(24 == shi)
{
shi = 0;
}
}
}
}
second1[0] = src[shi / 10];
second1[1] = src[shi % 10];
second1[2] = 0x40;
second1[3] = src[fen / 10];
second1[4] = src[fen % 10];
second1[5] = 0x40;
second1[6] = src[miao / 10];
second1[7] = src[miao % 10];
}
void timer1_isr() interrupt 3
{
TH1 = (65536 - 1000) / 256;
TL1 = (65536 - 1000) % 256;
display();
}
//interrupt x The x value following interrupt is the interrupt number, which means the interrupt port number to which this function corresponds. In 51, 0 is external interrupt 0; 1 is timer 0; 2 is external interrupt 1; 3 is timer 1; 4 is serial interrupt
In fact, when compiling, the entry address of your function is set to the jump address of the corresponding interrupt.
——————————————————————————————
Next, let’s talk about while. Use A and B to replace the two functions timer0_init and time1_init respectively. A+ and B+ represent two interrupt service routines.
①After A and B are executed, A continues to execute. However, it should be noted that the execution of A and B only turns on the timer at the end of the execution. It can be almost regarded as the end of A to start the timer accumulation, and then the same for B. However, as soon as B ends, it comes to A again. TH and TL in A are reset, resulting in interruption all the time and no entry into the interrupt service function. If you want to modify it here, you need to add your own delay function to give it time to enter the interrupt service program, but this is not as meaningful as the second method.
② In the second case, after A and B are executed, they stay in the while state, waiting for A and B to send out interrupt responses, entering the interrupt service routine according to the priority, and waiting for the interrupt service routine to end according to the internal interrupt, and returning to the while state. (The initial value has been given again in the interrupt service routine, and the timer will not be turned off automatically after being turned on). B has been refreshing the display, and A is giving values to the temp array.
The execution time on the right side of the above figure is wrong. I forgot to change it. The 1ms and 20ms in the interrupt service program are just re-initialized.
Previous article:51 MCU timer interrupt program
Next article:Interrupt timer and running light
Recommended ReadingLatest update time:2024-11-16 08:53
- 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
- Explaining Infineon's largest acquisition in history: the $10 billion cash acquisition of Cypress
- I found a very good 51 single-chip microcomputer introductory tutorial book and recommend it to beginners. It is suitable for beginners to learn programming by themselves.
- [ESP32-Korvo Review] Part 3: Smart Voice Assistant ESP-Skainet
- Differences between MSP430 programmer emulator and JTAG, SBW, and BSL interfaces
- ESP32-S2 development pit (1) - open filling
- MSP430 timer controls PWM output
- 【Short-term weather forecast system】Basic functions of indoor devices are realized
- [GD32E503 Review] Part 6: FreeRTOS and RTC screen display
- [Synopsys IP Resources] Automotive cybersecurity starts with chips and IP
- ARM MCU and ARM Core