嵌入式系统代写代做代考 Embedded Systems Reconfigurable computing

Reconfigurable computing

Small Embedded Systems

Unit 2.7
Digital I/O and Pulse Width Modulation

Introduction
So far, our consideration of digital has looked at very simple cases: using a perfect mechanical button to switch an LED fully on or off
In this unit we take a more sophisticated look at digital inputs and outputs:
Problems with mechanical switches: switch bounce
How to set the LED to an intermediate brightness: Pulse Width Modulation (PWM)
Using PWM for inductive loads, e.g. motor control

Switch Bounce
Mechanical switches and buttons often don’t close/open cleanly
User interface buttons/switches
Relays used in industrial control
They can bounce open and closed for a short interval

To pin 6
0 V
3.3 V
Voltage
Time

Switch Bounce
Sometimes we don’t care much,
Example LED is on when button is pressed, off when it is released

Sometimes it is important
Example LED should reverse state on button press

Off
On
Off
Off
Off
Off
Brief flicker lasts ~ 1ms
Output is wrong for sustained period
Should be on

Hardware De-Bounce
Resistor-Capacitor Circuit
Voltage rises slowly when capacitor charged through resistor
When switch opens, R1, R2 and C smooth out the bounce
When switch closes, and C discharges, R2 ensures that voltage change is slow
Schmitt Trigger
Squares up waveform
Changes state only when threshold reached
Packaged ICs are available for debounce

Hardware De-Bounce

https://www.allaboutcircuits.com/technical-articles/switch-bounce-how-to-deal-with-it/

No de-bounce
With de-bounce
Input to Schmitt trigger

Software De-Bounce
Hardware debounce normally adds additional expense, so we usually de-bounce in software
At each transition from LOW to HIGH or from HIGH to LOW
input signal is debounced by sampling across multiple reads over several milliseconds.
The input is not considered HIGH or LOW until the input signal has been sampled for at least X milliseconds in the new state.
Value of X is chosen to reflect the timescale over which the input signal may bounce before becoming steady state
There are several example code listings in the Arduino examples library

reading
X
X
X
buttonState
reading is instantaneous reading from input connected to button
marks times when reading has just changed
X is our debounce delay
buttonState is our debounced version of the button
If reading has not changed for more than X
buttonState is updated to current value of reading
else
buttonState hold its old value

Software De-Bounce
void loop() {
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastChangeTime = millis();
}
if ((millis() – lastChangeTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
}
}
lastButtonState = reading;
}
Simplified version of debounce example in Arduino example code library:

Pulse Width Modulation (PWM)
Suppose we don’t want to turn our output device fully on/off – we want an intermediate value
PWM control works by switching the voltage on and off very rapidly
By adjusting the duty cycle of the signal the average voltage can be varied
If an external device, e.g.
LED, motor, servo
has response time constants that are significantly longer than the frequency of the PWM, the external device just sees the average value of the PWM
Gives a pseudo-analog output from a digital pin

Pulse Width Modulation (PWM)
Duty cycle D = percentage time signal is high
Rising edge Pulse width Falling edge

amplitude Positive
Half Negative
Half

One Cycle

VLOW
VHIGH

Pulse Width Modulation (PWM)
We could generate PWM using software:

This would give a 10% duty cycle on a 1 kHz PWM
Not good –processor is kept busy so it can’t do other things

float dutyCycle=0.1;
long int highTime = dutyCycle * 1000;

void loop(){
digitalWrite(LED, HIGH);
delayMicroseconds(highTime);
digitalWrite(LED, LOW);
delayMicroseconds(1000-highTime);
}
Stay high for 100 ms
Stay low for 900 ms
Set output pin high
Set output pin low
10% duty cycle
1 kHz is 1000 ms period

Pulse Width Modulation (PWM)
Most microcontrollers have hardware to generate PWM (though not always available on all pins)
In Arduino, hardware PWM is generated by the analogWrite() function

N.B. the name is misleading: this isn’t really analog

void loop(){
analogWrite(LED,value);
}

Pin to write to

Duty cycle – integer from 0 to MaxValue

Pulse Width Modulation (PWM)
Duty cycle is expressed on the Uno and Nano boards as integer in range 0..255

Time
analogWrite(PIN,127);
Duty cycle 50%
analogWrite(PIN,63);
Duty cycle 25%
analogWrite(PIN,191);
Duty cycle 75%

PWM Hardware
Not all pins are connected to hardware PWM generators
PWM-capable pins are shown on pin diagrams as a wavy line, or labelled “PWM”

Pulse Width Modulation (PWM)
Duty cycle is expressed on the Huzzah board as integer in range 0..1023

Time
analogWrite(PIN,511);
Duty cycle 50%
analogWrite(PIN,255);
Duty cycle 25%
analogWrite(PIN,766);
Duty cycle 75%

PWM Hardware
Not all pins are connected to hardware PWM generators
PWM-capable pins are shown on pin diagrams as a wavy line, or labelled “PWM”

Example: LED Fader
Code to make LED fade in and out
const int LED = 0;
int brightness = 0;
int fadeAmount = 5;

void setup() { pinMode(LED, OUTPUT); }

void loop() {
analogWrite(LED, brightness);
brightness = brightness + fadeAmount;
if (brightness <= 0 || brightness >= 1023) {
fadeAmount = -fadeAmount;
}
delay(30);
}
Reverse the direction at the ends of the fade
Brightness of LED
Fade amount
1023 on ESP8266
255 on ATmega

Motor Control using PWM
One of the commonest uses for PWM is to vary the speed of a motor
Directly applying PWM output of microcontroller to a motor isn’t feasible:
Microcontrollers are low power devices and can’t source enough current to drive a motor
Operating voltage of a microcontroller is lower than we would normally wish for a motor
Often we will want to drive the motor in forward or backward direction: microcontroller can only output one polarity of voltage

Motor Control using PWM
Directly applying PWM output of microcontroller to a motor isn’t feasible
Use a transistor to switch in/out larger voltage/current

Motor is inductive: when power is suddenly removed from motor a large reverse voltage results
Fly-back diode provides safe path for current flow whilst current decays away and restricts reverse voltage size

M

Ground
Power
PWM signal

H-Bridge circuits
Often we will want to drive the motor in forward or reverse direction
Simple transistor switch can only drive in one direction
H-bridge circuit contains 4 switches which can be enabled/disabled to allow forward and reverse drive when PWM is applied

M
Ground
Power

H-Bridge circuits
Often we will want to drive the motor in forward or reverse direction
Simple transistor switch can only drive in one direction
H-bridge circuit contains 4 switches which can be enabled/disabled to allow forward and reverse drive when PWM is applied

M
Ground
Power

Forward

M
Ground
Power

Reverse

M
Ground
Power

Braking

H-Bridge circuits
Forward drive
0
1
Enabled
Enabled
Disabled
Disabled

M
Power
Ground

H-Bridge circuits
Reverse drive

1
0
Disabled
Disabled
Enabled
Enabled

M
Power
Ground

Summary
Debounce is needed for mechanical switches
Can be done in software or hardware
Pulse width modulation can give us an intermediate average value between HIGH and LOW (as long as the driven device responds more slowly than the PWM frequency)
Special driver circuits are needed to use PWM on inductive high-current loads, such as motors

/docProps/thumbnail.jpeg