example for using fast pwm

Dependencies:   FastPWM mbed

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

Committer:
rouaze
Date:
Thu Mar 24 14:55:06 2016 +0000
Revision:
3:14ba055f4bb7
Parent:
2:82d62dda5cc9
Child:
4:f229692e352f
WORKING CURRENT SOURCE CONTROL;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rouaze 0:e89048c4d596 1 #include "mbed.h"
rouaze 0:e89048c4d596 2 #include "FastPWM.h"
rouaze 0:e89048c4d596 3 //setting pwm outputs
rouaze 0:e89048c4d596 4 FastPWM buck(D11,1);
rouaze 0:e89048c4d596 5 FastPWM boost(D10,1);
rouaze 0:e89048c4d596 6 //seting analog input
rouaze 2:82d62dda5cc9 7 AnalogIn analog0_value(A0);
rouaze 2:82d62dda5cc9 8 AnalogIn analog1_value(A1);
rouaze 0:e89048c4d596 9 //setup serial coms
rouaze 0:e89048c4d596 10 Serial pc(USBTX, USBRX);
rouaze 0:e89048c4d596 11 //setup ticker
rouaze 0:e89048c4d596 12 Ticker toggle_sample_ticker;
rouaze 0:e89048c4d596 13 //setup gobal variables
rouaze 2:82d62dda5cc9 14
rouaze 0:e89048c4d596 15 //setup interrupt sample function
rouaze 2:82d62dda5cc9 16
rouaze 0:e89048c4d596 17
rouaze 0:e89048c4d596 18 //setup pwm function
rouaze 0:e89048c4d596 19 void buckpwm(int pwm){
rouaze 3:14ba055f4bb7 20 if (pwm <=500) {
rouaze 0:e89048c4d596 21 buck.pulsewidth_ticks( pwm );
rouaze 0:e89048c4d596 22 }
rouaze 0:e89048c4d596 23 }
rouaze 0:e89048c4d596 24
rouaze 0:e89048c4d596 25 int main() {
rouaze 0:e89048c4d596 26 //setup pwm
rouaze 0:e89048c4d596 27 uint32_t period_ticks=720;
rouaze 0:e89048c4d596 28 buck.prescaler(1);
rouaze 0:e89048c4d596 29 boost.prescaler(1);
rouaze 0:e89048c4d596 30 // define period of pwm...
rouaze 0:e89048c4d596 31 buck.period_ticks (period_ticks);
rouaze 0:e89048c4d596 32 boost.period_ticks (period_ticks);
rouaze 0:e89048c4d596 33 //define duty cycle
rouaze 2:82d62dda5cc9 34 int duty1 = 10;
rouaze 3:14ba055f4bb7 35 int duty2 = 5;
rouaze 2:82d62dda5cc9 36 buckpwm(duty1);
rouaze 2:82d62dda5cc9 37 boost.pulsewidth_us(duty2);
rouaze 0:e89048c4d596 38 //boostpwm(10);
rouaze 0:e89048c4d596 39 while(1) {
rouaze 2:82d62dda5cc9 40 buckpwm(duty1);
rouaze 2:82d62dda5cc9 41 boost.pulsewidth_us(duty2);
rouaze 2:82d62dda5cc9 42 //wait(1);
rouaze 3:14ba055f4bb7 43 float sample1= analog0_value.read()*3300;
rouaze 3:14ba055f4bb7 44 float sample2= analog0_value.read()*3300;
rouaze 3:14ba055f4bb7 45 float sample3= analog0_value.read()*3300;
rouaze 3:14ba055f4bb7 46 float sample4= analog0_value.read()*3300;
rouaze 3:14ba055f4bb7 47 float average;
rouaze 3:14ba055f4bb7 48 average=(sample1+sample2+sample3+sample4)/4;
rouaze 3:14ba055f4bb7 49 float sample5= analog1_value.read()*3500;
rouaze 3:14ba055f4bb7 50 //pc.printf("%.0f and %.0f duty1 %d \n\r ", average,sample5,duty1);
rouaze 3:14ba055f4bb7 51 if (duty1 >= 500 ) { // setting duty cycle limits
rouaze 3:14ba055f4bb7 52 duty1 = 500;
rouaze 2:82d62dda5cc9 53 }
rouaze 3:14ba055f4bb7 54 if (duty1 <= 1 ) { // setting duty cycle limits
rouaze 2:82d62dda5cc9 55 duty1 = 1;
rouaze 2:82d62dda5cc9 56 }
rouaze 3:14ba055f4bb7 57 if (average > 2000 ) { // If the value is greater than 7Mv increase the duty cycle
rouaze 3:14ba055f4bb7 58 duty1 = duty1+1;
rouaze 2:82d62dda5cc9 59 }
rouaze 3:14ba055f4bb7 60 if (average < 2000 ) { //If the value is smaller than 7Mv decrease the duty cycle
rouaze 3:14ba055f4bb7 61 duty1 = duty1-1;
rouaze 2:82d62dda5cc9 62 }
rouaze 0:e89048c4d596 63 }
rouaze 0:e89048c4d596 64 }