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
Table of Contents
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.
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:
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!
- 008 – Variable Frequency Drives: how this controller can transform induction motors foreverAC induction motors can be transformed into a highly controllable machine with Variable Frequency Drives or VFD. Click here to listen.
- 006 – Regenerative Braking, an awesome Feature found in Electric MotorsThis episode is related to this hidden feature of electric motors called regenerative braking. Click here to listen.
- 005 – 7 types of Electric Motors that you should know aboutThere are several types of electric motors that differs in efficiency, power, cost, torque output, etc. Click here to listen.
- 004 – AC Voltages, Frequencies and Plugs around the WorldLet's talk about electricity! Specifically about how the standards around the world. Click here to listen.
- 002 – RISC vs CISC, how a few Differences are crucial to ComputingToday in the market is found two kinds of processor architectures: RISC and CISC. Both have some advantages. Click here to listen.
[…] you haven't read about the Interrupt Sources or how to handle Functions in C, check them out […]