Embedded Microcontroller Systems

Embed Size (px)

Citation preview

  • 7/30/2019 Embedded Microcontroller Systems

    1/58

    Embedded Microcontroller Systems

  • 7/30/2019 Embedded Microcontroller Systems

    2/58

    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

  • 7/30/2019 Embedded Microcontroller Systems

    3/58

    Performance Metrics

    Latency time delay between when I/Odevice is ready for service and when uCresponds.

    input device time between when data isready and when it is actually latched into uC

    output device time between when device isready for new data and when it is sent.

    uCI/O Device

  • 7/30/2019 Embedded Microcontroller Systems

    4/58

    Performance Metrics

    Latency Hardware delays in uC subsystems

    Software delays

    uCI/O Device

  • 7/30/2019 Embedded Microcontroller Systems

    5/58

    Performance Metrics

    Throughput maximum data flow (bytesper 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

    uC I/O Device

  • 7/30/2019 Embedded Microcontroller Systems

    6/58

    Performance Metrics

    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.

    uC I/O Device

    I/O Device

  • 7/30/2019 Embedded Microcontroller Systems

    7/58

    Real time systems

    Hard real-time guarantees a maximum

    latency.

    Soft real-time system supports priority.

  • 7/30/2019 Embedded Microcontroller Systems

    8/58

    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.

  • 7/30/2019 Embedded Microcontroller Systems

    9/58

    Synchronization Gadfly Loop

    Polling loop

    Gadfly loop

    Busy-waiting loop software checks statusflag in a loop that does not exit until the status

    flag is set.

    waiting for

    input - busy

    new input is

    ready - done

    new data, gadfly loop completes

    software reads data, asks for another

    INPUTDEVICE:

  • 7/30/2019 Embedded Microcontroller Systems

    10/58

    Synchronization Gadfly Scenarios

    No Buffering

    busy done busy done

    Input Device:

    waiting for new

    input

    read

    data

    process

    data

    waiting read

    data

    uC software: I/O bound

    busy done

    Input Device:

    read

    data

    waiting

    uC software:

    process

    data

    CPU bound

    read

    data

    process

    data

    busy done waiting

    process

    data

    busy

  • 7/30/2019 Embedded Microcontroller Systems

    11/58

    Synchronization Gadfly Scenarios

    Buffering

    busy done

    Input Device:

    read

    data

    uC software:

    process

    data

    read

    data

    process

    data

    busy done

    process

    data

    busy

    BUFFER

    done busy

    As long as buffer

    is large enough,

    both software

    and I/O canoperate at their

    maximum rate

  • 7/30/2019 Embedded Microcontroller Systems

    12/58

    Synchronization Blind Cycle

    Software waits a fixed amount of time andassumes I/O will complete within the delay.

    No status flag from I/O device.

    Used for I/O that has predictable delays.

    busy done busy done

    Input Device:

    process

    datawaiting read

    data

    uC software:process

    datawaiting read

    data

    fixed delay

  • 7/30/2019 Embedded Microcontroller Systems

    13/58

    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.

  • 7/30/2019 Embedded Microcontroller Systems

    14/58

    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) butinfrequent.

    Buffering can also be used with interrupts toallow for better throughput.

    Must not forget that interrupts slow the mainprogram loop!

  • 7/30/2019 Embedded Microcontroller Systems

    15/58

    Buffering FIFO Queue

    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.

    Producer

    (uC or I/O)

    Consumer

    (uC or I/O)FIFOPut Get

  • 7/30/2019 Embedded Microcontroller Systems

    16/58

    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.

  • 7/30/2019 Embedded Microcontroller Systems

    17/58

    Synchronization - DMA

    Direct Memory Access I/O transfers datadirectly 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.

  • 7/30/2019 Embedded Microcontroller Systems

    18/58

    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

    uC

    DMA Controller

    Memory

    I/O

    deviceaddr

    data

  • 7/30/2019 Embedded Microcontroller Systems

    19/58

    Slow I/O Interface

    Keypad how slow can we go?

  • 7/30/2019 Embedded Microcontroller Systems

    20/58

    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)

  • 7/30/2019 Embedded Microcontroller Systems

    21/58

    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

  • 7/30/2019 Embedded Microcontroller Systems

    22/58

    Input Examples

    Sensors light

    force

    sound

    position

    orientation proximity

    tactile

    temperature

    pressure

    humidity speed

    acceleration

    displacement

    User input

    keyboards

    joysticks

    mouse

    keypad switches

    touchpad

    dial

    slider

  • 7/30/2019 Embedded Microcontroller Systems

    23/58

    Output Examples

    Actuators

    motors

    solenoids

    relays

    heaters

    lights

    piezoelectric materials

    (buzzers, linear actuator)

    speakers

    Displays

    LED displays

    LCD displays

    CRT displays

    indicator lights

    indicator gauges

  • 7/30/2019 Embedded Microcontroller Systems

    24/58

    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.

  • 7/30/2019 Embedded Microcontroller Systems

    25/58

    DC Motor

    Current required bymotor depends on

    how it is loaded.

    Current is almostalways more than the

    uC can provide.

    Need an interfacecircuit between uC

    and motor.

    Applied voltage (volts)

    no load

    loaded

    Motorcurren

    t(A)

  • 7/30/2019 Embedded Microcontroller Systems

    26/58

    Interfacing MotorsDigital Outputs

    Basic idea is to use aswitch of some kind toisolate current in uCfrom motor current.

    Motor is an inductorthough, so it storescurrent.

    Flyback diode used toroute current away

    from switch whenswitch opens to avoiddamage to switch.

    motor

    External Voltage

    +

    Controlsignal from

    uC

    flyback diode

    switch open current

  • 7/30/2019 Embedded Microcontroller Systems

    27/58

    Interfacing MotorsDigital Outputs

    H-Bridge circuit topology that allows bi-

    directional control of motor.

    motor

    +

    external

    voltage

    -

    Each switch

    controlled by anoutput of the uC.

    Switches

    implemented by

    relays, solid-stateswitches, or transistors

    Diodes omitted for

    simplicity.

  • 7/30/2019 Embedded Microcontroller Systems

    28/58

    Interfacing MotorsAnalog 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

    Power MOSFETs

    Motor driver ICs

    Relays

    used in the upcoming control lab

  • 7/30/2019 Embedded Microcontroller Systems

    29/58

    Stepper Motors

    Inherent digitalinterface

    Can easily control

    both position and

    velocity

    Used in disk drives,

    printers, etc.

    Small, fixed rotationper change in control

    signals

    http://images.google.com/imgres?imgurl=www.interq.or.jp/japan/se-inoue/picture/step1.jpg&imgrefurl=http://www.interq.or.jp/japan/se-inoue/e_step.htm&h=382&w=533&sz=25&tbnid=fRACYB_AJrkJ:&tbnh=92&tbnw=128&start=7&prev=/images%3Fq%3Dstepper%2Bmotor%26hl%3Den%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26sa%3DG
  • 7/30/2019 Embedded Microcontroller Systems

    30/58

    Basic Operation

    Simplified stepper motor

    +

    phase 1

    -

    -

    phase 2

    +

    5 teeth

    360 5 = 72

    moves 72per step

    Changing polarity of

    stator magnets causes

    step.

    Typical stepper motors

    have 200 steps per

    revolution, with 1.8

    per step.stator

    rotor

    electromagnets

  • 7/30/2019 Embedded Microcontroller Systems

    31/58

    Stepper Motor Interface

    8051

    port

    pins

    A

    A

    B

    B

    VDD

    Inverting

    buffers

    +Vmotor

    1010

    1001

    0101

    0110

    1010

    4-phase

    stepper

    motor

    1.8

    1.8

    1.8

    1.8

  • 7/30/2019 Embedded Microcontroller Systems

    32/58

    Output Display Example:

    LCD Display

    LCD Liquid Crystal Display Lower power than LED display

    More flexible in size and shape

    Slower response time

    http://images.google.com/imgres?imgurl=www.armyradio.com/Pictures/Radios/British%2520Radios/Ship%2520Speed%2520LCD%2520Display%2520B1.jpg&imgrefurl=http://www.armyradio.com/publish/Bits_and_Pieces.htm&h=280&w=516&sz=23&tbnid=V6QB4wcwzVgJ:&tbnh=69&tbnw=127&start=5&prev=/images%3Fq%3DLCD%2Bdisplay%26hl%3Den%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26sa%3DGhttp://images.google.com/imgres?imgurl=www.irtrans.de/images/products/lcd-blau-640.jpg&imgrefurl=http://www.irtrans.de/lcd.htm&h=394&w=640&sz=28&tbnid=giPgHHLkoMAJ:&tbnh=83&tbnw=134&start=13&prev=/images%3Fq%3DLCD%2Bdisplay%26hl%3Den%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26sa%3DG
  • 7/30/2019 Embedded Microcontroller Systems

    33/58

    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

    liquid crystal material

    back plane

    60 Hz

    Oscillator

    control

    CMOS

  • 7/30/2019 Embedded Microcontroller Systems

    34/58

    LCD Interfacing

    Simple parallel interface similar to LED:

    8051

    7-segment

    LCD

    Driver/Decoder

    port

    pins

    A

    B

    C

    D

    ab

    c

    d

    e

    f

    g

    60 Hz

    Oscillator

    Common Back Plane

    Separate Front Planes

    VDD

  • 7/30/2019 Embedded Microcontroller Systems

    35/58

    LCD Interfacing

    Serial driver interface

    48 bit shift register

    48 bit latch register

    MC145000 LCD Driver

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

    48 segment LCD display

    data in

    clock

    data out

  • 7/30/2019 Embedded Microcontroller Systems

    36/58

    Using Microcontrollers for Control

    Overview

    Open-loop control systems

    Simple closed-loop control systems

    Closed-loop position control

    PID controllers

  • 7/30/2019 Embedded Microcontroller Systems

    37/58

    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

  • 7/30/2019 Embedded Microcontroller Systems

    38/58

    Open-loop Control Systems

    No feedback path from the plant

    Note that these are all a function of time

    uC ActuatorsPhysical

    Plant

    Desired

    control

    variables

    X*(t)

    Controlcommands

    U(t)

    DrivingForces

    V(t)

    Real

    controlvariables

    X(t)

  • 7/30/2019 Embedded Microcontroller Systems

    39/58

    Open-loop Control Example

    Stepper motor

    uC ActuatorsPhysical

    Plant

    Control

    commands

    U(t)

    Driving

    Forces

    V(t)

    Real

    control

    variables

    X(t)

    uC

    Desired

    control

    variables

    X*(t)Inverting

    driving

    buffers

    steppermotor shaft position

    desired

    shaftposition

    specified

    in

    program

  • 7/30/2019 Embedded Microcontroller Systems

    40/58

    Open-loop Control Example

    Traffic light controller

    uC

    Inverting

    driving

    buffers

    desired light pattern

    in software

  • 7/30/2019 Embedded Microcontroller Systems

    41/58

    Closed-loop Control

    Feedback from plant to controller

    uC ActuatorsPhysical

    Plant

    Desired

    control

    variables

    X*(t)

    Controlcommands

    U(t)

    DrivingForces

    V(t)

    Real

    controlvariables

    X(t)

    Sensor

    Closed loop Control

  • 7/30/2019 Embedded Microcontroller Systems

    42/58

    Closed-loop Control

    Bang-bang control

    Bang-bang control output can only turn somethingON or OFF. No variable control.

    Requires a deadband or hysteresis which definesa range of acceptable values for output - otherwisethe control system components can wear out fromtoo many switching cycles. (Relays, for example, havea limited lifetime).

    Works well with physical plant with a slow responsetime.

  • 7/30/2019 Embedded Microcontroller Systems

    43/58

    Closed-loop Control Systems Bang-bang control temperature control

    example.

    Heater

    Temperature

    sensor

    uC

    plant

    Desired temperature,

    Tlow < T < Thigh

    estimate T

    TT

    Turn off Leave Turn on

    T > Thigh T < Tlow

    Flowchart of control algorithm

  • 7/30/2019 Embedded Microcontroller Systems

    44/58

    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.

    uC+1

    or

    -1

    ActuatorsPhysical

    Plant

    Desired

    control

    variables

    X*t

    Controlcommands

    U(t)

    DrivingForces

    V(t)

    Real

    controlvariables

    X(t)

    Sensor

  • 7/30/2019 Embedded Microcontroller Systems

    45/58

    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 notkeep 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.

    Cl d l C t l

  • 7/30/2019 Embedded Microcontroller Systems

    46/58

    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.

    PID C ll

  • 7/30/2019 Embedded Microcontroller Systems

    47/58

    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

    uCPID

    controller

    ActuatorPhysical

    Plant

    Desired

    output

    x*u(n) p(t)

    Real

    controlvariables

    x(t)

    Sensor

    x(n)

    e(n)

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

    +

    -

    PID C ll

  • 7/30/2019 Embedded Microcontroller Systems

    48/58

    PID Controller

    uCPID

    controller

    ActuatorPhysical

    Plant

    Desired

    output

    x*u(n) p(t)

    Actual

    Output

    x(t)

    Sensor

    x(n)

    e(n)+

    -

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

    Proportionaloutput is proportional to error input

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

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

    PID C t ll

  • 7/30/2019 Embedded Microcontroller Systems

    49/58

    PID Controller

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

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

    t is sampling period

    e(t)

    t

    I(t)

    t

    I(n)

    n

    t

    1 2 3 4 5

    I(n)

    n

    Ki large

    I(n)

    n

    Ki small

  • 7/30/2019 Embedded Microcontroller Systems

    50/58

    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

    PID C t ll

  • 7/30/2019 Embedded Microcontroller Systems

    51/58

    PID Controller

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

    Derivativeoutput is proportional to derivative of error signal

    Continuous: D(t) = Kd*dE

    dt

    E(n)E(n-1)t

    Discrete: D(n) = Kd *

    t is sampling period

  • 7/30/2019 Embedded Microcontroller Systems

    52/58

    PI Controller for Position Control

    uCPI

    controller

    ServoAmplifier

    DC

    Motor

    uf

    (n)p(t)

    Actual

    position

    2(t)

    Desired

    position

    1*

    (potentiometer)

    potentiometer

    2(n) ub(n)

    (we will not use the derivative term, which can cause instability)

  • 7/30/2019 Embedded Microcontroller Systems

    53/58

    PI Controller Hardware Setup

    8051Microcontroller

    ADC0

    Input

    voltage

    conversion

    interface

    ADC0.0

    ADC0.1

    setpoint

    potentiometer

    output

    potentiometer

    0 - 2.45V

    0 - 2.45V-15 to +15V

    -15 to +15V

    Output

    voltageconversion

    interface

    DAC0

    DAC1

    0 - 2.45V

    0 - 2.45V

    multiplexor

    DAC0

    DAC1

    to SA1SOD

    Input 1

    to SA1SOD

    Input 2

  • 7/30/2019 Embedded Microcontroller Systems

    54/58

    Circuit Schematics for Interface Circuits

    Input voltage conversion interface Output voltage conversion interface

  • 7/30/2019 Embedded Microcontroller Systems

    55/58

    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

  • 7/30/2019 Embedded Microcontroller Systems

    56/58

    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.

  • 7/30/2019 Embedded Microcontroller Systems

    57/58

    Total DeadBand Measurements

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

    input position output position

    1 2

    DB (1) = 1- 2

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

  • 7/30/2019 Embedded Microcontroller Systems

    58/58

    Control your position!

    Prelab software only