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@0:5f85a7752a04, 2014-11-18 (annotated)
- Committer:
- hjjeon
- Date:
- Tue Nov 18 02:16:00 2014 +0000
- Revision:
- 0:5f85a7752a04
- Child:
- 1:4f47e58cd882
Simple PWM for motor control
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 | 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 | 0:5f85a7752a04 | 13 | PwmOut mypwm(p21); |
hjjeon | 0:5f85a7752a04 | 14 | DigitalOut direction(p5); |
hjjeon | 0:5f85a7752a04 | 15 | DigitalOut brake(p6); |
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 | 0:5f85a7752a04 | 21 | mypwm = mypwm + 0.05; |
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 | } |