What is an interrupt routine?
A vivid metaphor is like you are reading my article now, and suddenly your friend asks you to go roast sweet potatoes together. At this time, you interrupt reading the article and roast sweet potatoes with your friend. After roasting sweet potatoes, you return to reading the article. Roasting sweet potatoes is like interrupting the program, which interrupts your reading of the article. In terms of the program, when the CPU is executing a program, an interrupt event suddenly occurs, and the CPU executes the interrupt program. After the execution is completed, the CPU returns to execute the original program.
Interrupt Events
What is an interrupt event? It is an event that causes an interrupt. For a microcontroller, these events are varied. For example, a key is pressed, a certain time has passed, a string of data is sent, or a data is received.
When talking about interrupts, we have to talk about queries, which are the opposite of interrupts. In fact, whether it is a button being pressed, a time being reached, or data being sent, these can actually be done by querying. For example, if you are a manager and you want to know whether your subordinates have completed their tasks, one way is to ask them whether the tasks have been completed. If they have not completed the tasks in the morning, ask them in the afternoon. If they have not completed the tasks in the afternoon, ask them again the next day... until the tasks are completed. This method is equivalent to the query method. Another method is to let your subordinates complete the tasks and report directly. You do not need to disturb your subordinates during the period when they are performing the tasks. When your subordinates complete the tasks, they will report to you as soon as possible. This method is like an interrupt.
Query method: The disadvantage is that it may waste a lot of CPU time and keep querying. It is fine if there are not many things, but once there are many things, the running speed will obviously slow down.
Interrupt mode: can be used in situations where time and response speed are required.
What events will cause interruption?
1. Interrupt control register INTCON
2. Peripheral interrupt enable register PIEX Note: X can be 1 2 3 4... The number of different microcontroller models is different
3. Peripheral interrupt flag register PIRX Note: X can be 1 2 3 4... The number of different microcontroller models is different
INTCON interrupt control register explanation:
1 Enable or disable the global interrupt function (GIE)
2. Turn on or off the interrupt function of all peripherals (PEIE). The peripherals are the devices written in the peripheral interrupt enable/flag register.
3 Enable some interrupt events.
PIEX and PIRX correspond to each other. For example, when TMR1IE of PIE1 is set to 1, timer timr1 will start interrupting. When TMR1 timer overflows, TMRIF of PIR1 will be 1 and the interrupt program will be executed.
Example explanation:
We modify the example in the previous lecture "PIC Microcontroller Timer" into an interrupt method to turn on the LED every 50MS and turn off the LED every 50MS.
/*Development environment MPLAB X IDE Chip model PIC16LF1823*/
#include
__CONFIG(FOSC_INTOSC&WDTE_OFF&PWRTE_ON&MCLRE_OFF&CP_ON&CPD_OFF&BOREN_ON
&CLKOUTEN_OFF&IESO_ON&FCMEN_ON);//This should be put on the previous line
__CONFIG(PLLEN_OFF&LVP_OFF) ;
#define LED LATA5
void init_fosc(void)
{
OSCCON= 0x68;
}
void init_gpio(void)
{
PORT = 0;
YEARS = 0;
ANSELA = 0;
TRISAbits.TRISA5=0;
}
void init_timer0(void)
{
OPTION_REG=0x87;
}
void interrupt isr(void) //interrupt procedure, interrupt is a keyword to indicate that this function is an interrupt function.
{
LED = ~LED; //Change the state of LED
INTCONbits.TMR0IF=0; //Clear the interrupt flag. If it is not cleared before leaving the interrupt routine, the program will interrupt continuously.
TMR0=61; //Give TMR0 an initial value of 61 and prepare for the next 50ms timing.
//The interrupt function ends and returns to the main function. Go back to where you came from, that is, return to while(1); in the main function
}
int main(int argc, char** argv)
{
init_dark();
init_gpio();
init_timer0();
INTCONbits.GIE=1; // Enable general interrupt
INTCONbits.TMR0IF=0; // Clear TMR0 overflow interrupt flag
INTCONbits.TMR0IE=1; // Enable TMR0 overflow interrupt
TMR0=61;
while(1); //The main function does nothing here and keeps looping. But when 50ms is up, TMR0 will overflow and the program will jump to void interrupt isr(void) for execution.
}
Let me help you sort out the general steps of the initial interrupt setting
1, enable general interrupt, enable peripheral interrupt. INTCONbits.GIE=1; INTCONbits.PEIE=1; In fact, it doesn't matter if you don't use the peripherals.
2. Clear the flag bit of the corresponding interrupt. For example, INTCONbits.TMR0IF=0;
3. Enable the corresponding interrupt. For example, INTCONbits.TMR0IE=1;
What needs to be paid attention to in the interrupt function/program is to clear the corresponding interrupt flag bit, such as INTCONbits.TMR0IF=0; otherwise the microcontroller will think that the interrupt has not been executed and will continue to interrupt.
Previous article:PIC MCU I/O Control
Next article:Summary of PIC microcontroller C knowledge points
- 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
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Three steps to govern hybrid multicloud environments
- Three steps to govern hybrid multicloud environments
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Melexis launches ultra-low power automotive contactless micro-power switch chip
- Melexis launches ultra-low power automotive contactless micro-power switch chip
- Recruitment: Assistant Optical Engineer, Assistant Electronic Engineer, Assistant CAE Engineer, Assistant Structural Engineer
- Multi-core DSP products in the SoC era
- Detailed explanation of Altera series FPGA chip IP core
- SinlinxA33 Modify the configuration file to change the output serial port
- 【Home treasure】 esp32s2 lvgl running lcd io configuration
- Balanced Amplifier
- Nuvoton 003 Series Platform MS51 Replacement N76E003 Guide V1.0
- Practical Guide for Motor Application Development - Based on WildFire STM32 Full Series Development Board
- 10G high-speed cable processing
- STM32F429IGH6