Sie sind auf Seite 1von 30

Programming embedded systems Seminar 2

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

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 input while(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 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,)

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?
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)

Which oscillator do you want to choose?

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

R= ? If I = 10mA

P0 = 0x0F; // set 4 high bits to 0V, 4 low bits to 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)?

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?
unsigned char Port_Data; P1 = 0xFF; // set Port 1 as input Port_Data = P1; // read Port 1 pins

How to how which switch pressed?


if (P1.0 == 0) //switch connected to P1.0 pressed? { //your code }

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

Vcc VO 3 R1 330 Ohm 10uF D1 LED

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

Dealing with switch bounce


In practice, all mechanical switch contacts bounce after the switch is closed and opened

Ideal SW

press Real SW

release

Counting the number of times that a SW is pressed becomes extremely difficult.

Dealing with switch bounce


By hardware:

Use RS latch

Use RC

Dealing with switch bounce


By software:
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, assume that the switch was pressed

1st read

2nd read

Dealing with switch long depression


What happen if the user press a switch for too long?

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

Das könnte Ihnen auch gefallen