Example 12 Pulse-Width Modulation (PWM):
Motors and Servos
Lecture L8.1
PIM_9DP256
Block Diagram
PWM Port
PWM PinsPP0 – PP7
Pins 4,3,2,1,112,111,110,109
PWM PinsPP0 – PP7
Pins 4,3,2,1,112,111,110,109
Motor Driver Circuit
SSR
1 2 3 4 5 6 7
891011121314
7406
+5v V
470
V
PWMp31
GND
1
MOTOR
2 4
6
1
2 3
4
Solid-state relay
G3VM-61B1
MOS FET Relays
Motor – Generator Experiment
7406 G3VM-61B1 Solid-State
Relay
470 Ω
Motor Generator
pwm
+5V +5V +5V
VOUT
1
2
7
14
1
2
4
6
1N4004
Using an AC Relay
74LS240
+ -
Relay
+5V
180 OhmsAC Voltage
AC Voltage Out
Control Signal
pwm
duty
period
Pulse-Width Modulation
duty cycle = 100%duty
period
Table 12.1 C function calls for controlling the speed of a DC motor
C Function Call Meaning motor0_init(); Initialize PWM0 with 10 ms period motor1_init(); Initialize PWM1 with 10 ms period motor2_init(); Initialize PWM2 with 10 ms period motor3_init(); Initialize PWM3 with 10 ms period motor4_init(); Initialize PWM4 with 10 ms period motor5_init(); Initialize PWM5 with 10 ms period motor6_init(); Initialize PWM6 with 10 ms period motor7_init(); Initialize PWM7 with 10 ms period
motor0(int speed); Set speed of motor0 (0 – 255) motor1(int speed); Set speed of motor1 (0 – 255) motor2(int speed); Set speed of motor2 (0 – 255) motor3(int speed); Set speed of motor3 (0 – 255) motor4(int speed); Set speed of motor4 (0 – 255) motor5(int speed); Set speed of motor5 (0 – 255) motor6(int speed); Set speed of motor6 (0 – 255) motor7(int speed); Set speed of motor7 (0 – 255)
// Example 12a: Motor demo#include <hidef.h> /* common defines and macros */#include <mc9s12dp256.h> /* derivative information */#include "main_asm.h" /* interface to the assembly module */#pragma LINK_INFO DERIVATIVE "mc9s12dp256b"void main(void) { int val; int speed; PLL_init(); // set system clock frequency to 24 MHz ad0_enable(); // enable a/d converter 0 lcd_init(); // enable lcd motor1_init(); // enable 8-bit pwm1 for motor while(1) { val = ad0conv(7); // 0 - 1023 speed = val >> 2; // 0 - 255 set_lcd_addr(0x40); // 2nd line of lcd display write_int_lcd(speed); // display speed motor1(speed); // set motor speed ms_delay(100); // delay 100 ms }}
period = 20 ms
period = 20 ms
period = 20 ms
width = 1.5 ms Neutral
width = 1.1 ms + 45 degrees
width = 1.9 ms - 45 degrees
Controlling the Position of a Servo using PWM
Table 12.2 C function calls for controlling the position of a servo
C Function Call Meaning servo1_init(); Initialize PWM1 with 20 ms period servo3_init(); Initialize PWM3 with 20 ms period servo5_init(); Initialize PWM5 with 20 ms period servo7_init(); Initialize PWM7 with 20 ms period
set_servo1(int width); Set position of servo1 (3360 – 5760) set_servo3(int width); Set position of servo3 (3360 – 5760) set_servo5(int width); Set position of servo5 (3360 – 5760) set_servo7(int width); Set position of servo7 (3360 – 5760)
// Example 12b: Servo demo#include <hidef.h> /* common defines and macros */#include <mc9s12dp256.h> /* derivative information */#include "main_asm.h" /* interface to the assembly module */#pragma LINK_INFO DERIVATIVE "mc9s12dp256b"void main(void) { int val; int width; PLL_init(); // set system clock frequency to 24 MHz ad0_enable(); // enable a/d converter 0 lcd_init(); // enable lcd servo1_init(); // enable pwm1 for servo while(1) { val = ad0conv(7); // 0 - 1023 width = (val << 1) + 3536; // width: 3536 - 5582 set_lcd_addr(0x40); // line 2 of lcd display write_int_lcd(width); // display width on lcd set_servo1(width); // move servo to pos width ms_delay(100); // delay 100 ms }}