Lecture#5 Chapter4 Peripherals

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 25

Embedded Systems Design: A Unified

Hardware/Software Introduction

Chapter 4 Standard Single Purpose


Processors: Peripherals

Masadeh
Introduction

• Single-purpose processors
– Performs specific computation task
– Custom single-purpose processors
• Designed by us for a unique task
– Standard single-purpose processors
• “Off-the-shelf” -- pre-designed for a common task
• a.k.a., peripherals
• serial transmission
• analog/digital conversions

Embedded Systems Design: A Unified


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Timers, counters, watchdog timers

• Timer: measures time intervals


– To generate timed output events
• e.g., hold traffic light green for 10 s
– To measure input events Basic timer
• e.g., measure a car’s speed 16-bit up 16 Cnt
Clk
counter
• Based on counting clock pulses
Top
• E.g., let Clk period be 10 ns
Reset
• And we count 20,000 Clk pulses
• Then 200 microseconds have passed
• 16-bit counter would count up to 65,535*10 ns =
655.35 microsec., resolution = 10 ns
• Top: indicates top count reached, wrap-around

Embedded Systems Design: A Unified


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Counters

• Counter: like a timer, but counts Timer/counter


pulses on a general input signal Clk
2x1 16-bit up 16 Cnt
rather than clock mux counter

– e.g., count cars passing over a sensor Cnt_in Top

– Can often configure device as either a Reset


Mode
timer or counter

Embedded Systems Design: A Unified


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Other timer structures
• Interval timer 16/32-bit timer

– Indicates when desired time Clk 16-bit up


counter 16 Cnt1
Timer with a terminal
interval has passed count
– We set terminal count to Top1
Clk 16-bit up
desired interval counter 16 Cnt
16-bit up
• Number of clock cycles counter 16 Cnt2

= Desired time interval /


Reset Top2
Clock period
=
• Cascaded counters Top Time with prescaler

• Prescaler Clk Prescaler 16-bit up


counter
– Divides clock Terminal count

– Increases range by
decreasing resolution Mode
(increase period),
Embedded Systems Design: A Unified
Hardware/Software Introduction, (c) 2000 Vahid/Givargis
• Timer’s range: MAX time interval the timer can
measure.
• Timer’s resolution: MIN interval it can measure
• Prescaler: A configurable clock-divider circuit

Embedded Systems Design: A Unified


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Example: Reaction Timer

• Reaction timer is an application that measures the time a person takes to respond
to a visual or audio stimulus.
• For example, turn a LED ON then the user press a button, the LCD display the
time (in ms) between seeing the Light and pressing the button (reaction of
seconds).
• This timer has No pre-scaler or terminal count register, but have a top signal.
• This timer allow to load an initial value
• We want to extend the range. 
• Load the timer with initial value, such that an overflow will happen after 1ms.
• Record the number of overflows= the number of milliseconds
– 1 instruction cycle= 6*83.33=0.5 microsec.
– X instruction cycle= 1 ms  X= 2000  intial timer value = 65535-2000=63535

Embedded Systems Design: A Unified


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Example: Reaction Timer
indicator reaction
/* main.c */
light button
#define MS_INIT 63535
LCD time: 100 ms void main(void){
int count_milliseconds = 0;

• Measure time between turning light on and user pushing configure timer mode
set Cnt to MS_INIT
button
wait a random amount of time
– 16-bit timer, Freq= 12 MHZ, clk period is 83.33 ns, turn on indicator light
start timer
counter increments every 6 cycles (1 instruction
cycle = 6 clock cycles) while (user has not pushed reaction button){
if(Top) {
– Resolution = 1 instruction cycle= 6*83.33=0.5 stop timer
set Cnt to MS_INIT
microsec. start timer
reset Top
– Range = 65535*0.5 microseconds = 32.77 count_milliseconds++;
}
milliseconds }
– Want program to count millisec., so initialize turn light off
printf(“time: %i ms“, count_milliseconds);
counter to 65535 – 1000/0.5 = 63535 }

Embedded Systems Design: A Unified


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Watchdog timer
• Watchdog timer: a Special type of timer.
• As regular timer, we configure watchdog timer with a real time value.
• Regular timer: generating a signal for us every X time units.
• Watchdog timer: We must generate a signal for the timer every X time
units. If we failed to generate such signal, the timer “times out” and
generate a signal indicate that we failed.

Embedded Systems Design: A Unified


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Watchdog timer
• Watchdog timers are commonly found in embedded systems and other
computer-controlled equipment where humans cannot easily access the
equipment or would be unable to react to faults in a timely manner
• The act of restarting a watchdog timer is commonly referred to as "kicking
the dog”
• CLRWDT (clear watchdog timer) instruction found in the instruction
set of some PIC microcontrollers.

Embedded Systems Design: A Unified


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Watchdog timer
• Must reset timer every
osc clk overflow overflow
X time unit, else timer prescaler scalereg timereg
to system reset
or
generates a signal interrupt

• Common use: detect checkreg

failure, self-reset
• Another use: timeouts /* main.c */ watchdog_reset_routine(){
/* checkreg is set so we can load value into
– e.g., ATM machine main(){ timereg. Zero is loaded into scalereg and
wait until card inserted 11070 is loaded into timereg */
– 16-bit timer, 2 call watchdog_reset_routine
checkreg = 1
microsec. resolution while(transaction in progress){ scalereg = 0
if(button pressed){ timereg = 11070
– timereg value = 2*(216- perform corresponding action }
1)–X = 131070–X call watchdog_reset_routine
} void interrupt_service_routine(){
– For 2 min., X = eject card
120,000 microsec. /* if watchdog_reset_routine not called every reset screen
< 2 minutes, interrupt_service_routine is }
called */
}

Embedded Systems Design: A Unified


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Watchdog timer
16 bit counter
osc clk overflow overflow to system reset
prescaler scalereg timereg
or
interrupt
11 bit counter
checkreg

• Prescaler divides osc by 12 to generate clk signal


• Scalereg is 11 bit up counter
• When Scalereg overflows, it back to 0, and the overflow cause the timereg which is
16 bit up counter to increment.
• When timereg overflows it cause the system to reset.,
• Checkreg must be enabled to reset the watchdog timer
• When a value is loaded into timereg, checkreg is automatically reset.
• Timereg is incremented every T seconds:
– T =12 * 211 * 1/( osc frequency) = 12 * 211 * 1/(12 * 106) = 0.002 (watchdog time resolution = 2 ms)
– timereg range is 0 to 65535  timer range is 0 to 131,070 ms=2.18 minutes
– Timereg value =131070 – X = 120,000 ms  X= 11070

Embedded Systems Design: A Unified


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Serial Transmission Using UARTs
• UART: Universal
embedded
Asynchronous Receiver device
1 1 0 1
Transmitter 0
0 1 1

– Takes parallel data and


transmits serially
10011011 10011011
– Receives serial data and
converts to parallel Sending UART Receiving UART
start bit end bit
• Serial communication is data

useful for
– Long distances
– When I/O pins are few
• UART requires a Timer to
shift at appropriate rate
1 0 0 1 1 0 1 1

Embedded Systems Design: A Unified


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Serial Transmission Using UARTs
• Parity: extra bit for simple error checking
• Start bit, stop bit
• Bit rate: a true measure of the number of bits that are sent over a
connection in one second.
• Baud rate: the number of signal changes that are transmitted over a
connection in 1 second.
– bit rate usually higher than baud rate

Embedded Systems Design: A Unified


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Pulse width modulator
• PWM : generating an output
signal that repeatedly switches pwm_o
between high and low values. clk
• Duty cycle: % time high
25% duty cycle – average pwm_o is 1.25V
– Square wave: 50% duty cycle
• PWM is used to generate a
pwm_o
clock-like signal to another
clk
device (i.e. , blink light at a
specific rate) 50% duty cycle – average pwm_o is 2.5V.

pwm_o

clk

75% duty cycle – average pwm_o is 3.75V.

Embedded Systems Design: A Unified


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Pulse width modulator
• Common use: Control the average current or voltage input to an electric device
– Direct current (DC) electrical motor rotates when its input voltage is set to high, with the
rotation speed proportional to the input voltage level.
– For example, the revolution per minutes (rpm) equal 100 the input voltage.
• Control the average input voltage to a DC motor, using:
– DC-to-DC converter circuit
– Digital to analog converter
– PWM (the simplest method)
• Using a PWM, we set the duty cycle to achieve the desired average voltage.
– Assume the PWM output is 5V when high, and 0Vwhen low. The average output voltage is 1.25V for 25% duty cycle, 2.5V
for 50$%duty cycle, and 3.75 for 75% duty cycle.
• Another use for PWM is to encode control commands in a single signal for use by
another device i.e., a radio-controlled car by sending pulses of different width

Embedded Systems Design: A Unified


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Controlling a DC motor with a PWM
% of Maximum
clk clk_div counter Input Voltage RPM of DC Motor
Voltage Applied
( 0 – 254)
controls how 0 0 0
fast the
counter 8-bit 2.5 50 1840
increments counter <
comparator pwm_o
cycle_high, 3.75 75 6900
pwm_o = 1
counter >= 5.0 100 9200
cycle_high cycle_high,
pwm_o = 0 Relationship between applied voltage and speed of
the DC Motor
Internal Structure of PWM

void main(void){ The PWM alone cannot drive the


DC motor, a possible way to 5V
/* controls period */ implement a driver is shown
PWMP = 0xff; below using an MJE3055T NPN
/* controls duty cycle */ transistor.
PWM1 = 0x7f; 5V From DC
processor MOTOR
while(1){};
} A

Embedded Systems Design: A Unified


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
LCD controller
• A liquid crystal display (LCD) is a low cost, low power device capable of displaying a
text and images.
• LCD is used widely in ESs (watches, fax and copy machines, calculators)
• LCD controller is used to provide a simple interface to an LCD ( 8 data inputs and 1
enable input)
• To send a byte to the LCD, we provide a value to the 8 inputs and pulse the enable.
• The byte may be
• Control word: Initialize the LCD, Clear the display, set position, display brightness,..
• Data word: ASCII character to be displayed

Embedded Systems Design: A Unified


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
LCD controller

E void WriteChar(char c){


communications
R/W bus
RS = 1; /* indicate data being sent */
RS
DATA_BUS = c; /* send data to LCD */
DB7–DB0 EnableLCD(45); /* toggle the LCD with appropriate delay */
8
}
microcontroller LCD
controller

CODES RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0 Description
I/D = 1 cursor moves left DL = 1 8-bit
0 0 0 0 0 0 0 0 0 1 Clears all display, return cursor home
I/D = 0 cursor moves right DL = 0 4-bit
S = 1 with display shift N = 1 2 rows 0 0 0 0 0 0 0 0 1 * Returns cursor home
S/C =1 display shift N = 0 1 row
Sets cursor move direction and/or
0 0 0 0 0 0 0 1 I/D S
S/C = 0 cursor movement F = 1 5x10 dots specifies not to shift display
R/L = 1 shift to right F = 0 5x7 dots ON/OFF of all display(D), cursor
0 0 0 0 0 0 1 D C B
ON/OFF (C), and blink position (B)
R/L = 0 shift to left
0 0 0 0 0 1 S/C R/L * * Move cursor and shifts display

Sets interface data length, number of


0 0 0 0 1 DL N F * *
display lines, and character font

1 0 WRITE DATA Writes Data

Embedded Systems Design: A Unified


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Keypad controller

N1
N2
N3 k_pressed
N4

M1
M2
M3
M4 4
key_code key_code

keypad controller

N=4, M=4

Embedded Systems Design: A Unified


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Stepper motor controller

• Stepper motor: rotates fixed number Sequence


1
A
+
B
+
A’
-
B’
-
of degrees when given a “step” 2 - + + -
3 - - + +
signal 4 + - - +
– In contrast, DC motor just rotates when 5 + + - -

power applied, coasts to stop Vd 1 16 Vm

• Rotation achieved by applying A’


A
2
3
MC3479P 15
14
B
B’

specific voltage sequence to coils GND


4
5
13
12
GND

• Controller greatly simplifies this


Bias’/Set 6 11 Phase A’
Clk 7 10 CW’/CCW
O|C 8 9 Full’/Half Step

Red A
White A’
Yellow B
Black B’

Embedded Systems Design: A Unified


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Stepper motor with controller (driver)
/* main.c */
void main(void){
MC3479P sbit clk=P1^1;
sbit cw=P1^0; */turn the motor forward */
Stepper Motor
cw=0; /* set direction */
Driver 8051 clk=0; /* pulse clock */
void delay(void){
CW’/CCW int i, j; delay();
10 P1.0 clk=1;
CLK for (i=0; i<1000; i++)
7 P1.1
for ( j=0; j<50; j++)
i = i + 0; /*turn the motor backwards */
2 A’ B 15 } cw=1; /* set direction */
3 A B’ 14 clk=0; /* pulse clock */
delay();
clk=1;

The output pins on the stepper motor driver do not


Stepper provide enough current to drive the stepper motor. +V
Motor To amplify the current, a buffer is needed. One 1K
Q1
possible implementation of the buffers is pictured
A B
to the left. Q1 is an MJE3055T NPN transistor
and Q2 is an MJE2955T PNP transistor. A is Q2
connected to the 8051 microcontroller and B is
1K
connected to the stepper motor.

Embedded Systems Design: A Unified


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Stepper motor without controller (driver)
8051 /*main.c*/ /* counter clockwise movement */
sbit notA=P2^0; if(dir==0){
P2.4 GND/ +V
sbit isA=P2^1; for(y=0; y<=step; y++){
sbit notB=P2^2; for(z=19; z>=0; z - 4){
P2.3
sbit isB=P2^3; isA=lookup[z];
P2.2
sbit dir=P2^4; isB=lookup[z-1];
P2.1
notA=lookup[z -2];
P2.0
void delay(){ notB=lookup[z-3];
int a, b; delay( );
for(a=0; a<5000; a++) }
Stepper for(b=0; b<10000; b++) }
Motor a=a+0; }
} }
void main( ){
void move(int dir, int steps) { int z;
A possible way to implement the buffers is located int y, z; int lookup[20] = {
below. The 8051 alone cannot drive the stepper motor, so /* clockwise movement */ 1, 1, 0, 0,
several transistors were added to increase the current going if(dir == 1){ 0, 1, 1, 0,
to the stepper motor. Q1 are MJE3055T NPN transistors for(y=0; y<=steps; y++){ 0, 0, 1, 1,
and Q3 is an MJE2955T PNP transistor. A is connected to for(z=0; z<=19; z+4){ 1, 0, 0, 1,
the 8051 microcontroller and B is connected to the stepper isA=lookup[z]; 1, 1, 0, 0 };
motor. +V
isB=lookup[z+1]; while(1){
1K notA=lookup[z+2]; /*move forward, 15 degrees (2 steps) */
Q1
notB=lookup[z+3]; move(1, 2);
+V B
1K
delay(); /* move backwards, 7.5 degrees (1step)*/
} move(0, 1);
A Q2
} }
330 } }

Embedded Systems Design: A Unified


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Analog-to-digital converters

Vmax = 7.5V 1111 4 4


7.0V 1110
6.5V 1101 3 3

analog output (V)


analog input (V)
6.0V 1100
5.5V 1011
2 2
5.0V 1010
4.5V 1001
4.0V 1 1
1000
3.5V 0111
3.0V 0110 time time
t1 t2 t3 t4 t1 t2 t3 t4
2.5V 0101
2.0V 0100 0100 1000 0110 0101 0100 1000 0110 0101
1.5V 0011 Digital output Digital input
1.0V 0010
0.5V 0001
0V 0000

proportionality analog to digital digital to analog

Embedded Systems Design: A Unified


Hardware/Software Introduction, (c) 2000 Vahid/Givargis
Digital-to-analog conversion using
successive approximation
Given an analog input signal whose voltage should range from 0 to 15 volts, and an 8-bit digital encoding, calculate the correct encoding for
5 volts. Then trace the successive-approximation approach to find the correct encoding.

5/15 = d/(28-1) Encoding: 01010101


d= 85

Successive-approximation method

½(Vmax – Vmin) = 7.5 volts 0 0 0 0 0 0 0 0 ½(5.63 + 4.69) = 5.16 volts 0 1 0 1 0 0 0 0


Vmax = 7.5 volts. Vmax = 5.16 volts.

½(7.5 + 0) = 3.75 volts 0 1 0 0 0 0 0 0 ½(5.16 + 4.69) = 4.93 volts 0 1 0 1 0 1 0 0


Vmin = 3.75 volts. Vmin = 4.93 volts.

½(7.5 + 3.75) = 5.63 volts 0 1 0 0 0 0 0 0 ½(5.16 + 4.93) = 5.05 volts 0 1 0 1 0 1 0 0


Vmax = 5.63 volts Vmax = 5.05 volts.

½(5.63 + 3.75) = 4.69 volts 0 1 0 1 0 0 0 0 ½(5.05 + 4.93) = 4.99 volts 0 1 0 1 0 1 0 1


Vmin = 4.69 volts.

Embedded Systems Design: A Unified


Hardware/Software Introduction, (c) 2000 Vahid/Givargis

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy