Interrupts Short and Simple - Part 1 - Good Programming Practices - Embedded
Interrupts Short and Simple - Part 1 - Good Programming Practices - Embedded
Sign In Sign Up
Editor's note: In this first part in a series on the appropriate use of interrupts in
embedded systems design , Priyadeep Kaur of Cypress Semiconductor starts with
general guidelines and good practices that should be followed.
Interrupts must be carefully and cautiously handled, mainly because carelessly written
interrupts can lead to some mysterious run-time errors. These errors are difficult to
uncover and understand since the controller might enter into an undefined state, report
invalid data, halt, reset, or otherwise behave in an incomprehensible manner. Being
cognizant of some simple interrupt handling practices can help us prevent such events.
In this series of articles, we discuss, with relevant examples, the following simple yet
important interrupt-handling nuggets that help prevent such errors, including:
1 of 5 9/12/2017, 10:19 AM
Interrupts short and simple: Part 1 - Good programming practices | Emb... http://www.embedded.com/design/programming-languages-and-tools/43...
bit flag;
void ISR(void)
{
flag=1;
}
void main()
{
--
--
while(1)
{
--
--
/* Wait for the ISR to set the
* flag; reset it before
* taking any action. */
if (flag)
{
flag = 0;
/* Perform the required action here */
}
}
}
2 of 5 9/12/2017, 10:19 AM
Interrupts short and simple: Part 1 - Good programming practices | Emb... http://www.embedded.com/design/programming-languages-and-tools/43...
#define PERIOD_10ms 0x01
#define PERIOD_14ms 0x02
#define PERIOD_19ms 0x03
void Timer_ISR(void)
{
static char State = PERIOD_10ms;
switch(State)
{
case PERIOD_10ms:
{
// Toggle pin;
// Timer Stop;
// Change period to 14ms;
// Timer Start;
break;
}
case PERIOD_14ms:
{
// Toggle pin;
// Timer Stop;
// Change period to 19ms;
// Timer Start;
break;
}
case PERIOD_19ms:
{
// Toggle pin;
// Timer Stop;
// Change period to 10ms;
// Timer Start;
break;
}
default:
{
/* Timer_ISR entered undefined state */
// Make default period 10ms
break;
}
}
}
3 of 5 9/12/2017, 10:19 AM
Interrupts short and simple: Part 1 - Good programming practices | Emb... http://www.embedded.com/design/programming-languages-and-tools/43...
ASPENCORE NETWORK
EBN (http://www.ebnonline.com/) EDN (http://www.edn.com/) EE Times (http://www.eetimes.com/) EEWeb (https://www.eeweb.com/) Electronic Products
(http://www.datasheets.com/) Embedded Control Europe (http://www.embedded-control-europe.com/) Embedded Know How (http://www.embedded-know-how.com/ ) Embedded News
(http://embedded-news.tv/) IOT Design Zone (http://iot-design-zone.com/) Motor Control Design (http://motor-control-design.com/) Electronics Know How (http://electronics-know-how.com/)
4 of 5 9/12/2017, 10:19 AM
Interrupts short and simple: Part 1 - Good programming practices | Emb... http://www.embedded.com/design/programming-languages-and-tools/43...
GLOBAL NETWORK
EE Times Asia (http://www.eetasia.com/) EE Times China (http://www.eet-china.com/) EE Times India (http://www.eetindia.co.in/) EE Times Japan (http://eetimes.jp/) EE Times Taiwan
(http://www.eettaiwan.com/) EDN Asia (http://www.ednasia.com/) EDN China (http://www.ednchina.com/) EDN Taiwan (http://www.edntaiwan.com/) EDN Japan (http://ednjapan.com/)
Working With Us: About (http://www.aspencore.com/) | Contact Us (http://mac.aspencore.com/AspenCore_Contact-Us.html) | Media Kits (http://mac.aspencore.com/AspenCore_Media-Guide.html)
Terms of Service (http://www.aspencore.com/terms-of-use) | Privacy Statement (http://www.aspencore.com/privacy-policy) | Copyright ©2017 AspenCore All Rights Reserved
5 of 5 9/12/2017, 10:19 AM