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-03
Revision:
2:82d62dda5cc9
Parent:
0:e89048c4d596
Child:
3:14ba055f4bb7

File content as of revision 2:82d62dda5cc9:

#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 <=100) {
                    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 = 3;
    buckpwm(duty1);   
    boost.pulsewidth_us(duty2);
//boostpwm(10);   
    while(1) {
               buckpwm(duty1);   
                boost.pulsewidth_us(duty2);
               //wait(1);
               float sample1= analog0_value.read()*3500;
               float sample2= analog1_value.read()*3500;
                pc.printf("%.0f and %.0f duty1 %d \n\r  ", sample1,sample2,duty1);
                if (duty1 >= 100 ) { // If the value is greater than 2V then switch the LED on
                                  duty1 = 100;
                                    }
                if (duty1 <= 1 ) { // If the value is greater than 2V then switch the LED on
                                  duty1 = 1;
                                    }
                if (sample1 > 2000 ) { // If the value is greater than 2V then switch the LED on
                                  duty1 = duty1-1;
                                    }
                if (sample1 < 2000 ) { // If the value is greater than 2V then switch the LED on
                                  duty1 = duty1+1;
                                    }
    }
}