Mbed Course Notes - Timers and Interrupts
Mbed Course Notes - Timers and Interrupts
Mbed Course Notes - Timers and Interrupts
These course notes are written by R.Toulson (Anglia Ruskin University) and T.Wilmshurst (University of Derby). (c) ARM 2012 These course notes accompany the textbook Fast and effective embedded system design : Applying the ARM mbed
A automotive system needs to be able to respond rapidly to a crash detection sensor in order to activate the passenger airbag
Interrupts allow software processes to be halted while another, higher priority section of software executes Interrupt routines can be programmed to execute on timed events or by events that occur externally in hardware Routines executed by events that occur from an external source (e.g. a mouse click or input from another program) can be referred to as event driven.
An introduction to timers
Interrupts in embedded systems can be thought of as functions which are called by specific events rather than directly in code.
The simplest type of interrupt is one which automatically increments a counter at a periodic interval, this is done behind the scenes while the software is operating. Most microcontrollers have built in timers or real-time-interrupts which can be used for this purpose.
The main code can then be executed at specified time increments by evaluating the counter value.
For example, we can set some pieces of software to operate every 10ms and others to operate every 100ms. We call this scheduled programming.
There is no point in executing both the injection management and the fuel level management systems at the same rate.
// toggle output1
// toggle output2
Note: You will need to define a second timer object, digital output and task function prototype.
10
attach
attach attach_us attach_us detach
11
// flip 2 function
12
Hardware interrupts
Microprocessors can be set up to perform specific tasks when hardware events are incident.
This allows the main code to run and perform its tasks, and only jump to certain subroutines or functions when something physical happens.
i.e. a switch is pressed or a signal input changes state.
Interrupts are used to ensure adequate service response times in processing. The only real disadvantage of interrupt systems is the fact that programming and code structures are more detailed and complex.
13
InterruptIn
rise rise fall fall mode
Note: any digital input can be an interrupt except pin 19 and pin 20
14
You may notice some issues with this simple program, what are they?
15
It is therefore easy to see how a single button press can cause multiple interrupts and hence the LED can get out of synch with the button. We therefore need to debounce the switch with a timer feature.
16
// only allow toggle if debounce timer // has passed 200 ms // restart timer when the toggle is performed
17
An example of a classic hardware debouncer would be two cross-coupled NAND gates form a very simple Set-Reset (SR) latch. Another example of a software debouncer would be to look for a number of sequential readings of the switch, e.g. if the input changes from 0 to 1 and then continues to read 1 for the next ten samples then the switch has been pressed.
18
Extended exercises
Exercise 6: Using an oscilloscope evaluate the debounce characteristic of your pushbutton. What is the ideal debounce time for your pushbutton? Note that longer debounce times reduce the capability for fast switching, so if fast switching is required a different type of pushbutton might be the only solution Exercise 7: Combine the timer and hardware interrupt programs to show that a scheduled program and an event driven program can operate together. Flash two LEDs at different rates but allow a hardware interrupt to sound a buzzer if a pushbutton is pressed. Exercise 8: Accelerometer chips such as the ADXL345 have interrupt output flags to enable an interrupt based on an excessive acceleration (as used in vehicle airbag systems). Investigate and experiment with the ADXL345 interrupt feature to sound a buzzer when a high impact is seen.
19
Summary
Time and event management in embedded systems An introduction to timers Using the mbed Timer object Using multiple timers Using the mbed Ticker object Hardware interrupts External interrupts on the mbed Switch debouncing for interrupt control Extended exercises
20