Introduction to PIC Microcontrollers: Interrupts image 19

Introduction to PIC Microcontrollers: Interrupts


Introduction to PIC Microcontrollers: Interrupts image 13

“Do something today that your future self will thank you for.”

— Ashraf Said

In this article, interrupts are discussed in detail along with some programming tips for PIC microcontrollers.

before we start about this Article come check our new videos on our Youtube Channel :

EDUCATIONAL ENGINEERING TEAM

Proceeding on our Topic:

Overview of Interrupts

What is an interrupt?

Interrupts are a basic way to manage the flow of your code. In order to begin using interrupts, you need to first understand what they are and how they work. An interrupt occurs when something happens that requires your attention.

Interrupts Materials:

HARDWARE

  • PIC Programmer (PICKIT3 clone will suffice)
  • PIC16F819
  • 5.6k Resistor
  • 680 Ohm Resistor
  • tactile switch
  • buzzer
  • LED
  • wires

SOFTWARE:

  • XC8
  • MPLAB X

TOOLS:

  • WINDOWS 7 or later PC (ideally)

We discussed how to program PIC16 devices using MPLAB X and XC8 in the earlier articles. In this piece, we’ll examine interruptions and how they might be helpful.

Schematic

Schematic

Introduction to PIC Microcontrollers: Interrupts image 14

Polling Alone Is Insufficient

Imagine a situation where a PIC device needs to perform an action when a digital signal is received on one of its pins. A while loop that examines an input pin and executes some response code if the pin reads a specific digital level might be used to quickly create this. This method of detection, however, has a few drawbacks:

  • Polling may result in blocking.
  • The signal might not be received

The PIC merely remains in the loop until it notices the signal when it polls the pin and determines that the desired input has not been met. As a result, no additional code can run until the input has been recognized, which could be wasteful. What about quick external signals if that isn’t annoying enough? Input pulses that are genuine may not be recognized if the input signal on the PIC’s pin is too fast, in which case they will be disregarded. How then do we avoid this? Is it possible to identify inputs and other events so that our PIC can perform other duties while it waits? Interrupts are useful in this situation!

Using Interrupt with PIC Microcontroller – MikroC
Jun 28, 2012 PIC Microcontroller consists of both Hardware and Software Interrupts. If the interrupts are generated by external hardware at certain pins …
 

Interruptions’ Function

The majority of PICs—if not all—have an attribute known as interrupts, which compels the PIC to do a certain task whenever an interrupt signal is detected. Consider the previous scenario where the PIC must do a particular task in response to the detection of an external signal.

We can define an interrupt procedure that executes whenever an interrupt is received in place of polling. We can next check to see if an external signal was noticed inside of this interrupt service procedure (ISR). If so, the PIC can be programmed to execute some response code; otherwise, the interrupt can be ignored (since it could have come from another source that we are not interested in). Since the hardware and interrupt procedure we defined handle all of the handlings, polling is not done at all in the main code.

You must realize, however, that the interrupt service routine and the PIC’s main code do not execute simultaneously; rather, the PIC stops executing the main code, executes the interrupt service routine, and then starts executing the main code. Since interrupt service routines block the main code when they execute, they should be as brief and quick as possible.

While some PIC devices have interrupt priority, we will limit our discussion to the PIC16F819 for the time being.

The Interrupt Service Routine in Coding (ISR)

We can now program a simple ISR for the PIC16F819 using XC8 because we have a fundamental understanding of how interrupts operate. The cool thing about XC8 is that it takes care of a lot of the small details, like context saving, so we only have to worry about configuring interrupts and defining how the PIC will respond to an interrupt. The first step is to enable peripheral interrupts as well as the PIC’s interrupt mechanism. The most crucial interrupt data is stored in a register called INTCON, which is present in the majority of PICs. In contrast to PEIE, which is used to enable interrupts from peripherals (such as the INT pin, ADC, SPI, I2C, and I/O change), GIE is the bit that first enables interrupts.

Introduction to PIC Microcontrollers: Interrupts image 15

The INTE bit in the INTCON register must then be set in order to enable the INT pin interrupt. Pin 6 on RB0 contains the INT pin, which we also set to have input by making sure its TRIS bit is set (covered in a previous article).

The INTEDG bit, which is located in the OPTION REG register, is another one that we have control over. The interrupt will activate if this bit is set and the digital signal on the INT pin changes from a logical 0 to a logical 1. (rising edge). When this bit is cleared, the signal’s transition from a logical 1 to a logical 0 triggers an interrupt (falling edge).

Introduction to PIC Microcontrollers: Interrupts image 16

When the INT pin detects a rising edge signal once these bits have been configured, the PIC will now carry out an interrupt function. What code though? The ISR is where? This is where XC8 truly simplifies things. All we have to do is declare an interrupt function, and XC8 is smart enough to instruct the PIC where to store the code so that the PIC runs it when an interrupt occurs. An example of an empty interrupt service routine is shown below. It demonstrates how XC8 defines interrupt routines.

Introduction to PIC Microcontrollers: Interrupts image 17

This process would undoubtedly be problematic and empty. A flag bit is set when an interrupt is fired. These interrupt flag bits must be cleared when our ISR starts; otherwise, the ISR would restart as soon as it was finished (the flag bits are what cause the interrupt, after all). Therefore, the first duty in our ISR is to determine whether the INT flag has been raised (equal to 1). If so, we shall clear it before performing our response code.

All interrupt sources feature interrupt enable bits and interrupt flag bits; you can use the datasheet to determine which registers these bits are located in. In this example, when a rising edge is seen on the INT pin, we briefly activate a buzzer that is attached to RA0.

Introduction to PIC Microcontrollers: Interrupts image 18

EXAMPLES OF INTERRUPTS

 

The PIC performs the following duties in our example:

  • blinks an LED (in the main code)
  • when a rising edge signal is found on INT (RB0), it emits a brief beep (interrupt response)

The main characteristic of this program is that it continuously runs the LED code until an interrupt is detected, at which point the ISR is run. The PIC resumes regular operation and continues the LED flash sequence after the beep sound (the ISR) has finished. Keep in mind that even while the main code’s for-loop delays are in effect, the PIC can still detect interrupts. In this method, the delay won’t be able to prevent input signal detection.

interrupts