Interrupt sources in 8-bit PIC and how to quickly attend them

Interrupt sources in 8-bit PIC and how to quickly attend them

Interruption by hardware takes care of emergent situations that requires attention as soon as possible. The code is interrupted, the situation is attended, and finally is taken back to previous position. Therefore, they make a great choice for reliable programming without complicated instructions. In this post, you will learn how to attend some of the many interrupt sources available with examples from 8-bit PIC microcontrollers.

Business vector created by freepik – www.freepik.com

Where could they be used?

Interrupts can used in a wide variety of applications. They attend situations like:

  • a counter has reached a threshold.
  • a push button has been pressed.
  • a communication bus has transmitted new information.
  • Etc (many more interesting in fact).

In such cases, the microcontroller MUST stop executing whatever it was doing and accordingly respond to the tasks mentioned.

Numeric pad. Respecfully borrowed from Wikipedia.

Interrupt Sources

8-bit PIC microcontrollers provide many sources of interrupts. Moreover, it varies from device to device. Here are a few from popular 8-bit microcontrollers:

  • PIC12F683: 10 registers with 4 – 8 sources each.
  • PIC16F628A: 10 sources.
  • PIC16F684: 11 sources.
  • PIC16F877A: 15 sources.
  • PIC18F4550: 10 registers with 4 – 8 sources each.

Enabling the Interrupt Sources

Global Interrupts

To enable all available in the PIC microcontroller, activate the following instructions:

PEIE_bit=1; //Peripheral Interrupts
INTCON.GIE=1; //Global Interrupts (One Switch to control them all)

To disable them, write a 0 instead of the 1.

Specific Interrupts

Find the Enable Bit that you specifically want to use. For this example, Timer 1 and Port B will be explained.

TMR1IF_bit=0; //The flag should be erased before it is enabled.
TMR1IE_bit=1; //Enable Timer 1

RBIF_bit=0; //The flag should be erased before it is enabled.
RBIE_bit=1; //Enable Port B

The Function that attends the Interrupt Sources

If you don't know what Functions are, click in this link to learn more about it.

Before the Main function, write down this function called interrupt(). As you might have guessed, is the place where they will be attended whenever an interrupt flag have been raised. Moreover, from here another function could be also called.

void __interrupt(void) interruptFunction() {

}

Disable the Global Switch

Avoid having 2 or more interrupts enabled at the same time. To do so, disable all of them from happening in the first place. Afterwards when it has been attended, reenable all of them.

void __interrupt(void) interruptFunction() {
  INTCON.GIE=0; //disable global interrupts

  //Interrupt Sources

  INTCON.GIE=1; //enable global interrupts
}

Attend individually each one

The following lines of code have to be adapted to each of the interrupt sources. I will use the Timer 0 as example.

Which one was actually triggered? 2 previously set bits needs to be checked in order to find it out. If both are true, then we know for sure which one was the cause.

  • TMR0IE_bit
  • TMR0IF_bit

Once the condition has been proved, disable the Timer 0 and go straightforward to the function in charge of the task assigned. Here I call it Routine_Tmr0() but you can name it as you want. Afterwards, the code will return to the Interrupt function. Turn off the flag, since the code have already attended it and re-enable the interrupt of Timer 0.

if(TMR0IE_bit && TMR0IF_bit) {
  TMR0IE_bit=0; //Disable Timer 0 interrupt
  Routine_Tmr0();
  TMR0IF_bit=0; //Once attended, erase the interrupt flag from Timer 0
  TMR0IE_bit=1; //Enable Timer 0 interrupt
}

Notice that in the IF condition, TMR0IE_bit is equal to TMR0IE_bit==1

Conclusion

To summarize, Interrupts helps the PIC microcontroller to attend emergent situation quickly. Furthermore, some Interrupt sources were explained how do they work and how to enable them. Finally, a function to attend the them was written in detail.

Further reading

To read more about the beginner's guide to 8-bit PIC microcontrollers, refer to the following posts:

How to drive 7-Segment Displays with a 8-bit PIC - Learn how to drive 7-segment displays using the PIC16F684 microcontroller. Download the code and watch an example. Click here to…
Multiplexing Traffic Lights easily in a PIC microcontroller - Using the 8-bit PIC16F684, a circuit for multiplexing traffic lights is assembled in a breadboard with simple LEDs. Click here…
Timer 1, how to make a reliable Real Time Clock in 8-bit PIC - In this tutorial, a Real Time Clock RTC is done using Timer 1 and the 32768Hz oscillator in a PIC…
Timer 0 Timer 0 as External Counter in 8-bit PIC microcontroller - Timer 0 can also be set as external counter. This way it can act upon reaching a certain threshold. Click…
Timer 0 Timer 0, a simple Counter in 8-bit PIC you need to know - Timer 0 is a counter that is always working and it's used commonly to refresh information on screens. Click here…
Interrupt sources in 8-bit PIC and how to quickly attend them - Interrupts are the critical way to manage emergent situations in a PIC microcontroller. Click here to read to learn the…
Code Template Code Template, an easy guide for PIC Microcontrollers in C - Writing a program with order is a time saver. Download today the code template for PIC microcontrollers for free. Click…
Blinking LED with a PIC microcontroller, a helpful Indicator - Learn how to continuously turn on and off a simple LED; in other words, to blink a LED. The microcontroller…
Hello World with 8-bit PIC microcontroller - Hello World is the first code in any programming language and now it's applied to a PIC microcontroller. Click here…
PICKIT 4 How to connect any PICkit to a microcontroller - PICkits are the tools for programming PIC microcontrollers. Do you want to learn how to connect a PICkit? Click here…

You have reached this far!

Thank you for reading the blog post. Your comments and suggestions are welcomed. At the bottom of this page, leave a message or just say hi! The whole team of techZorro will appreciate it. Don't forget to share it on social media as well.

techZorro’s Index of Content

Click on the following link to browse likewise content in the blog in techZorro. This index will help you see what you are looking for in a bird’s eye view.

techZorro's Newsletter!

If you enjoyed this blog post, please subscribe to techZorro’s newsletter so you don’t miss any future blog posts!

The next posts will reveal how to use many of these interrupt sources, such as Timers, External INT, etc.

You have reached this far!

Thank you for reading the blog post. Your comments and suggestions are welcomed. At the bottom of this page, leave a message or just say hi! The whole team of techZorro will appreciate it. Don't forget to share it on social media as well.

techZorro’s Index of Content

Click on the following link to browse likewise content in the blog in techZorro. This index will help you see what you are looking for in a bird’s eye view.

techZorro's Newsletter!

If you enjoyed this blog post, please subscribe to techZorro’s newsletter so you don’t miss any future blog posts!

techZorro's Index of Content

Keep Reading!

One Comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.