In the design of single-chip microcomputer program, setting a good clock interrupt will enable one CPU to play the role of two CPUs, greatly facilitate and simplify the compilation of programs, and improve the efficiency and operability of the system. We can put some routine programs that need to be executed regularly in the clock interrupt, and we can also use the clock interrupt to assist the main program to complete timing, delay and other operations.
The following takes the AT89C51 system with a 6MHz clock as an example to illustrate the application of clock interruption.
Timer initial value and interrupt cycle The clock interrupt does not need to be too frequent, generally 20mS (50Hz) is sufficient. If a time base signal of one hundredth of a second is required, 10mS (100Hz) can be used. Here we take 20mS, and use timer T0 to work in 16-bit timer mode (mode 1). The working mode of T0 is: it automatically adds 1 after each machine cycle. When the count reaches 0FFFFh and is about to overflow, an interrupt will be generated, and the hardware will set the corresponding flag bit for software query. That is, when the interrupt occurs, N+1 machine cycles have passed compared to when it was started. Therefore, we only need to pre-store a number that is N less than the full value 0FFFFh in T0, and then start the timer, and an interrupt will be generated after N machine cycles. This value is the so-called "initial value". The following is the calculation of the initial value we need: the clock is 6MHz, 12 clock cycles are one machine cycle, and there are 10,000 machine cycles in 20mS. (10000)10=(2710)16, then 0FFFFh-2710h+1=0D8F0h. Since it takes 7~8 machine cycles to respond to interrupt, protect the context and reload the initial value, add 7 to this value, that is, the initial value that should be loaded into T0 is 0D8F7h. After each interrupt is entered, the values of A and PSW are pushed into the stack first, and then 0D8F7h is loaded into T0.
Set a unit, add 1 every time an interruption We can take a unit in the internal RAM and name it INCPI (Increase Per Interrupt). In the interruption, after loading the initial value of T0, use the INC INCPI instruction to increase it by 1. From this unit, both the interrupt program and the main program can obtain any integer multiple of 20mS between 1 and 256. For example: there is a program that sends display to the digital tube, which needs to be executed every 0.5 seconds to refresh the display. You can set a unit (called a waiting unit) W_DISP, use the /MOV A,INCPI/ADD A,#25/MOV W_DISP,A/ statement to make it 25 greater than the current INCPI value, and then check whether it is equal to the INCPI value in each interruption. If it is equal, it means that 25 interrupt cycles have passed, then the display sending program will be executed, and W_DISP will be added with 25, waiting for the next 0.5 seconds. We can set multiple waiting units to extract multiple different time base signals. Let the interrupt program query each waiting unit in turn to see if it is equal to INCPI at each interrupt. If they are equal, execute the corresponding processing and reset the value of the waiting unit, otherwise skip it. For example: refresh or flash the display with a 0.5 second signal, generate a real-time clock with a 1 second signal, or output a square wave of a certain frequency, query the input device at a certain interval, etc.
Reading keys in interrupts Usually, we read the keyboard in the main program, the steps are: scan the keyboard, if a key is pressed, delay for tens of milliseconds to debounce, confirm again that the key is indeed pressed, and then process the work corresponding to the key, and repeat the above steps after completion. But there are two shortcomings: 1. The key input cannot be latched when processing the corresponding work, that is, the key may be missed. 2. The CPU cannot do other things during the delay debounce, which is not efficient. If the key reading is put into the clock interrupt, the above shortcomings can be avoided. The method is: if the same key is read as pressed in two adjacent interrupts, the key is valid (the debounce purpose is achieved), and it is latched into the first-in-first-out (queue) keyboard buffer, waiting for the main program to process. In this way, the main program can still respond to keyboard input while processing the key. The buffer depth can usually be set to 8 levels. If the number of latched keys is more than 8, the new key is ignored, and an alarm is issued to remind the user that the new key will be invalid. If the keyboard buffer queue stagnates for much longer than the maximum time required for the main program to process a key press, it means that the main program has made an error or run away. In this case, you can use an instruction to reset the system during an interrupt, thus fulfilling the purpose of a watchdog.
The delay in the main program has a normally open clock interrupt, so when the main program needs a shorter delay with higher precision, the clock interrupt should be temporarily closed. If the program needs a longer delay with lower precision, the following writing method can be used to avoid multi-layer nested loop delays.
Example: Output a high level pulse for 1 second on P1.1
MOV A,INCPI
INC A
CJNE A,INCPI$ ; Wait for an interrupt to complete
SETB P1.1; Set P1.1 to H, pulse starts
ADD A,#50; 50 20mS equals 1 second
CJNE A,INCPI,$ ; Wait for the interruption to increase INCPI by 1 50 times
CLR P1.1; Set P1.1 to L, the pulse ends
Conclusion: From the above, we can see that we should flexibly apply clock interrupts, reasonably allocate tasks to interrupts and main programs, and the two should have clear division of labor and simple interfaces. The skills in this area still need to be explored and experienced in practice. In addition, we should pay attention to: the execution time of the interrupt handler should be shortened as much as possible, and it should not be longer than 20mS.
Previous article:The Three-wire Structure Design of Serial Peripheral Interface Circuit of Single Chip Microcomputer
Next article:Software configuration problem of using the "external counting" TIMx_ETR pulse measurement of the stm8 microcontroller
Recommended ReadingLatest update time:2024-11-16 12:50
- Popular Resources
- Popular amplifiers
- Wireless Sensor Network Technology and Applications (Edited by Mou Si, Yin Hong, and Su Xing)
- Modern Electronic Technology Training Course (Edited by Yao Youfeng)
- Modern arc welding power supply and its control
- Small AC Servo Motor Control Circuit Design (by Masaru Ishijima; translated by Xue Liang and Zhu Jianjun, by Masaru Ishijima, Xue Liang, and Zhu Jianjun)
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
- How to prevent wage subsidy fraud? All Sohu employees were exposed to wage subsidy fraud
- How to use a power amplifier to amplify and output a pulse train signal? How to use the Burst function of a signal generator?
- 【DIY Creative LED】LED lights and holes
- Surge arrester explanation and working principle
- Is there any error in the schematic diagram of the electrostatic generator?
- Cheap_Flash_FS (SPI_Flash version) -- embedded SPI_FLASH file system free source code, please download
- How to write interrupt function after using library function in MSP430F5529
- TPS61040 boost circuit abnormality
- [Atria AT32WB415 series Bluetooth BLE 5.0 MCU] + CAN communication
- What does it mean when a pushpin icon is displayed during STM32CubeMX pin configuration?