Beruflich Dokumente
Kultur Dokumente
HARDWARE FOUNDATIONS
Dr. Thanh Hung Tran Department of Automation Technology, College of Engineering, Can Tho University Email: tthung@ctu.edu.vn
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
#include <Reg52.H> void main (void) { unsigned char Port1_data; P1 = 0xFF; // set Port1 as input while(1) { Port1_Data = P1; // Read the value of P1 P2 = Port1_Data; // Copy the value to P2 } }
Outline
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
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 beat What 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,)
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?
Oscillator Error minutes types (%) in 1 year Crystal 0.001% 525,600 oscillator Ceramic 0.5% 525,600 resonator RC 20% 525,600 oscillator Error/year (minutes) 5.26 2,628 105,120 43.8 1,752 Error/year (hours)
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:
Controlling DC loads
The 89S52 port pins can be set to 0V or 5V
R= ? If I = 10mA
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)?
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
Reading switches
How to read the switch status?
unsigned char Port_Data; P1 = 0xFF; // set Port 1 as input Port_Data = P1; // read Port 1 pins
Problem?
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
Xy dng phn cng h thng nhng theo cc bc sau (trn project board):
1. Mc mch ngun: To in th 5V t pin 9V
LM7805 1
GND
VI
9V BATTERY
2
Exercise 2.2
2. Mc mch dao ng v reset
Exercise 2.2
3. Mc mch hin th LED Port 0 v 4 SW0-SW4 Port 1
Vcc
1 1 1 1 2 2 2 2 SW0 SW1 SW2 SW3
Vcc
+
10uF
R2 10k
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
Ideal SW
press Real SW
release
Use RS latch
Use RC
1st read
2nd read
1. Read the relevant port pin 2. If a switch depression is detected, wait for 20ms and then read again 3. If the second reading confirms the first reading, wait until the switch is released, assume that the switch was pressed