Example to explain the interrupt program of PIC microcontroller

Publisher:灵感火花Latest update time:2018-11-22 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

 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.


Reference address:Example to explain the interrupt program of PIC microcontroller

Previous article:PIC MCU I/O Control
Next article:Summary of PIC microcontroller C knowledge points

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号