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-01
Revision:
1:1ac1f46ae264
Parent:
0:e89048c4d596

File content as of revision 1:1ac1f46ae264:

#include "mbed.h"
#include "FastPWM.h"
//setting pwm outputs
    FastPWM buck(D11,1);
    FastPWM boost(D10,1);
//seting analog input  
    AnalogIn analog_value(A0);
//setup serial coms
    Serial pc(SERIAL_TX, SERIAL_RX);
    DigitalOut myled(LED1);
//setup ticker 
    Ticker toggle_sample_ticker;
//setup gobal variables
    int count=0;
    int SampleSum=0;
    float sample[8];
//setup interrupt sample function
    void sampling() {
            if(count<=8) {
                 count=count+1;
                 sample[count]=(analog_value.read());
                 SampleSum=(sample[0]+sample[1]+sample[2]+sample[3]+sample[4]+sample[5]+sample[6]+sample[7]);
                            }
                else
                    count=0;
                    }
                    
//setup pwm function               
    void buckpwm(int pwm){
             if (pwm <=100) {
                    buck.pulsewidth_ticks( pwm );
                                } 
                            }

int main() {
  int i = 1;
  pc.printf("Hello World !\n");
//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
    buckpwm(1);   
    boost.pulsewidth_us(3);
//boostpwm(10);   
    while(1) {
               // toggle_sample_ticker.attach(&sampling, 0.1); // will cal mycode every 1/8000 second
                //test serial
                      wait(1);
                        pc.printf("This program runs since %d seconds.\n", SampleAverage);
                int SampleAverage=SampleSum/9;
    }
}