2 phan cung
TRANSCRIPT
![Page 1: 2 Phan cung](https://reader033.vdocuments.mx/reader033/viewer/2022051709/577ccf2c1a28ab9e788f1256/html5/thumbnails/1.jpg)
Programming embedded systems
Seminar 2
HARDWARE FOUNDATIONS
Dr. Thanh Hung Tran Department of Automation Technology,
College of Engineering, Can Tho UniversityEmail: [email protected]
![Page 2: 2 Phan cung](https://reader033.vdocuments.mx/reader033/viewer/2022051709/577ccf2c1a28ab9e788f1256/html5/thumbnails/2.jpg)
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
![Page 3: 2 Phan cung](https://reader033.vdocuments.mx/reader033/viewer/2022051709/577ccf2c1a28ab9e788f1256/html5/thumbnails/3.jpg)
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
![Page 4: 2 Phan cung](https://reader033.vdocuments.mx/reader033/viewer/2022051709/577ccf2c1a28ab9e788f1256/html5/thumbnails/4.jpg)
AT89S52 Pin Configurations
![Page 5: 2 Phan cung](https://reader033.vdocuments.mx/reader033/viewer/2022051709/577ccf2c1a28ab9e788f1256/html5/thumbnails/5.jpg)
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 }}
![Page 6: 2 Phan cung](https://reader033.vdocuments.mx/reader033/viewer/2022051709/577ccf2c1a28ab9e788f1256/html5/thumbnails/6.jpg)
Outline
How to build the first embedded system with AT89S52?
• Oscillator circuit
• Reset circuit
• Controlling outputs (LEDs)
• Reading inputs (SWs)
![Page 7: 2 Phan cung](https://reader033.vdocuments.mx/reader033/viewer/2022051709/577ccf2c1a28ab9e788f1256/html5/thumbnails/7.jpg)
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
![Page 8: 2 Phan cung](https://reader033.vdocuments.mx/reader033/viewer/2022051709/577ccf2c1a28ab9e788f1256/html5/thumbnails/8.jpg)
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
![Page 9: 2 Phan cung](https://reader033.vdocuments.mx/reader033/viewer/2022051709/577ccf2c1a28ab9e788f1256/html5/thumbnails/9.jpg)
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.
![Page 10: 2 Phan cung](https://reader033.vdocuments.mx/reader033/viewer/2022051709/577ccf2c1a28ab9e788f1256/html5/thumbnails/10.jpg)
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?
![Page 11: 2 Phan cung](https://reader033.vdocuments.mx/reader033/viewer/2022051709/577ccf2c1a28ab9e788f1256/html5/thumbnails/11.jpg)
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?
![Page 12: 2 Phan cung](https://reader033.vdocuments.mx/reader033/viewer/2022051709/577ccf2c1a28ab9e788f1256/html5/thumbnails/12.jpg)
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
![Page 13: 2 Phan cung](https://reader033.vdocuments.mx/reader033/viewer/2022051709/577ccf2c1a28ab9e788f1256/html5/thumbnails/13.jpg)
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!
![Page 14: 2 Phan cung](https://reader033.vdocuments.mx/reader033/viewer/2022051709/577ccf2c1a28ab9e788f1256/html5/thumbnails/14.jpg)
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
![Page 15: 2 Phan cung](https://reader033.vdocuments.mx/reader033/viewer/2022051709/577ccf2c1a28ab9e788f1256/html5/thumbnails/15.jpg)
Reset circuit
• The AT89S52 has 3 sources of reset:
- Power-on Reset:
- External Reset:
- Watchdog Reset: Automatic reset if program stops
![Page 16: 2 Phan cung](https://reader033.vdocuments.mx/reader033/viewer/2022051709/577ccf2c1a28ab9e788f1256/html5/thumbnails/16.jpg)
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
![Page 17: 2 Phan cung](https://reader033.vdocuments.mx/reader033/viewer/2022051709/577ccf2c1a28ab9e788f1256/html5/thumbnails/17.jpg)
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
![Page 18: 2 Phan cung](https://reader033.vdocuments.mx/reader033/viewer/2022051709/577ccf2c1a28ab9e788f1256/html5/thumbnails/18.jpg)
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?
![Page 19: 2 Phan cung](https://reader033.vdocuments.mx/reader033/viewer/2022051709/577ccf2c1a28ab9e788f1256/html5/thumbnails/19.jpg)
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
![Page 20: 2 Phan cung](https://reader033.vdocuments.mx/reader033/viewer/2022051709/577ccf2c1a28ab9e788f1256/html5/thumbnails/20.jpg)
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
![Page 21: 2 Phan cung](https://reader033.vdocuments.mx/reader033/viewer/2022051709/577ccf2c1a28ab9e788f1256/html5/thumbnails/21.jpg)
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}
![Page 22: 2 Phan cung](https://reader033.vdocuments.mx/reader033/viewer/2022051709/577ccf2c1a28ab9e788f1256/html5/thumbnails/22.jpg)
Reading switches
#include <Reg52.H>sbit K1 = P1^0;void main (void){while(1) { if (K1 == 0) //switch K1 pressed?
{ //your code
} }
}
![Page 23: 2 Phan cung](https://reader033.vdocuments.mx/reader033/viewer/2022051709/577ccf2c1a28ab9e788f1256/html5/thumbnails/23.jpg)
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
![Page 24: 2 Phan cung](https://reader033.vdocuments.mx/reader033/viewer/2022051709/577ccf2c1a28ab9e788f1256/html5/thumbnails/24.jpg)
Exercise 2.2
2. Mắc mạch dao động và reset
![Page 25: 2 Phan cung](https://reader033.vdocuments.mx/reader033/viewer/2022051709/577ccf2c1a28ab9e788f1256/html5/thumbnails/25.jpg)
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
![Page 26: 2 Phan cung](https://reader033.vdocuments.mx/reader033/viewer/2022051709/577ccf2c1a28ab9e788f1256/html5/thumbnails/26.jpg)
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
![Page 27: 2 Phan cung](https://reader033.vdocuments.mx/reader033/viewer/2022051709/577ccf2c1a28ab9e788f1256/html5/thumbnails/27.jpg)
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
![Page 28: 2 Phan cung](https://reader033.vdocuments.mx/reader033/viewer/2022051709/577ccf2c1a28ab9e788f1256/html5/thumbnails/28.jpg)
Dealing with switch bounce
• By hardware:
Use RS latch Use RC
![Page 29: 2 Phan cung](https://reader033.vdocuments.mx/reader033/viewer/2022051709/577ccf2c1a28ab9e788f1256/html5/thumbnails/29.jpg)
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
![Page 30: 2 Phan cung](https://reader033.vdocuments.mx/reader033/viewer/2022051709/577ccf2c1a28ab9e788f1256/html5/thumbnails/30.jpg)
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