PIC microcontroller interrupt program example
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)
{
PORTA = 0;
LATA = 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_fosc();
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 microcontroller electric thermal shear circuit schematic diagram
Next article:PIC MCU Application Questions and Answers 14 articles
Recommended ReadingLatest update time:2024-11-16 12:48
- 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
- 【Topmicro Intelligent Display Module】Part 4: Comprehensive evaluation of text scrolling, RTC, etc.
- Display QR code-Intelligent TFT module
- EEWORLD University ---- Open Source PWM Robotic Arm (STM32 Version) Video Tutorial
- [RVB2601 Creative Application Development] Part 4 Sound Histogram
- Waveform changes of different ANT frequencies
- Regulator tube usage help
- [GD32L233C-START Review] IV. USART
- Electric vehicle charger schematic, file name is chip name
- SD card creative stickers
- How do you entertain yourself while staying at home?