You are on page 1of 58

Embedded Microcontroller

Systems
More interfacing and examples

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Overview
• Performance metrics
• Synchronization methods
• I/O Devices and hardware interface issues
• Microcontroller in control systems
• Control block diagrams
• Actuators, plant, sensors
• Open loop, closed loop control
• Proportional and integral controllers
Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers
Performance Metrics
I/O Device
uC

• Latency – time delay between when I/O


device is ready for service and when uC
responds.
– input device – time between when data is ready
and when it is actually latched into uC
– output device – time between when device is
ready for new data and when it is sent.
Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers
Performance Metrics
I/O Device
uC

• Latency
– Hardware delays in uC subsystems
– Software delays

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Performance Metrics
uC I/O Device

• Throughput – maximum data flow (bytes


per second) that can be processed from the
I/O device.
– can be limited by uC or by I/O device
– can be reported as long term average or short
term maximum

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Performance Metrics
uC I/O Device
I/O Device

• Priority – determines the order of service when


more than two or more devices request at the same
time.
– determines if a high-priority device can suspend a low-
priority request that is currently being processed.
– may want to implement equal priority so that no device
monopolizes the uC.

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Real time systems
• Hard real-time – guarantees a maximum
latency.
• Soft real-time – system supports priority.

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Synchronization Methods
• I/O devices can be in one of 3 “states”
– idle – disabled or inactive, no I/O occurs
– busy – working on generating an input (input I/O) or
accepting an output (output I/O)
– done – ready for a new transaction.
• Busy to done transitions cause status flags to
become true.

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Synchronization – Gadfly Loop
• Polling loop
• Gadfly loop
• Busy-waiting loop – software checks status
flag in a loop that does not exit until the
status flag is set. new data, gadfly loop completes

INPUT new input is


waiting for
DEVICE: input - busy ready - done

software reads data, asks for another


Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers
Synchronization – Gadfly Scenarios
No Buffering
Input Device:
busy done busy done

uC software: I/O bound


waiting for new read process waiting read
input data data data

Input Device:
busy done waiting busy done waiting busy

uC software: CPU bound


process read process read process
data data data data data
Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers
Synchronization – Gadfly Scenarios
Buffering

Input Device: As long as buffer


is large enough,
busy done busy done busy done busy
both software
and I/O can
operate at their
BUFFER maximum rate

process read process read process


data data data data data
uC software:

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Synchronization – Blind Cycle
• Software waits a fixed amount of time and
assumes I/O will complete within the delay.
• No status flag from I/O device.
• Used for I/O that has predictable delays.
Input Device:
busy done busy done

uC software:
process waiting read process waiting read
data data data data

fixed delay
Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers
Synchronization - Interrupts
• Interrupts – hardware causes software to
execute ISR.
• Global data structures used to communicate data
between main program and ISR.
• Timer interrupts used to execute specific functions
at regular intervals.

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Synchronization - Interrupts
• Use interrupts when:
– Arrival times of input is variable.
– There are other things to do in main program.
– I/O is important (alarm, hardware failure) but
infrequent.
• Buffering can also be used with interrupts to
allow for better throughput.
• Must not forget that interrupts slow the main
program loop!

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Buffering – FIFO Queue
Producer Put Get Consumer
FIFO
(uC or I/O) (uC or I/O)

• Data is received (Get) in the same order that it was


transmitted (Put)
• As long as FIFO is not full or empty, both producer and
consumer operate at their own rate.
• Need a way for producer and consumer to know if FIFO is
full or empty.

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Synchronization – Periodic Polling
• Periodic polling – uses a clock/timer
interrupt to periodically check the I/O
status.
– Used in cases where interrupts are desirable
(there is much to do in the main program) but
the I/O device does not support interrupts.
– Keypad is an example – will investigate in the
next exercise.

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Synchronization - DMA
• Direct Memory Access – I/O transfers data
directly to/from memory.
– Requires a DMA controller between memory
and I/O device.
– Used when bandwidth and latency are
important parameters.
– Data transfer only – no processing of data.

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


DMA
• Burst mode DMA – a block transferred while the
uC is halted
– used when uC and DMA rates are similar
• Cycle-stealing DMA – data is transferred during
cycles when the uC is not using the bus.
– used when uC rate is faster than I/O

DMA Controller
I/O
uC
device
addr
Memory
data

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Slow I/O Interface

Keypad – how slow can we go?

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


I/O Devices and hardware interface
issues
• Overview:
• Categories of I/O
• Input and Output examples
• Output actuator examples
• DC Motor (analog and digital control)
• Stepper Motor
• Output display example
• LCD display (parallel and serial)

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


I/O Categories
• Input devices
– Sensors, User-input
• Output devices
– Actuators, Displays
• Complex I/O devices (printers, faxes, coprocessors, etc)

Analog I/O Digital I/O


– Voltage levels - Voltage levels
– Current draw - Synchronization
– Sampling frequency - Throughput
– Noise - Noise

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Input Examples
• Sensors • User input
– light – keyboards
– force – joysticks
– sound
– mouse
– position
– keypad
– orientation
– proximity – switches
– tactile – touchpad
– temperature – dial
– pressure – slider
– humidity
– speed
– acceleration
– displacement

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Output Examples
• Actuators • Displays
– motors – LED displays
– solenoids – LCD displays
– relays – CRT displays
– heaters – indicator lights
– lights – indicator gauges
– piezoelectric materials
(buzzers, linear actuator)
– speakers

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Example – DC Motor
• Important in LOTS of applications
– cameras, drives, elevators, trains, robots…
• Many types, but all work similarly:
– Apply voltage across + and – leads,
electrical energy is converted to mechanical
energy.
– For some range of voltage, the torque of the
motor shaft is proportional to value of
voltage.

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


DC Motor
• Current required by
motor depends on
how it is loaded.

Motor current (A)


• Current is almost

led
always more than the loaded

l
sta
uC can provide.
no load
• Need an interface
circuit between uC Applied voltage (volts)
and motor.
Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers
Interfacing Motors
Digital Outputs
• Basic idea is to use a flyback diode
switch of some kind to
isolate current in uC
from motor current. External Voltage
+
• Motor is an inductor switch open current

though, so it stores

swi urren
current.

t ch
motor

c
• Flyback diode used to

cl o
sed
t
route current away from
Control
switch when switch signal from
opens to avoid damage uC
to switch.

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Interfacing Motors
Digital Outputs
H-Bridge – circuit topology that allows bi-
directional control of motor.
• Each switch
controlled by an
output of the uC.
+
external
voltage motor • Switches
- implemented by
relays, solid-state
switches, or transistors
• Diodes omitted for
simplicity.
Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers
Interfacing Motors
Analog Output
• 8051 DAC can provide up to 15 mA of
current, up to 3.3V voltage.
• Must provide both voltage and current
amplification to drive a DC motor.
– Amplifiers used in the upcoming control lab
– Power MOSFETs
– Motor driver ICs
– Relays

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Stepper Motors
• Inherent digital
interface
• Can easily control
both position and
velocity
• Used in disk drives,
printers, etc.
• Small, fixed rotation
per change in control
signals

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Basic Operation
• Simplified stepper motor
5 “teeth”
360° ÷ 5 = 72°
-
phase 2 moves 72° per “step”
+
S5

N1
N5

+ Changing polarity of
S4 S1
phase 1
- N2 stator magnets causes
N4
step.
S3
S2
N3

rotor Typical stepper


motors have 200 steps
per revolution, with
electromagnets stator 1.8° per step.
Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers
Stepper Motor Interface
+Vmotor
VDD
4-phase
Inverting
8051 stepper
buffers motor
A
port A’
pins B
B’

1010
1.8°
1001
1.8 °
0101
1.8 °
0110
1.8 °
1010
Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers
Output Display Example:
LCD Display
• LCD – Liquid Crystal Display
– Lower power than LED display
– More flexible in size and shape
– Slower response time

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


LCD Operation
• AC voltage required – DC voltage damages LCD
• Control changes reflectivity of the liquid crystal
material.
• Actual light energy supplied by room light or back
light. front plane
CMOS back plane
control

60 Hz
Oscillator
liquid crystal material

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


LCD Interfacing
• Simple parallel interface – similar to LED:
VDD 7-segment
LCD
Driver/Decoder Separate Front Planes
8051 a
b
c
A d
port B e
pins C f
D g
Common Back Plane

60 Hz
Oscillator

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


LCD Interfacing
• Serial driver interface
MC145000 LCD Driver
data in data out
48 bit shift register
clock

48 bit latch register


BP1 BP2 BP3 BP4 FP1 FP2 FP3 FP4 FP5 FP6 FP7 FP8 FP9 FP10 FP11 FP12

48 segment LCD display

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Using Microcontrollers for Control
• Overview
– Open-loop control systems
– Simple closed-loop control systems
– Closed-loop position control
– PID controllers

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Some Terminology
• Control variables – properties we want to
control (position, velocity, temperature, etc)
• Control commands – output to actuators
• Driving forces – the actuator forces that
cause the control variables to change (heat,
force, etc)
• Physical plant – the thing being controlled

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Open-loop Control Systems
• No feedback path from the plant
• Note that these are all a function of time
Real
Control Driving control
commands Forces variables
Desired
U(t) V(t) Physical X(t)
control Actuators
variables uC Plant
X*(t)

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Open-loop Control Example
• Stepper motor
Real
Control Driving control
commands Forces variables
Desired
U(t) V(t) Physical X(t)
control
uC Actuators
variables Plant
X*(t)
Inverting
driving
desired buffers
shaft
uC stepper
position
motor shaft position
specified
in
program
Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers
Open-loop Control Example
• Traffic light controller
Inverting
driving
buffers

uC

desired light pattern


in software
Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers
Closed-loop Control
• Feedback from plant to controller

Real
Control Driving control
commands Forces variables
Desired
U(t) V(t) Physical X(t)
control Actuators
variables uC Plant
X*(t)

Sensor

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Closed-loop Control
“Bang-bang control”
• Bang-bang – control output can only turn something ON
or OFF. No variable control.

• Requires a “deadband” or “hysteresis” which defines a


range of acceptable values for output - otherwise the
control system components can wear out from too many
switching cycles. (Relays, for example, have a limited
lifetime).

• Works well with physical plant with a slow response


time.

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Closed-loop Control Systems
• Bang-bang control – temperature control
example.
Desired temperature,
Tlow < T < Thigh “plant”
Flowchart of control algorithm
Heater
estimate T’
uC
T’ > Thigh T’ < Tlow T’
T’ Temperature
sensor

Turn off Leave Turn on

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Closed-loop Position Control
• Incremental control – adds or subtracts a small
constant from the output control command, U(t),
in response to X(t) sensed.
Real
Control Driving control
commands Forces variables
Desired
U(t) V(t) Physical X(t)
control Actuators
variables uC Plant
+1
X*t or
-1

Sensor

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Incremental Control
• Rate of sampling is very important
– If sampling rate is too fast, actuators are
saturated and a bang-bang system results.
– If sampling rate is too slow, then controller will
not keep up with plant.
• Rule of thumb for rate: control execution
rate is 10x the step response of the plant.
• Must check for underflow and overflow
after increment or decrement.
Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers
Closed-loop Control
PID Controller
• Faster and more accurate than previous systems.
• Based on linear control theory.
• Three components – sometimes fewer are used.

– Proportional – output is linearly related to error signal.

– Integral – output is related to integral of the error


signal.

– Derivative – output is related to derivative of the error.

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


PID Controller
• To understand, must transform parts of control diagram into discrete time
domain.
• Very important to have periodic sampling and processing.
• In the figure below, n is the sample number

Real
control
Desired variables
output + e(n) uC u(n) p(t) Physical x(t)
x* PID Actuator
- Plant
controller

x’(n)
Sensor

error signal: e(n) = x*(n) - x’(n)


Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers
PID Controller
u(t) = P(t) + I(t) + D(t)

Proportional – output is proportional to error input


Continuous time: P(t) = Kp * E(t)
Discrete time: P(n) = Kp * E(n)

Actual
Desired Output
output + e(n) uC u(n) p(t) Physical x(t)
x* PID Actuator
- Plant
controller

x’(n)
Sensor
Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers
PID Controller
u(t) = P(t) + I(t) + D(t)

Integral – output is proportional to integral of error signal


Continuous time: I(t) = Ki E(t) dt
Discrete time: I(n) = Ki ∑ E(n) ∆ t = Ki ∆ t ∑ E(n)

e(t) I(n)
Ki large

t n

Ki small
I(t) I(n) I(n)
∆t
t 1 2 3 4 5 n n
∆ t is sampling period
Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers
Equation for Integral Component
Discrete time: I(n) = Ki ∆ t ∑ E(n)

integral += errorsig; //integral = integral + errorsig


//integral is sum of errorsignals
//integral includes: M_MEAS samples of error
//multiplied by GAIN_PRECISION
...
output = Kp*errorsig / M_MEAS/GAIN_PRECISION +
Ki * integral / M_MEAS / SAMPLERATE /GAIN_PRECISION

= 1/∆ t

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


PID Controller
u(t) = P(t) + I(t) + D(t)

Derivative – output is proportional to derivative of error signal


dE
Continuous: D(t) = Kd* dt
Discrete: D(n) = Kd * E(n) – E(n-1)
∆t

∆ t is sampling period

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


PI Controller for Position Control
(we will not use the derivative term, which can cause instability)

Desired Actual
position position
θ 1* uC θ 2(t)
(potentiometer)
uf(n) Servo p(t) DC
PI
θ 2(n) Amplifier Motor
controller ub(n)

potentiometer

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


PI Controller Hardware Setup
8051
Microcontroller
setpoint ADC0.0
Input

multiplexor
potentiometer -15 to +15V
0 - 2.45V
voltage
conversio ADC0
ADC0.1 to SA1SOD
output n DAC0 Output Input 1
potentiometer -15 to +15V interface 0 - 2.45V DAC0
0 - 2.45V voltage
conversion to SA1SOD
DAC1 interface Input 2
DAC1
0 - 2.45V

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Circuit Schematics for Interface Circuits
Input voltage conversion interface Output voltage conversion
interface

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


PI Control Algorithm
• Controller must execute the following tasks:
– Sample inputs
– Compute error value
– Compute integral value
– Compute output signal from error value,
integral value and preset Kp and Ki.
– Send computed output signals to amplifier

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Controller Performance Metrics
• Stability – a requirement
• Response time – how fast the output responds
to the input.
• Steady state error – how much the output
differs from the input after it has settled.
• For position controller – DEADBAND is a
measure of the steady state error.

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Total DeadBand Measurements
input position output position
θ 1 θ 2

DB (θ 1) = θ 1- θ 2

repeat for a negative angle, - θ 1 DB (-θ 1) = θ 2- θ 1

TDB(θ 1) = |DB(θ 1)| + | DB(-θ 1)|

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers


Control your position!

Prelab – software only

Prof. Cherrice Traver EE/CS-152: Microprocessors and Microcontrollers

You might also like