Simple PWM for motor control
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
main.cpp@2:4eb08f46fc91, 2014-11-18 (annotated)
- Committer:
- hjjeon
- Date:
- Tue Nov 18 02:22:12 2014 +0000
- Revision:
- 2:4eb08f46fc91
- Parent:
- 1:4f47e58cd882
Add comments
Who changed what in which revision?
User | Revision | Line number | New 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 | |
hjjeon | 0:5f85a7752a04 | 7 | int main() { |
hjjeon | 0:5f85a7752a04 | 8 | |
hjjeon | 0:5f85a7752a04 | 9 | Serial pc(USBTX, USBRX); |
hjjeon | 0:5f85a7752a04 | 10 | pc.baud(115200); |
hjjeon | 0:5f85a7752a04 | 11 | |
hjjeon | 2:4eb08f46fc91 | 12 | // This program is for NXP LPC1768. If you use other platform, change pin number!! ============ |
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 | 2:4eb08f46fc91 | 16 | DigitalOut myled(LED1);//For LED blinking. |
hjjeon | 2:4eb08f46fc91 | 17 | //============================================================================================= |
hjjeon | 0:5f85a7752a04 | 18 | |
hjjeon | 0:5f85a7752a04 | 19 | direction = FORWARD;// FORWARD == 0;. |
hjjeon | 0:5f85a7752a04 | 20 | |
hjjeon | 0:5f85a7752a04 | 21 | while(1) { |
hjjeon | 0:5f85a7752a04 | 22 | myled = !myled; |
hjjeon | 1:4f47e58cd882 | 23 | mypwm = mypwm + 0.05;// 0.05 is 5% duty pulse width |
hjjeon | 0:5f85a7752a04 | 24 | wait(3); |
hjjeon | 0:5f85a7752a04 | 25 | if( mypwm >= 1) |
hjjeon | 0:5f85a7752a04 | 26 | { |
hjjeon | 0:5f85a7752a04 | 27 | brake = 1; //stop |
hjjeon | 0:5f85a7752a04 | 28 | } |
hjjeon | 0:5f85a7752a04 | 29 | } |
hjjeon | 0:5f85a7752a04 | 30 | } |