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

Dependencies:   mbed

Fork of pwm_example by Baser Kandehir

main.cpp

Committer:
BaserK
Date:
2015-07-10
Revision:
2:65f66eff154f
Parent:
1:554b65be8506
Child:
3:1174132d5190

File content as of revision 2:65f66eff154f:

/*   PWM motor driving example on LPC1768 
*
*    @author: Baser Kandehir 
*    @date: July 9, 2015
*    @license: Use this code however you'd like
*   
*    @description of the program: 
*
*    Program is written as an example for the PWM control on mbed LPC1768.
*    After making neccessary connections, this program can control speed 
*    and direction of a motor. Example code will increase motor speed to
*    its max in 5 seconds and decrease the motor speed from max to zero
*    in 5 seconds and it will keep doing this.    
*    
*    @connections:
*-------------------------------------------------------------- 
*    |LPC1768|        |Peripherals|
*    Pin 21 -------->  First input of the L293D
*    Pin 22 -------->  Second input of the L293D
*    GND ----------->  GND of any peripheral
*    VOUT (3.3 V) -->  VCC of the the motor driver
*---------------------------------------------------------------
*   Note: L293D is a very simple and useful motor driver that mostly used in
*   robotics. For detailed info and neccessary connections for L293D, please
*   refer to its datasheet.
*/

#include "mbed.h"

PwmOut PWM1(p21);     // pwm outputs
PwmOut PWM2(p22);

/* Function prototype */
void motorControl(bool dir, float pwmVal);

int main()
{
    while(1)
    {
        for(float i=0;i<1;i=i+0.01)  // increase speed from 0 to 1 in 5 seconds (50ms*100). 
        {
            motorControl(1,i);
            wait_ms(50);    
        }
        for(float i=1;i>0;i=i-0.01)  // decrease speed from 1 to 0 in 5 seconds (50ms*100).
        {
            motorControl(0,i);
            wait_ms(50);    
        }   
    }    
}

// This is a motor control function which controls the speed and direction of
// a motor. Motor drivers like L293D can be used.
void motorControl(bool dir, float pwmVal)
{
    PWM1.write(0);       // Duty cycles are initially zero   
    PWM2.write(0);   
    PWM1.period_ms(1);   // 1 kHz pwm frequency
    PWM2.period_ms(1);
    
    if(dir==1)               // forward direction
    {
        PWM1.write(pwmVal);        
        PWM2.write(0);   
    }
    else if(dir==0)         // reverse direction
    {
        PWM2.write(pwmVal);        
        PWM1.write(0);       
    }    
}