2 phan cung
TRANSCRIPT
Programming embedded systems
Seminar 2
HARDWARE FOUNDATIONS
Dr. Thanh Hung Tran Department of Automation Technology,
College of Engineering, Can Tho UniversityEmail: [email protected]
Review: AT89S52
Low-voltage, high-performance microcontroller:
• 32 programmable I/O lines (in 4 ports)• 8K Bytes of In-System Programmable (ISP)
Flash Memory (10,000 Write/Erase Cycles )• 256 x 8-bit Internal RAM • Up to 64K Bytes optional external memory
Review: AT89S52
• Three 16-bit Timer/Counters • Eight Interrupt Sources • Full Duplex UART Serial Channel • Low-power Idle and Power-down Modes • 4.0V to 5.5V Operating Range • Fully Static Operation: 0 Hz to 33 MHz • Watchdog Timer
AT89S52 Pin Configurations
Review: Reading and writing ports
#include <Reg52.H>void main (void){unsigned char Port1_data;P1 = 0xFF; // set Port1 as inputwhile(1) { Port1_Data = P1; // Read the value of P1 P2 = Port1_Data; // Copy the value to P2 }}
Outline
How to build the first embedded system with AT89S52?
• Oscillator circuit
• Reset circuit
• Controlling outputs (LEDs)
• Reading inputs (SWs)
Seminar objectives
At the end of this seminar, by referring the lecture notes, students will be able to:
• know how to build the hardware for an embedded system, based on AT89S52
• understand issue of stability• write programs to access inputs and control
outputs
Oscillator circuit
Why oscillator circuit is needed for embedded systems?
- It drives embedded/computer systems- It is the “heartbeat” of the system. All operations
of the system follow this beatWhat happen if the oscillator fails or run incorrectly?- If the oscillator fails: the system will stop working- If the oscillator run incorrectly: any timing calculations
done by the system will be inaccuracy
Oscillator circuit
• Most of microcontrollers use Pierce oscillator, a very popular oscillator, because it requires a minimum of components.
• Most the components required for the oscillator were integrated on the microcontroller.
You need 1 crystal and 2 small capacitors to form an oscillator.
Oscillator circuit
AT89S52 has many options for oscillator circuit:
• Crystal oscillator• Ceramic resonator oscillator• External clock source (RC
oscillator,…)
Which one do you want to choose?
Stability issues
• The key factor in selecting an oscillator for an embedded system is the issue of oscillator stability (how much of error).
• Frequency tolerance of an:- crystal oscillator: 0.001%- ceramic resonator oscillator: 0.5%- RC oscillator: 20%What is the practical meaning of these
numbers?
Stability issue
• If the oscillator runs for 1 year, how many minutes may it gain or lose?
• Which oscillator do you want to choose?
Oscillator types
Error (%)
minutes in 1 year
Error/year (minutes)
Error/year (hours)
Crystal oscillator
0.001% 525,600 5.26
Ceramic resonator
0.5% 525,600 2,628 43.8
RC oscillator
20% 525,600 105,120 1,752
Oscillator frequency selection
• The maximum frequency that AT89s52 can run is 33 MHz (2.75 MIPS: Million Instructions Per Second)
• Which frequency do you want to chose for your embedded system?
• Do you know that:- Many applications do not require high speed- More speed, more power consumption- Accessing to low-speed peripherals (LED,…) is much
easier if microcontroller runs at low speed, low cost
Keep the clock frequency as low as possible!
Reset circuit
• Why do you need to reset the microcontroller? - To put the hardware to its initial state- To start a program at the beginning• What happen if the microcontroller do not reset
correctly?- The hardware is not in its initial state- The microcontroller may run anywhere, not
only your program, but also random codes
Reset circuit
• The AT89S52 has 3 sources of reset:
- Power-on Reset:
- External Reset:
- Watchdog Reset: Automatic reset if program stops
Controlling DC loads
• The 89S52 port pins can be set to 0V or 5V
• Each pin can sink or source a current up to 10mA• BUT total current for one port:P0=26mA, other=15mA• How about controlling larger loads (current > 10mA)?
R = ? If I = 10mA
P0 = 0x0F; // set 4 high bits to 0V, 4 low bits to 5V
I
Controlling large DC loads
• Need a buffer between port pin and the load• The buffer supports current for the load• Buffer selection: depend on required current
Exercise 2.1
1. How to turn on an LED connected to P0.0?• Open BLINKY project• Start debugging• Click Run• Observe Port pins while the program running• Click Reset 2. What happen with the Port pins and the program?
Reading switches
• Why do you need to read switches?- Embedded systems usually use switches as their user
interface, to know what users want- Same rule applied for any system, from very simple to
very complex one
Reading switches
• How to connect switches to the microcontroller?
- If SW pressed, pin = 0- If SW not pressed, pin = ? Need a pull-up resistor
- If SW pressed, pin = 0- If SW not pressed, pin = 1
Reading switches
• How to read the switch status?
• How to how which switch pressed?
• Problem?
unsigned char Port_Data;P1 = 0xFF; // set Port 1 as inputPort_Data = P1; // read Port 1 pins
if (P1.0 == 0) //switch connected to P1.0 pressed?{
//your code}
Reading switches
#include <Reg52.H>sbit K1 = P1^0;void main (void){while(1) { if (K1 == 0) //switch K1 pressed?
{ //your code
} }
}
Exercise 2.2
• Xây dựng phần cứng hệ thống nhúng theo các bước sau (trên project board):
1. Mắc mạch nguồn: Tạo điện thế 5V từ pin 9V
R 13 3 0 O h m
L M 7 8 0 51
2
3V I
GN
D
V O
1 0 u F
D 1
L E D
9 V
B A TTE R Y
V c c
Exercise 2.2
2. Mắc mạch dao động và reset
Exercise 2.2
3. Mắc mạch hiển thị LED ở Port 0 và 4 SW0-SW4 ở Port 1
S W 31 2
S W 01 2
S W 21 2
S W 11 2
+
V c c
R 21 0 k
V c c
1 0 u F
33p
Exercise 2.3
• Write a program to:- Count the number of times that SW0 is pressed - Display the number on LEDs on Port 0- Clear the number when SW3 is pressed.
How many time does the program count for one press?• Modify the program to count only one for one press
Dealing with switch bounce
• In practice, all mechanical switch contacts bounce after the switch is closed and opened
Counting the number of times that a SW is pressed becomes extremely difficult.
press release
Ideal SW
Real SW
Dealing with switch bounce
• By hardware:
Use RS latch Use RC
Dealing with switch bounce
• By software:1. Read the relevant port pin2. If a switch depression is detected, wait for 20ms and
then read again3. If the second reading confirms the first reading,
assume that the switch was pressed
2nd read1st read
Dealing with switch long depression
• What happen if the user press a switch for too long?
1. Read the relevant port pin2. If a switch depression is detected, wait for 20ms and then
read again3. If the second reading confirms the first reading, wait until
the switch is released, assume that the switch was pressed