Académique Documents
Professionnel Documents
Culture Documents
Seminar 5
Review
In the previous seminars, we use a super loop to run a task. In this seminar, we will use a different way to run a task.
Outline
How to create an Embedded OS for embedded applications?
Periodic functions Implement periodic functions Creating Embedded OS Using Embedded OS
Seminar objectives
At the end of this seminar, by referring the lecture notes, students will be able to: understand issue of periodic function use timer to create a simple Embedded OS (EOS) implement periodic functions running in EOS
With this architecture, it is very difficult to run the task at a precise interval of time.
Periodic function
There are many applications require to run tasks repeatedly, at precise intervals
Keypad need to be scanned every 200ms The display (PC monitor, LCD) must be refreshed 60 times every second Music must be sampled at 44.100 times per second Temperature sensors must be sampled once per second
In practice, many embedded systems must be able to support this type of periodic function
Periodic function
If you need to run the function X() every 50 ms, how do you implement? If you know the function takes 10 ms to complete
while(1) //super loop { X(); delay(40); }
This approach will be fine, if: 1. We know the precise duration of X() 2. This duration never varies
Periodic function
while(1) //super loop { X(); delay(40); }
However, 1. It is very difficult to know the duration of X() 2. This duration often vary 3. This approach is in inefficient: waste in a delay loop
Is there any way to implement periodic function, running at precise 50 ms interval ?
Instead of waiting for timer overflow, you can setup an interrupt from timer
Exercise 1
Put Timer0_interrupt_init() and Timer0_ISR() into file Interrupt.c and Interrupt.h Modify the program in Exercise 4.3, using Timer0_interrupt_init and Timer0_ISR()
It takes time to reload the initial value for the timer. Sometime you forget to reload the value. This approach is not adequate if the system requires very precise timing
void EOS_init(unsigned int x) { unsigned int _count = 65536 - x; RCAP2H = _count/256; RCAP2L = _count%256;//set reload value TH2 = RCAP2H; TL2 = RCAP2L;//set initial value of Timer EA = 1; ET2 = 1; //enable timer 2 interrupt T2CON = 0x04; //set Timer 2 as 16 bit timer, start timer }
EOS_ISR() interrupt INTERRUPT_Timer_2_Overflow { TF2 = 0; Your_function(); //This function will be called every 50 ms }
Example 5.1
#include <main.h> #include <delay.h> void main (void) { while(1) { P0=~P0; // delay_t0(50000); } }
Run Example 4.1 in simulator Use Performance Analyzer (PA) to see the percentage of time that delay_t0 taken How many percent?
Example 5.2
#include "Main.h" #include "EOS.h" void main (void) { EOS_init(50000); while(1) // Super Loop { go_to_sleep(); // Enter idle mode to save power } }
EOS_ISR() interrupt INTERRUPT_Timer_2_Overflow //in file EOS.c { TF2 = 0; // Put your code here P0 = ~P0; }
Run Example 5.2 in simulator Use Performance Analyzer (PA) to see the percentage of time that go_to_sleep() taken How many percent?
Exercise 2
Put EOS_init() and EOS_ISR() into file Interrupt.c and Interrupt.h Modify the program in Exercise 5.1, using EOS Only run the time if the switch connected to P1.0 is pressed. Reset the time if the switch connected to P1.3 is pressed.