example for using fast pwm

Dependencies:   FastPWM mbed

Example for the use of fast pwm on an stm nucleo F302R8.

main.cpp

Committer:
rouaze
Date:
2016-03-24
Revision:
3:14ba055f4bb7
Parent:
2:82d62dda5cc9
Child:
4:f229692e352f

File content as of revision 3:14ba055f4bb7:

#include "mbed.h"
#include "FastPWM.h"
//setting pwm outputs
    FastPWM buck(D11,1);
    FastPWM boost(D10,1);
//seting analog input  
    AnalogIn analog0_value(A0);
    AnalogIn analog1_value(A1);
//setup serial coms
Serial pc(USBTX, USBRX);
//setup ticker 
    Ticker toggle_sample_ticker;
//setup gobal variables
    
//setup interrupt sample function
   
                    
//setup pwm function               
    void buckpwm(int pwm){
             if (pwm <=500) {
                    buck.pulsewidth_ticks( pwm );
                                } 
                            }

int main() {
//setup pwm
    uint32_t period_ticks=720;
    buck.prescaler(1);
    boost.prescaler(1);
// define period of pwm...
    buck.period_ticks (period_ticks);
    boost.period_ticks (period_ticks); 
//define duty cycle
 int duty1 = 10;
 int duty2 = 5;
    buckpwm(duty1);   
    boost.pulsewidth_us(duty2);
//boostpwm(10);   
    while(1) {
               buckpwm(duty1);   
                boost.pulsewidth_us(duty2);
               //wait(1);
               float sample1= analog0_value.read()*3300;
               float sample2= analog0_value.read()*3300;
               float sample3= analog0_value.read()*3300;
               float sample4= analog0_value.read()*3300;
                float average;
                average=(sample1+sample2+sample3+sample4)/4;
               float sample5= analog1_value.read()*3500;
            //pc.printf("%.0f and %.0f duty1 %d \n\r  ", average,sample5,duty1);
                if (duty1 >= 500 ) { // setting duty cycle limits
                                  duty1 = 500;
                                    }
                if (duty1 <= 1 ) { // setting duty cycle limits
                                  duty1 = 1;
                                    }
                if (average > 2000 ) { // If the value is greater than 7Mv increase the duty cycle
                                  duty1 = duty1+1;
                                    }
                if (average < 2000 ) { //If the value is smaller than 7Mv decrease the duty cycle
                                  duty1 = duty1-1;
                                    }
    }
}