This article takes the AT89C51 system with a 6MHz clock as an example to illustrate the application of clock interrupts:
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 W 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 add 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_DI, use the /MOV A,INCPI/ADD A,#25/MOV W_DI ,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_DI 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
SET 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 50 times
CLR P1.1; Set P1.1 to L, the pulse ends
Conclusion: From the above, we can see that we need to flexibly apply clock interrupts, reasonably allocate tasks to interrupts and main programs, and the two should have clear division of labor and simple interfaces.
Note: The execution time of the interrupt handler should be shortened as much as possible, and should not be longer than 20mS.
Previous article:Introduction to Misunderstandings in Software Anti-interference Technology of MCS51 Series Microcontrollers
Next article:Introduction to the interface application between single chip microcomputer and liquid crystal display
- Molex leverages SAP solutions to drive smart supply chain collaboration
- Pickering Launches New Future-Proof PXIe Single-Slot Controller for High-Performance Test and Measurement Applications
- CGD and Qorvo to jointly revolutionize motor control solutions
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Nidec Intelligent Motion is the first to launch an electric clutch ECU for two-wheeled vehicles
- Bosch and Tsinghua University renew cooperation agreement on artificial intelligence research to jointly promote the development of artificial intelligence in the industrial field
- GigaDevice unveils new MCU products, deeply unlocking industrial application scenarios with diversified products and solutions
- Advantech: Investing in Edge AI Innovation to Drive an Intelligent Future
- CGD and QORVO will revolutionize motor control solutions
- 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
- National Technology N32 MCU RF Resource Library (official, practical information)
- Raspberry Pi Pico Windows Development Environment - Compile under Visual Studio Code...
- Power circuit problem
- View Circuit-ADC and System (1)
- Teach you to understand the role of resistors in circuits
- EEWORLD University Hall----High-efficiency power architecture for smart door locks, battery-free light switches and wireless sensors
- 【E840-DTU】TCP connection
- Tank-1 test stand description (for burning and testing ESP8266 series)
- How to implement software reset and restart on HGI MCU
- Understanding digital signal processing