introduction
The number of interrupt sources of PIC series microcontrollers has reached 14, which is quite rich; but it also brings some problems: the problem of not being able to handle "high-level priority processing" when handling multiple interrupts, so many interrupt sources are prone to interrupt conflicts when processing, how to effectively handle the timing of interrupt arrival, and how to implement the algorithm have become the first problems that need to be solved.
1 Interrupt handling technology
Although the PIC series microcontrollers have many interrupts, they do not specify the priority of interrupts. When an interrupt occurs, no judgment is made, but the pointer is pointed to 0004H (the starting address of the interrupt) first, and the next operation is completely left to the user's "soft processing". The interrupt timing diagram is as follows:
Figure 1 INT pin interrupt timing diagram
The protection of the interrupt scene is a very important part of the interrupt technology. For the PICl6F87X microcontroller, during the interrupt service program, only the return address, that is, the value of the program counter Pc, is automatically pushed into the hardware stack; and in the interrupt handler, it is generally necessary to back up the contents of these registers at the beginning of the interrupt handler, just like using registers such as WReg and STATUS, that is, to perform the so-called scene protection.
The PICl6F87X sub-series microcontroller has up to 14 interrupt sources, but only one interrupt vector. There is no priority level between the interrupt sources, and there is no non-maskable interrupt. The PIC microcontroller uses a hardware stack structure, which does not occupy program memory space or data memory space, and does not require the user to operate the stack pointer; but at the same time, it also determines that it does not have the push (PUSH) and pop (POP) instructions in other microcontroller instruction systems. When implementing interrupt scene protection, it cannot be implemented using a stack, but rather through copying and backing up variables. The general implementation method is: first determine the scene to be protected, generally including the contents of registers such as WReg and STATUS, and then define variables corresponding to these registers on each page. To back up the scene. When an interrupt occurs, copy the contents of these scene registers to the backup variables at the beginning of the interrupt processing code, and then copy them back to restore the scene when exiting the interrupt processing:
2 Design of frequency meter based on PICl6F87X microcontroller
With the rapid development of electronic technology, control devices with single-chip microcomputer as the core have been fully penetrated into all aspects of test instruments and metrology verification. At the same time, frequency meter, as a common tool, is widely used in engineering technology, radio measurement, metrology and other fields. The designed frequency meter is mainly used to measure pulse frequency.
2.1 Design Principles
The PIC16F87X microcontroller has integrated capture, comparison, and pulse width modulation (PWM) modules. When the module works in capture mode, it can capture the rising or falling edge of the external input pulse and generate a corresponding interrupt.
The PIC16F87X microcontroller also integrates a timer module, which uses TMRI as a timer. The working principle of the timer is to increment from 0000H to FFFFH through the TMR1 "register pair" TMR1H: TMR1L. When it returns to 0000H, a high overflow will occur, and the overflow interrupt flag TMR1IF will be set to 1, causing a CPU interrupt response.
In a uniform pulse sequence, the pulse frequency value is equal to the number of pulses occurring per unit time. Based on this principle, the built-in timer module TMR1 of the PIC16F87X series microcontroller can be used for timing, and the capture function of the module can be used at the same time. Every n (n=1, 4, 16) pulses are captured once and an interrupt is generated, and the timer timing tl and tm when the first and (m-1)*n+1 pulses arrive can be recorded.
The pulse frequency value can be obtained by dividing the number of captured pulses by the time interval between the 1st and (m-1)*n+1th pulses. Therefore, the pulse frequency value calculation formula is:
f=n*(m-1)/(tm-t1)
If a larger frequency needs to be measured, a frequency divider with a corresponding multiple can be connected between the frequency to be measured and the CPU * port as needed. Each time a 1/n frequency divider is connected, the measurable frequency range can be expanded n times (as shown in Figure 2).
Figure 2 CPU external connection diagram [page]
2.2 Programming
(1) Main program
The main program flow chart is shown in Figure 3.
Figure 3 Main program flow chart
(2) Interrupt program
The interrupt program flow chart is shown in Figure 4.
Figure 4 Interrupt subroutine flow chart
3. Issues to note when handling multiple interrupts
1) To handle the problem of multiple interrupts, the interrupt handler algorithm can be roughly divided into two categories: weak interrupt priority (using CALL and RETURN method) and strong interrupt priority (using GOTO method).
2) If multiple interrupt requests occur simultaneously, the order in which the interrupts are processed depends on the order in which the interrupt sources are checked in the interrupt routine.
3) To prevent interrupt requests from being lost, pay attention to the following two situations: If the interrupt interval of the same interrupt source is less than the processing time of the interrupt service, the interrupt event may be ignored (reflected in the interrupt service process, the flag bit is set twice in succession). For example: the time interval between interrupt events is 30ms, and the time for interrupt service processing plus jump judgment is 50ms, then the situation will be as follows:
[Number of interruptions______1][Number of interruptions______2][Number of interruptions_____3][Number of interruptions_____4]
[Number of processings______1][Number of processings______2][Number of processings_____4]
If IF is cleared at the beginning of interrupt processing, then as shown in the figure above, interrupt events 3 and 4 occur twice during processing time 2. In this case, even if IF is cleared before interrupt time 3 occurs, the third interrupt will be lost.
In addition, even if the time interval between interrupts is greater than the time interval between interrupt services, if the instruction for clearing the interrupt flag is placed at the end of the interrupt service subroutine, the interrupt request may be lost (i.e., two interrupt flag setting events correspond to only one clearing instruction and one interrupt processing). In other words, if the instruction for clearing the interrupt flag is placed at the end of the interrupt service program, the opportunity to respond to the second interrupt request from the interrupt source during interrupt processing may be lost.
4) In order to write a concise interrupt program, we should grasp the real-time feature of the interrupt. For the real-time interrupt data acquisition system, the interrupt feature is data acquisition. Therefore, in the interrupt program, only data acquisition and flag bit setting should be processed, and data processing should be placed outside the interrupt. The main program performs data processing through loop detection. The specific approach is: first open up a "storage buffer" as a transmission medium for the collected data, that is, to store the collected data and wait for the main program to process it; the interrupt program is responsible for data collection and assigns the collected data value to the "storage buffer"; the main program repeatedly detects the "storage buffer" through conditional loop statements and processes the collected information in a timely manner. In this way, the processing method can not only effectively realize the function of the interrupt, but also greatly reduce the time of each interrupt and improve the response speed of the entire program.
5) For applications with strict requirements on interrupt response and processing time, the instruction arrangement for protecting the scene should also consider the delay issue.
6) The CPU must be prohibited from responding to interrupts when performing table lookup operations to avoid jumping to an undesired address when the interrupt returns.
4 Conclusion
Flexible application of interrupts can not only save a lot of CPU resources, but also simplify the program. It has higher real-time performance and stability. After testing, this frequency meter has the advantages of high measurement accuracy, easy use, stability and reliability, and can be used in the field of measurement and testing. At the same time, due to the use of software control, simple circuit structure, less hardware, low cost and easy to carry, it can also be widely used in industrial and agricultural production and residents' lives, and has promotion value.
Previous article:Common Input/Output Circuit Design Methods for Single Chip Microcomputers
Next article:C language exercises for microcontroller counter
Recommended ReadingLatest update time:2024-11-16 17:43
- 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
- What should be paid attention to for FCCSP packaged PCB?
- SimpleLink Development Red Book
- Altium Designer 19.1.6, Reports BOM does not generate the BOM table, and AD19 will be stuck. What is the reason?
- Do you really know the impact of AC coupling capacitors?
- [Digi-Key Innovation Design Competition] Material Unboxing + ESP32-S3-BOX
- EEWORLD University - Code reuse demonstration using TI SimpleLink? MCU platform
- STM32 RTC registers
- Inductance calculation formula
- [NXP Rapid IoT Review] + Recommendation for NXP Rapid IoT to connect the differential ADC input port of KW64 to the expansion port
- [Allwinner V853 heterogeneous AI intelligent vision development board review] "mkimage command not found" error solution...