Professional Documents
Culture Documents
Lecture 5 - Peripherals
Lecture 5 - Peripherals
Peripherals
Courtesy to Prof. Vahid for the resources used in preparing this lecture note.
Common Peripherals
– Timer
– Analog-to-Digital Converter
– Pulse Width Modulator
– Serial Communication
Timer
• Timer: measures time intervals
– To generate timed output events
– To measure input events Basic timer
– 10MHz
• Counter Size
– 16 bit
Counters
• Counter: like a timer, but
counts pulses on a general
input signal rather than clock Timer/counter
Clk
– e.g., count cars passing over a 2x1
mux
16-bit up
counter
16 Cnt
Reset
– Can often configure device as Mode
Clk 16-bit up
– terminal count Timer with a terminal counter 16 Cnt1
count
– Clock cycles Top1
Clk 16-bit up
= Desired time interval counter 16 Cnt
16-bit up
16 Cnt2
/ Clock period counter
Reset Top2
• Pre-scaler =
Top
– Divides clock Time with prescaler
Prescaler 16-bit up
– Increases range, Clk
counter
Terminal count
decreases resolution
Mode
Pulse width modulator
• Generates pulses with
pwm_o
adjustable duty cycle. clk
• control average voltage 25% duty cycle – average pwm_o is 1.25V
to electric device
pwm_o
– DC motor speed,
clk
dimmer lights
50% duty cycle – average pwm_o is 2.5V.
• REPLACES
pwm_o
– DC-DC converter
clk
– DAC
75% duty cycle – average pwm_o is 3.75V.
Stepper motor without
controller (driver)
8051
P2.4 GND/ +V 1, 1, 0, 0,
P2.3 0, 1, 1, 0,
P2.2
P2.1 0, 0, 1, 1,
P2.0
1, 0, 0, 1,
1, 1, 0, 0
Steppe
r
Motor
move_forward (){
1K
+V
move_backward (){
Q1
+V B
1K
A Q2 }
330
Controlling a DC
motor with a PWM
% of Maximum
clk clk_div counter Input Voltage RPM of DC Motor
Voltage Applied
( 0 – 254)
controls 0 0 0
how fast the
counter 8-bit 2.5 50 1840
increments comparator pwm_o
3.75 75 6900
void main(void){
5V
/* controls period */
PWMP = 0xff;
/* controls duty cycle */
PWM1 = 0x7f; From DC
process MOTOR
while(1){}; or
}
Analog-to-digital
converters
Vmax = 7.5V 1111 4 4
7.0V 1110
6.5V 1101
Successive-approximation method
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
N1
N2
N3 k_pressed
N4
M1
M2
M3
M4 4
key_code key_code
keypad controller
N=4, M=4
Stepper motor
controller
Sequence A B A’ B’
• Stepper motor: rotates fixed number 1 + + - -
of degrees when given a “step” signal 2 - + + -
3 - - + +
– In contrast, DC motor just rotates when 4 + - - +
power applied, coasts to stop 5 + + - -
Bias’/Set
5
6
12
11
GND
Phase A’
Clk 7 10 CW’/CCW
O|C 8 9 Full’/Half Step
Red A
White A’
Yellow B
Black B’
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 */
void delay(void){
CW’/CCW int i, j; clk=0; /* pulse clock */
10 P1.0 delay();
CLK for (i=0; i<1000; i++)
7 P1.1 clk=1;
for ( j=0; j<50; j++)
i = i + 0;
2 A’ B 15 } /*turn the motor backwards */
3 A B’ 14 cw=1; /* set direction
*/
clk=0; /* pulse clock */
delay();
clk=1;
}
The output pins on the stepper motor driver
Stepper do not provide enough current to drive the +V
Motor stepper motor. To amplify the current, a 1K
Q1
buffer is needed. One possible
A B
implementation of the buffers is pictured to the
left. Q1 is an MJE3055T NPN transistor and Q2
Q2 is an MJE2955T PNP transistor. A is
1K
connected to the 8051 microcontroller and B
is connected to the stepper motor.