Upload
kt8815
View
213
Download
1
Embed Size (px)
Citation preview
Programming embedded systems
Seminar 4
REAL-TIME CONSTRAINS
Dr. Tran Thanh Hung Department of Automation Technology,
College of Engineering, Can Tho UniversityEmail: [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: Time delay is not exactly 20 ms• Problem 2: What happen if the pin will not go high?
Problem 1
Problem 2
Problem 1: Precise time delay
• Run Example 3.1 on debug mode• Click on (Performance Analyzer Window) • Right-click, chose setup PA• In Function symbols area, double-click on
delay, click Define and then Close• Click on delay symbol to see the time that
delay takes. Is that correctly 20ms?
Problem 1: Precise time delay
• Software delays are not accurate enough for some applications.
• How to create precise time delays?Use hardware: Timers
• AT89S52 has 3 16-bit timers: Timer 0,Timer 1, Timer 2These timers can be used to create accurate time delays.
Note: Each timer has many functions. We only consider the function for measuring time in this seminar.
Problem 1: Precise time delay
• Run any of your program in simulator, toggle Disassembler window• Chose PeripheralTimerTimer 0 • Chose Mode 1• Check TR0 • Click to run the program step by step• Look at while the program is running• What do you see?• Now set TH0 TL0 = FF F1• Click until TH0 TL0 = FF FF• Look at TF0 while clicking one more time. • What happen with TF0 and TH0 TL0?
Problem 1: Precise time delayvoid delay_t0(void){ TMOD &= 0xF0; TMOD |= 0x01; //set Timer 0 mode 1
TH0 = 0; TL0 = 0; //set initial value of TimerTF0 = 0; //make sure overflow flag off
TR0 = 1; //start Timer 0 while(TF0==0); //wait Timer 0 overflow (TF = 1) TR0 = 0; //stop Timer 0
}• How long does it take to run this function?• If OSC_FRE = 12000000, OSC_PER_INTS = 12• Timer 0 run at a speed: (OSC_FRE/OSC_PER_INTS)=1.000.000 times/second• 1 increment of timer takes 1/1.000.000 seconds = 1 s• 65536 increments take 65536 s = 65,536 ms
• If you want a shorter delay…?
Problem 1: Precise time delay
void delay_t0(tWord x){ TMOD &= 0xF0; TMOD |= 0x01; //set Timer 0 mode 1
x = 65536 - x;TH0 = x/256;TL0 = x%256; //set initial value of TimerTF0 = 0; //make sure overflow flag off
TR0 = 1; //start Timer 0while(!TF0); //wait Timer 0 overflow (TF = 1) TR0 = 0; //stop Timer 0
}• How long does it take to run this function?• 1 increment of timer takes (OSC_PER_INTS/OSC_FRE) * 103 ms• x increments take x *(OSC_PER_INTS/OSC_FRE) * 103 ms
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(20); //debounce
if (K1 == 0) //K1 still pressed? { while (K1 == 0); return_value = 1; } }
• 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 ?
Problem 1
Problem 2{ delay_t0_ms(20);
Problem 2: Time-out mechanisms
• Set a time-out mechanism for waiting an event
tWord time_out_loop = 0;
if( K1 == 0 ){ delay_t0_ms(20); if(K1 == 0 )
{ while (K1 == 0 & (++time_out_loop !=0) ); return_value = 1;}
}
• How to know the whether SW released or time out?
Problem 2: Time-out mechanisms
tWord time_out_loop = 0;
if( K1 == 0 ){ delay_t0_ms(20); if(K1 == 0 )
{ while (K1 == 0 & (++time_out_loop !=0) ); if (K1 == 1) return_value = 1; }
}
• How do you change the time for time-out?
Problem 2: Time-out mechanisms
#define time_out 10000
tWord time_out_loop = 65536 - time_out;
if( K1 == 0 ){ delay_t0_ms(20); if(K1 == 0 ){ while (K1 == 0 & (++time_out_loop !=0) ); if (K1 == 1) return_value = 1;}}
• How do you set the time for time-out?
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)
Note: Do not wait for timer overflow in this function
• Put this function to file delay.c and delay.h• Use this function to create a time-out mechanism in the
program for reading switch (exercise 3.1)
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