PIC microcontroller interrupt program example

Publisher:才富五车330Latest update time:2020-02-05 Source: eefocusKeywords:PIC Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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.

Keywords:PIC Reference address:PIC microcontroller interrupt program example

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

DS18B20 and PIC microcontroller communication source program
     ORG     PIC54      GOTO    MAIN      ORG     0 ;---------------------- ;---------------------------- DELAY22                     MOVLW   D'200'               ; DELAY 2*250=500mS         MOVWF    COUNT1 DE32    MOVLW   D'250'                ; 8*250=2mS     MOVWF    COUNT2 DE42    NOP                          ; 1+2
[Microcontroller]
Understand the technical characteristics and advantages of PIC microcontrollers in one article
For electronic engineers, PIC microcontroller is a very important product. PIC microcontroller is an integrated circuit used to control peripheral devices, a CPU with decentralized (multi-tasking) function. Compared with humans, the brain is the CPU, and the shared part of PIC is equivalent to the human nervous system
[Microcontroller]
Understand the technical characteristics and advantages of PIC microcontrollers in one article
Serial port reception problems encountered by PIC microcontrollers
When using the serial port of the PIC16F1825/9 chip to receive data, a problem is encountered. When the serial port receives data quickly, the serial port may freeze. That is, the program runs normally, the serial port sends data normally, but the serial port cannot receive data. After consulting the PIC data manual,
[Microcontroller]
Serial port reception problems encountered by PIC microcontrollers
PIC MCU DS18B20 Temperature Alarm Program and Simulation
1. Use direct-insertion digital temperature sensor DS18B20;  2. This program only displays the positive part, please solve the negative part by yourself, and the accuracy is reserved to the last two digits;  3. Using the skip ROM instruction of DS18B20 does not support the acquisition of multiple sensors on a sing
[Microcontroller]
PIC MCU DS18B20 Temperature Alarm Program and Simulation
The first routine for the PIC microcontroller
I spent a long time looking for software online today. I downloaded MPLAB from the official website, but it was troublesome to register with PICC. The software used today is Protues simulation.   The first program lights up a lamp. The circuit is very simple. The program is posted below.   #include pic.h //Include h
[Microcontroller]
Design of water temperature and water level controller based on PIC16C72 single chip microcomputer control
Solar water heaters are rapidly entering thousands of households due to their advantages of energy saving, environmental protection, and low cost. The water temperature and water level controllers that are matched with solar water heaters are very convenient for users to use. The product has a good market prospect and
[Microcontroller]
Design of water temperature and water level controller based on PIC16C72 single chip microcomputer control
PIC16F877A Capture Mode
/* In Capture mode, after an external CCP1 event is triggered, CCPR1H and CCPR1L will obtain the values ​​in TMR1H and TMR1L of Timer1. For the trigger event of CCP1, set the corresponding bits CCP1M3--CCP1M0 in the key CCP1Con. CCP1IE enables interrupts. When an interrupt occurs, the CCP1IF bit is set to 1 and
[Microcontroller]
PIC microcontroller keyboard program
;The PIC MCU keyboard program uses the "level change interrupt" of PORTB to input the keyboard, so that when the K1 key is pressed, the first digital tube displays 1, when the K2 key is pressed, the first digital tube displays 2, when the K3 key is pressed, the first digital tube displays 3, and when the K4 key
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号