Programming Embedded Systems Seminar 4: Real-Time Constrains
Programming Embedded Systems Seminar 4: Real-Time Constrains
REAL-TIME CONSTRAINS
Dr. Tran Thanh Hung Department of Automation Technology, College of Engineering, Can Tho University Email: [email protected]
Review
In the previous seminar, we know how to develop software that can be easy to understand, maintain and reuse.
In this seminar, we will look on a new issue: real-time. Many embedded systems require real-time operation.
Outline
How to satisfy real-time constraints for embedded systems? Precise time delays Timeout mechanisms
Seminar objectives
At the end of this seminar, by referring the lecture notes, students will be able to: understand issue of real-time use timers to create accurate time delays implement mechanisms for time-out
Real-time problems
Look at the program for reading a switch:
if (K1 == 0) //switch K1 pressed? { delay(20); //debounce if (K1 == 0) //K1 still pressed? { while (K1 == 0); return_value = 1; } }
Problem 1 Problem 2
Problem 1: Time delay is not exactly 20 ms Problem 2: What happen if the pin will not go high?
Example 4.1
#include <main.h> #include <delay.h> void main (void) { while(1) { P1 = 0; // Turn LEDs on delay_t0(50000); P1 = 0xFF; // Turn LEDs off delay(50); } } Run this program in simulation, compare the time taken by delay and delay_t0 Maximum time-delay of delay_t0 = ? If you want a longer delay ?
Exercise 4.1
Write a delay ms function, using delay_t0
/******************************************************************************* * @fn delay_t0_ms(unsigned int _ms) * @brief Delay ms using delay_t0 (timer 0), independent MCU clock * @param _ms : number of ms needed to delay * @return void */
void timer_delay_ms(unsigned int _ms) Put functions delay_t0, delay_t0_ms in file delay.c and delay.h, so that these functions can be reused Modify exercise 3.1 to delay exactly 20ms (debound) Is that correctly 20ms?
Real-time problems
Look at the program for reading a switch:
if (K1 == 0) //switch K1 pressed? { delay_t0_ms(20); delay(20); //debounce if (K1 == 0) //K1 still pressed? { while (K1 == 0); return_value = 1; } }
Problem 1
Problem 2
Problem 1: Time delay is not exactly 20 ms Problem 2: What happen if the pin will not go high? How to solve Problem 2 ?
if( K1 == 0 ) { delay_t0_ms(20); if(K1 == 0 ) { while (K1 == 0 & (++time_out_loop !=0) ); if (K1 == 1) return_value = 1; } }
Exercise 4.2
Write a program to set up time in ms for time-out, using timer 0 void set_time_out_ms(unsigned int _ms)
Exercise 4.3
Write a program to send the running time to serial port in the following format: Hour : Minute : Second : x/10 second (Hour = 0-23, Minute & Second = 0-59, x = 0-9)
Notes: *Use the function delay_t0_ms in Exercise 4.1 to measure passing time and update Hour, Minute, Second, x *Use function printf() in <stdio.h> to send a string to serial port printf(Running time: %d : %d : %d : %d\n, Hour, Minute, Second, x); * Run the program in simulation, click on to see the time