control de velocidad progresivo de un motor de CC en ambas direcciones

Dependencies:   mbed

Fork of pwm_example by Baser Kandehir

Committer:
BaserK
Date:
Thu Jul 09 11:57:47 2015 +0000
Revision:
1:554b65be8506
Parent:
0:908bb50b9f41
Child:
2:65f66eff154f
Comment update

Who changed what in which revision?

UserRevisionLine numberNew contents of line
BaserK 0:908bb50b9f41 1 /* PWM motor driving example on LPC1768
BaserK 0:908bb50b9f41 2 *
BaserK 0:908bb50b9f41 3 * @author: Baser Kandehir
BaserK 0:908bb50b9f41 4 * @date: July 9, 2015
BaserK 0:908bb50b9f41 5 * @license: Use this code however you'd like
BaserK 0:908bb50b9f41 6 *
BaserK 0:908bb50b9f41 7 * @description of the program:
BaserK 0:908bb50b9f41 8 *
BaserK 1:554b65be8506 9 * Program is written as an example for the PWM control on mbed LPC1768.
BaserK 0:908bb50b9f41 10 * After making neccessary connections, this program can control speed
BaserK 0:908bb50b9f41 11 * and direction of a motor. Example code will increase motor speed to
BaserK 0:908bb50b9f41 12 * its max in 5 seconds and decrease the motor speed from max to zero
BaserK 0:908bb50b9f41 13 * int 5 seconds and it will keep doing this.
BaserK 0:908bb50b9f41 14 *
BaserK 0:908bb50b9f41 15 * @connections:
BaserK 0:908bb50b9f41 16 *--------------------------------------------------------------
BaserK 0:908bb50b9f41 17 * |LPC1768| |Peripherals|
BaserK 0:908bb50b9f41 18 * Pin 21 --------> First input of the L293D
BaserK 0:908bb50b9f41 19 * Pin 22 --------> Second input of the L293D
BaserK 0:908bb50b9f41 20 * GND -----------> GND of any peripheral
BaserK 0:908bb50b9f41 21 * VOUT (3.3 V) --> VCC of the the motor driver
BaserK 0:908bb50b9f41 22 *---------------------------------------------------------------
BaserK 0:908bb50b9f41 23 * Note: L293D is a very simple and useful motor driver that mostly used in
BaserK 0:908bb50b9f41 24 * robotics. For detailed info and neccessary connections for L293D, please
BaserK 0:908bb50b9f41 25 * refer to its datasheet.
BaserK 0:908bb50b9f41 26 */
BaserK 0:908bb50b9f41 27
BaserK 0:908bb50b9f41 28 #include "mbed.h"
BaserK 0:908bb50b9f41 29
BaserK 0:908bb50b9f41 30 PwmOut PWM1(p21); // pwm outputs
BaserK 0:908bb50b9f41 31 PwmOut PWM2(p22);
BaserK 0:908bb50b9f41 32
BaserK 0:908bb50b9f41 33 /* Function prototype */
BaserK 0:908bb50b9f41 34 void motorControl(bool dir, float pwmVal);
BaserK 0:908bb50b9f41 35
BaserK 0:908bb50b9f41 36 int main()
BaserK 0:908bb50b9f41 37 {
BaserK 0:908bb50b9f41 38 while(1)
BaserK 0:908bb50b9f41 39 {
BaserK 0:908bb50b9f41 40 for(float i=0;i<1;i=i+0.01) // increase speed from 0 to 1 in 5 seconds (50ms*100).
BaserK 0:908bb50b9f41 41 {
BaserK 0:908bb50b9f41 42 motorControl(1,i);
BaserK 0:908bb50b9f41 43 wait_ms(50);
BaserK 0:908bb50b9f41 44 }
BaserK 0:908bb50b9f41 45 for(float i=1;i>0;i=i-0.01) // decrease speed from 1 to 0 in 5 seconds (50ms*100).
BaserK 0:908bb50b9f41 46 {
BaserK 0:908bb50b9f41 47 motorControl(0,i);
BaserK 0:908bb50b9f41 48 wait_ms(50);
BaserK 0:908bb50b9f41 49 }
BaserK 0:908bb50b9f41 50 }
BaserK 0:908bb50b9f41 51 }
BaserK 0:908bb50b9f41 52
BaserK 0:908bb50b9f41 53 // This is a motor control function which controls the speed and direction of
BaserK 0:908bb50b9f41 54 // a motor. Motor drivers like L293D can be used.
BaserK 0:908bb50b9f41 55 void motorControl(bool dir, float pwmVal)
BaserK 0:908bb50b9f41 56 {
BaserK 0:908bb50b9f41 57 PWM1.write(0); // Duty cycles are initially zero
BaserK 0:908bb50b9f41 58 PWM2.write(0);
BaserK 0:908bb50b9f41 59 PWM1.period_ms(1); // 1 kHz pwm frequency
BaserK 0:908bb50b9f41 60 PWM2.period_ms(1);
BaserK 0:908bb50b9f41 61
BaserK 0:908bb50b9f41 62 if(dir==1) // forward direction
BaserK 0:908bb50b9f41 63 {
BaserK 0:908bb50b9f41 64 PWM1.write(pwmVal);
BaserK 0:908bb50b9f41 65 PWM2.write(0);
BaserK 0:908bb50b9f41 66 }
BaserK 0:908bb50b9f41 67 else if(dir==0) // reverse direction
BaserK 0:908bb50b9f41 68 {
BaserK 0:908bb50b9f41 69 PWM2.write(pwmVal);
BaserK 0:908bb50b9f41 70 PWM1.write(0);
BaserK 0:908bb50b9f41 71 }
BaserK 0:908bb50b9f41 72 }