Simple PWM for motor control

Dependencies:   mbed

This program is for motor control with Arduino motor shield and LCP 1768.

For motor control with arduino motor shield, it needs direction signal and brake signal. Maybe brake signal is optional. So, the code use three pins, direction(gpio), brake(gpio) and PWM output.

- Schematic /media/uploads/hjjeon/lpc1768_motor_shield_schem.jpg

Committer:
hjjeon
Date:
Tue Nov 18 02:20:03 2014 +0000
Revision:
1:4f47e58cd882
Parent:
0:5f85a7752a04
Child:
2:4eb08f46fc91
Add comments

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hjjeon 0:5f85a7752a04 1 #include "mbed.h"
hjjeon 0:5f85a7752a04 2
hjjeon 0:5f85a7752a04 3 #define FORWARD 0
hjjeon 0:5f85a7752a04 4 #define BACKWARD 1
hjjeon 0:5f85a7752a04 5
hjjeon 0:5f85a7752a04 6 DigitalOut myled(LED1);
hjjeon 0:5f85a7752a04 7
hjjeon 0:5f85a7752a04 8 int main() {
hjjeon 0:5f85a7752a04 9
hjjeon 0:5f85a7752a04 10 Serial pc(USBTX, USBRX);
hjjeon 0:5f85a7752a04 11 pc.baud(115200);
hjjeon 0:5f85a7752a04 12
hjjeon 1:4f47e58cd882 13 PwmOut mypwm(p21);// NXP 1768 platform PWM output
hjjeon 1:4f47e58cd882 14 DigitalOut direction(p5);// NXP 1768 platform GPIO pin #5 output
hjjeon 1:4f47e58cd882 15 DigitalOut brake(p6);// NXP 1768 platform GPIO pin #6 output
hjjeon 0:5f85a7752a04 16
hjjeon 0:5f85a7752a04 17 direction = FORWARD;// FORWARD == 0;.
hjjeon 0:5f85a7752a04 18
hjjeon 0:5f85a7752a04 19 while(1) {
hjjeon 0:5f85a7752a04 20 myled = !myled;
hjjeon 1:4f47e58cd882 21 mypwm = mypwm + 0.05;// 0.05 is 5% duty pulse width
hjjeon 0:5f85a7752a04 22 wait(3);
hjjeon 0:5f85a7752a04 23 if( mypwm >= 1)
hjjeon 0:5f85a7752a04 24 {
hjjeon 0:5f85a7752a04 25 brake = 1; //stop
hjjeon 0:5f85a7752a04 26 }
hjjeon 0:5f85a7752a04 27 }
hjjeon 0:5f85a7752a04 28 }