Unit2 ARM
Unit2 ARM
Peripheral Interfacing-I
They must be able to convert input analog signals, for example from
microphone or temperature sensor, to digital data
They must also be able to convert digital signals to analog form, for
example
if driving a loudspeaker or dc motor
Effectively it "measures" the input voltage, and gives a binary output number
proportional to its size
The input range of the ADC is usually determined by the value of a voltage
reference
Introducing Analog output Data Conversion
The conversion is started by a digital input, called here SC
It takes finite time, and the ADC signals with the EOC line when the conversion is
complete
The resulting data can then be enabled onto a data bus using the OE line
Introducing Analog output Data Conversion
Introducing Analog output Data Conversion
Resolution and quantisation
By converting an analog signal to digital, we are effectively approximating it, as
any one digital output value has to represent a very small range of analog input
voltages, i.e. the width of any of the steps on the “staircase” n.
If we want to convert an analog signal that has a range 0-3.3 V to an 8-bit digital
signal, then there are 256 (i.e. 28) distinct output values. Each step has a width of
3.3/256 = 12.89 mV, and the worst case quantisation error is 6.45mV.
The mbed uses a 12-bit ADC. This leads to a step width of 3.3/2^12, or 0.8 mV;
the worst case quantisation error is therefore 0.4 mV.
The more samples taken, the more accurate the digital data will be. Samples are
normally taken at fixed periods (i.e., every 0.2ms) and define the rate of sampling
by the sampling frequency (the number of samples taken per second).
Introducing Analog output Data Conversion
The sample frequency needs to be chosen with respect to the rate of which the
sampled data is changing. If the sample frequency is too low then rapid changes in
the analog signal may not be obvious in the resulting digital data.
For this reason the Nyquist sampling criterion states that the sampling frequency
must be at least double that of the highest frequency of interest.
Introducing Analog output Data Conversion
The mbed has up to six analog inputs, on pins 15 to 20
Introducing Analog output Data Conversion
Exercise 1:Attach a potentiometer output to mbed pin 20. (Note:pin 20 is
connected to potentiometer Pot 2 of the application board)
Start a new mbed project and enter the code below.
This code will continuously display the analog input value when used with a host
PC terminal application.
Digital Output on the mbed-Using Light Emitting Diodes
LEDs now appear in all manner of shapes and sizes, the most familiar perhaps being the
single LED
Digital Output on the mbed-Using Light Emitting Diodes
The mbed, however, has 26 digital input/output (I/O) pins (pins 5-30) which can be configured
either as digital inputs or outputs
Digital Output on the mbed’ Using mbed External Pins
The digital I/O pins are named and configured to output using DigitalOut
#include "mbed.h"
DigitalOut redled(p5); //define and name a
digital output on pin 5
DigitalOut greenled(p6); //define and name a
digital output on pin 6
int main() {
while(1)
{ redled =
1;
greenled = 0;
wait(0.2);
redled = 0;
greenled = 1;
wait(0.2);
}
}
Using Digital Inputs-Connecting Switches to a Digital System
#include "mbed.h"
DigitalOut redled(p5);
DigitalIn opto_switch(p12);
int main() {
while(1) {
if (opto_switch==1) //input = 1 if beam interrupted
redled = 1; //switch led on if beam interrupted
else
redled = 0; //led off if no interruption
} //end of while
}
Seven-Segment Displays
(MSB) 0 0 0 0 0 1 1 1 (LSB)
Seven-Segment Displays
(MSB) DP g f e d c b a (LSB)
Seven-Segment Displays
(MSB) DP g f e d c b a (LSB)
#include "mbed.h"
BusOut display(p5,p6,p7,p8,p9,p10,p11,p12); //
segments dp,a,b,c,d,e,f,g
int main() {
while(1) {
for(int i=0;
i<4; i++) {
switch (i){
case 0: display = 0x3F; break; //display 0
case 1: display = 0x06; break; //display 1
case 2: display = 0x5B; break;
case 3: display = 0x4F; break;
} //end of switch
wait(0.2);
} //end of for
} //end of while
}
Switching Larger DC Loads
a resistive load, like a motor or heater using a bipolar transistor requires load current, and
the value of the current gain (β) of the transistor
devices in today’s electronics using MOSFET requires threshold gate-to-source voltage,
above which the transistor switches on.
inductive load, like a solenoid or DC requires freewheeling diode.
This is needed because any inductance with current flowing in it stores energy in the
magnetic field which surrounds it.
When that current is interrupted, in this case, by the transistor being switched off, the
energy has to be returned to the circuit.
This happens through the diode, which allows decaying current to continue to
circulate.
If the diode is not included then a high voltage transient occurs, which can/will destroy the
FET.
Switching Larger DC Loads
Switching Multiple Seven-Segment Displays
After processing the data, they may then need to convert digital data back to analog
form, for example, to drive a loudspeaker or DC motor
A digital-to-analog converter is a circuit which converts a binary input number into an
analog output.
Vr is the value of
the voltage reference,
D is the value of the binary input
word,
n is the number of bits in
that word, and
Vo is the output voltage
Analog Output-DAC
The number of possible output values is given by 2n, and the step size by Vr/ 2n; this is
called the resolution.
The maximum possible output value occurs when D = (2n -1)
The range of the DAC is the difference between its maximum and minimum output
values.
For example, a 6-bit DAC will have 64 possible output values; if it has a 3.2 V reference,
it will have a resolution (step size) of 50 mV.
Analog Output-DAC
Analog Output-DAC
The duty cycle is the proportion of time that the pulse is “on” or “high” and is
expressed as a percentage,
A 100% duty cycle therefore means “continuously on” and a 0% duty cycle means
“continuously off.”
PWM streams are easily generated by digital counters and comparators, which can
readily be designed into a microcontroller.
They can also be produced simply by program loops and a standard digital output,
with no dedicated hardware at all
Another Form of Analog Output: Pulse Width Modulation
Servo Control
A servo is a small, lightweight, rotary position control device, often used in radio
controlled cars and aircraft to control angular position of variables such as steering,
elevators, and rudders
Another Form of Analog Output: Pulse Width Modulation
A servo is a small, lightweight, rotary position control device, often used in radio
controlled cars and aircraft to control angular position of variables such as steering,
elevators, and rudders
#include "mbed.h"PwmOut speaker(p21);
void play_tone(float frequency, float volume, int interval, int rest) {
speaker.period(1.0 / frequency);
speaker = volume;
wait(interval);
speaker = 0.0;
wait(rest);}
int main(){
while(1) {
play_tone(659.0, 0.5, 1, 1); play_tone(554.0, 0.5, 1, 1);
play_tone(659.0, 0.5, 1, 1); play_tone(554.0, 0.5, 1, 1);
play_tone(440.0, 0.5, 1, 1); play_tone(494.0, 0.5, 1, 0.5);
play_tone(554.0, 0.5, 1, 0.5); play_tone(587.0, 0.5, 1, 1);
play_tone(494.0, 0.5, 1, 1); play_tone(659.0, 0.5, 1, 1);
play_tone(554.0, 0.5, 1, 1); play_tone(440.0, 0.5, 1, 2); }}