Programming Embedded Systems Seminar 2: Hardware Foundations
Programming Embedded Systems Seminar 2: Hardware Foundations
Seminar 2
HARDWARE FOUNDATIONS
Dr. Tran Thanh Hung
Department of Automation Technology,
College of Engineering, Can Tho University
Email: [email protected]
Outline
How to build the first embedded system
with MSP430?
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 MSP430G2452
understand issue of stability
write programs to access inputs and control
outputs
MSP430G2452 Features
Low Supply-Voltage Range: 1.8 V to 3.6 V
Ultra-Low Power Consumption:
Active Mode: 220 A at 1 MHz, 2.2 V
Standby Mode: 0.5 A
Off Mode (RAM Retention): 0.1 A
MSP430G2452 Features
1x 16-Bit Timer
Universal Serial Interface (USI) Supporting SPI, I2C
10-Bit 200-ksps A/D Converter
On-Chip Comparator for Analog
16 I/O pins, Touch-Sense
Serial Onboard Programming
8K Bytes of In-System Programmable (ISP) Flash
Memory (10,000 Write/Erase Cycles )
256 Bytes Internal RAM
MSP430G2452 Pinout
Oscillator circuit
Why oscillator circuit is needed for embedded
systems?
- It provides clock to drive embedded 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
There are many options for
oscillator circuit:
Crystal oscillator
Ceramic resonator oscillator
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?
Reset circuit
Reset circuit
The MSP430 has many sources of reset:
- Power On Reset (POR) (Hardware)
Power up
Active reset pin (low)
Low signal at Supervisory Voltage System (PORON = 1)
Brownout reset
Controlling DC loads
The MSP430 port pins can be set to 0V or Vcc
P1OUT ^= 0x01;
Bi tp 2.1
Kho st nh hng ca vic thay i tc xung clock
To project Baitap2_1
To source file baitap2_1.c, copy ni dung trong baitap1_1.c
Bin dch v Debug chng trnh
Chy chng trnh lin tc, quan st LED, ch tc chp tt
Tm dng chng trnh
Trn menu, chn View\Registers, trong System_Clock\BCSCTL1,
t tt c cc bit RSEL v 0
Chy li chng trnh. Tc chp tt LED th no?
Tm dng chng trnh. Ghi li gi tr cc thanh ghi v I/O
Bm vo biu tng reset chng trnh . iu g xy ra vi I/O,
cc thanh ghi, im bt u thc hin chng trnh?
Chy li chng trnh. Tc chp tt LED th no?
Bi tp 2.2
Vit chng trnh bt v tt tng LED
trn Port P1. Ghi ch li th t bit iu
khin ng vi tng LED.
(gn trc tip gi tr cho P1OUT,
V d:
P1OUT = 1
P1OUT = 0)
Bi tp 2.3
Vit chng trnh:
- Bt tt c cc LED
- Tt v bt tng LED m khng nh
hng n cc LED khc. (Dng php
AND (&) v OR (|) vi mt n tt v
bt bit)
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
Reading switches
How to read the switch status?
unsigned char Port_Data;
P1DIR = 0;
// set Port 1 as input
Port_Data = P1IN; // read Port 1 pins
Problem?
Reading switches
Bi tp 2.4
Vit chng trnh m s ln SW ni vi chn P2.0 (c
th l SW0) c bm, xut kt qu ra cc LED trn
Port P1.
Kim tra xem mi ln bm SW, chng trnh m
c my ln?
Lu : Do cu to ca board m rng, SW0 c ni vi
P2.0 v P2.4. nhn bit c phm c bm hay
khng, cn cho 1 chn (v d P2.4) xung mc 0, xt
chn cn li.
Ideal SW
press
release
Real SW
Use RS latch
Use RC
1st read
2nd read
Bi tp 2.4 (tt)
Vit chng trnh m s ln SW ni vi chn P1.0
c bm, xut kt qu ra cc LED trn Port P1.
Kim tra xem mi ln bm SW, chng trnh m
c my ln?
Thay i chng trnh mi ln bm SW, chng
trnh ch m 1.
Keypad interface
If a system has many SWs, how to interface with SWs?
Keypad 4x4
Keypad is a matrix of SWs or keys
How to know which SW/key is pressed?
Algorithm
Bi tp 2.5
Vit chng trnh:
- c ma trn phm 4x4
- Hin th th t phm c
bm ra cc LED
T hc
c file msp430g2452 Datasheet:
- ch cu hnh cc port v cch t cu
hnh
- tn s DCO
c chng 4, Embedded C