Interrupts short and simple: Part 1 - Good programming practices | Emb... http://www.embedded.com/design/programming-languages-and-tools/43...
Sign In Sign Up
Home (/) Programming Languages & Tools Development Centers (/development/programming-languages-and-tools)
Design How-To (/development/programming-languages-and-tools/articles)
Share 51 Tweet Like 32
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.
Any embedded application generally involves a number of functions. Even a simple
temperature control application, for instance, includes a number of tasks like reading the
user input, displaying the data on an LCD, reading the temperature sensor/ADC output,
and controlling the fan/heater output. The controller’s bandwidth is to be divided among all
these tasks in such a way that, to an end user, the functions seem to be executed in
parallel. Designing this involves deciding a background process - i.e., the main process for
the controller - and interrupting the controller at regular intervals for all other tasks. Note
that there may be asynchronous interrupts as well, such as a master trying to
communicate with the slave controller on an as-needed basis. Proper interrupt handling
thus becomes a critical task.
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;
#pragma interrupt_handler ISR
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;
}
}
}
Share 51 Tweet Like 32
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...
all articles (/rss/all) category ▼
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.electronicproducts.com/) Electronics-Tutorials (http://www.electronics-tutorials.ws/) Embedded (http://www.embedded.com/) Planet Analog (http://www.planetanalog.com/)
ElectroSchematics (http://www.electroschematics.com/) Power Electronics News (http://www.powerelectronicsnews.com/) TechOnline (http://www.techonline.com/) Datasheets.com
(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/)
ESM China (http://www.esmchina.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