control de velocidad progresivo de motor CC en ambos sentidos

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 
00002 #include "mbed.h"
00003 
00004 PwmOut PWM1(PF_8);     // pwm outputs
00005 PwmOut PWM2(PF_7);
00006 DigitalOut led1(LED1);
00007 
00008 /* Function prototype */
00009 void motorControl(bool dir, float pwmVal);
00010 
00011 int main()
00012 {
00013     
00014     led1 = 1;
00015     wait(1);
00016     led1 = 0;
00017     PWM1.write(0);       // Duty cycles are initially zero   
00018     PWM2.write(0);   
00019     PWM1.period(0.02f);   // 1 kHz pwm frequency
00020     PWM2.period(0.02f);
00021     
00022     while(1)
00023     {
00024         for(float i=0;i<1;i=i+0.01f)  // increase speed from 0 to 1 in 5 seconds (50ms*100). 
00025         {
00026             motorControl(1,i);
00027             wait(0.05f);    
00028         }  
00029         for(float i=1;i>0;i=i-0.01f)  // decrease speed from 1 to 0 in 5 seconds (50ms*100).
00030         {
00031             motorControl(0,i);
00032             wait(0.05f);    
00033         }   
00034         led1 = 1;
00035         wait(0.5);
00036         led1 = 0;
00037     }    
00038 }
00039 
00040 // This is a motor control function which controls the speed and direction of
00041 // a motor. Motor drivers like L293D can be used.
00042 void motorControl(bool dir, float pwmVal)
00043 {
00044     if(dir==1)               // forward direction
00045     {
00046         PWM2.write(0);   
00047         PWM1.write(pwmVal);        
00048     }
00049     else if(dir==0)         // reverse direction
00050     {     
00051         PWM1.write(0);       
00052         PWM2.write(pwmVal);   
00053     }    
00054 }