Presentation is loading. Please wait.

Presentation is loading. Please wait.

Interrupts, Tasks and Timers

Similar presentations


Presentation on theme: "Interrupts, Tasks and Timers"— Presentation transcript:

1 Interrupts, Tasks and Timers
An embedded system have to respond to events where timing is a key consideration, eg to measure time durations to generate time-based events, which may be single or repeating to respond to external events at an appropriate speed, and which may not happen at predictable times In doing some combination of these, the system may find that it is being required to do more than one thing at the same time. The system has to be able to prioritise, and so choose which action to undertake first. So, methods to control time-based activity is required.

2 Interrupts, Tasks and Timers
Timer – some circuit to measure time, and so initiate an event when a certain time has elapsed Interrupt – a method that allows a program to be suspended, so that the CPU can deal with a certain occurance, and once this has been dealt with, the original program can continue to execute as required. Tasks – are distinct activities that are performed, each of which has a block of code that is executed. As the number of tasks increases, we need to be able to develop techniques to handle them.

3 Interrupts, Tasks and Timers Polling
An example of an event driven task would be when a user pushes a button – can handle this by continuously monitoring the external input. This is an example of polling Respond to button 1 Y Downside is when we need to monitor for very many events. Is button 1 pressed ? N Also, the CPU cannot perform any other tasks during a Y Respond to button 2 Is button 2 pressed ? polling routine. And all inputs are treated equally, so a response to an urgent event may be delayed until a relatively insignificant number of potential inputs are screened N Do other stuff

4 Interrupts, Tasks and Timers
An alternative approach is to design the hardware so that an external input can effectively demand attention and stop the CPU from what it is currently doing. An example – suppose you had agreed to meet a friend at the bus station, the bus is running late and you don’t want to leave your friend waiting around once they finally arrive. Every 5 minutes, you could check on the live arrivals website of the bus station (effectively a polling type activity) or you can get your friend to call you when the bus leave the motorway and is 10 minutes away from its final destination. They you can carry on doing other things, until you get the call – this is an interrupt. In responding to interrupts, most microcontrollers follow a general pattern

5 Load Context, including PC
Interrupts, Tasks and Timers Interrupt detected Main Program is running CPU completes the current instruction – it is about to execute a different piece of code, so must save key information so that it can continue after servicing the interrupt – this is the context. Program Counter points to where the the CPU should return to. All info is saved on the Stack – small piece of memory. Interrupt Vector points to the location of the code to service the interrupt. Complete Current Instruction Save “Context” on Stack, including Program Counter (PC) Find Interrupt Vector, and load PC Complete Current Instruction Interrupt Service Routine (ISR) starts N Once the Interrupt has been serviced, the CPU retrieves the information saved before dealing with the interrupt, and normal service is resumed. End of ISR ? Y Load Context, including PC from Stack Complete Current Instruction Main Program resumes

6 Interrupts on the mbed Can get more details on the mbed website
/* Simple Interrupt Example*/ #include "mbed.h" InterruptIn button(p5); DigitalOut led1(LED1); DigitalOut flash(LED4); //this will cause the interrupt void ISR1(){ led1=!led1; } //the interrupt service routine int main() { button.rise(&ISR1); //interrupt routine executes on the //rising edge of the input from p5 while(1) { flash=!flash; wait(0.25); }

7 Interrupts, Tasks and Timers
A bit more detail Most processors have a number of important interrupt mechanisms Interrupts can be prioritised – ie some are more important than others, and so if two interrupts occur at the same time, the higher priority interrupt executes first Interrupts can be masked – ie switched off if they are not needed, or are getting in the way. This can be temporary Interrupts can be nested – ie a higher priority interrupt can interrupt a lower priority. Can make programming complex iii) In the LPC1768, interrupts are controlled by the Nested Vectored Interrupt Controller (NVIC).

8 The NXP LPC 1768 Microcontroller Block Diagram

9 Interrupts, Tasks and Timers Timers
We have made extensive use of the wait() function in the lab, which is very useful, but whilst we are using this function, the microcontroller cannot perform any other activity. As in previous situations, with ADC, DAC, PWM etc, it is useful to be able to let timing go on in the background, whilst the program continues to do more useful stuff. How can this be done ? The Digital Counter, which we will look at later in the course Preload 1 CLK in Interrupt generated when counter overflows Read

10 Interrupts, Tasks and Timers Timers
Lets say clock is running at 100 MHz, and we have 32 bit counter, and can neglect propagation delay. Clock period is 10 ns. 32 bits means counts from 0 to 232-1 (4,294,967,295), and so would take seconds. Can also generate an interrupt when a given bit is set in the counter. As we have 10 nm precision, can get very accurate timing for synchronous data transfer for example. The LPC1768 has four general purpose timers, based on the above approaches.

11 If you change the number of characters and record the time taken,
/* Simple Timer Example*/ #include "mbed.h" Timer t; Serial pc(USBTX,USBRX); int main(){ t.start(); pc.printf("Greetings t.stop(); Earthling!\n"); pc.printf("The time taken was %f seconds\n", t.read()); } If you change the number of characters and record the time taken, can you work out what the data rate is ?

12 Interrupts, Tasks and Timers
Switch debounce Switches are mechanical and as the contact is made and broken, the output often swings between logic states for a short time after the switch is opened or closed. This can cause all sorts of problems

13 Interrupts, Tasks and Timers
Switch debounce If an interrupt was initiated by a user pressing a button, then could get into all sorts of problems due to this effect. Can be mitigated by detecting the first rising or falling edge, then masking any further interrupts for a given period of time, until the switch has “settled down” – can take millisecond timescales. Refer to the mbed website for additional background to understand the code on the next slide which performs switch debounce in software

14 /* Switch Debounce Example*/
#include "mbed.h" InterruptIn button(p10); DigitalOut led1(LED1); Timer debounce; Void toggle(void); //Interrupt on push button pin 10 //define timer debounce //function prototype int main(){ debounce.start(); //the address of the toggle function button.rise(&toggle); //is assigned to the rising edge } Void toggle(){ if(debounce.read_ms()>10) led1=!led1; debounce.reset(); //only allow toggle if //has passed 10 ms //restart timer after debounce timer toggle execution


Download ppt "Interrupts, Tasks and Timers"

Similar presentations


Ads by Google