Pierre rouaze
/
fastpwmexample
example for using fast pwm
Example for the use of fast pwm on an stm nucleo F302R8.
Diff: main.cpp
- Revision:
- 3:14ba055f4bb7
- Parent:
- 2:82d62dda5cc9
- Child:
- 4:f229692e352f
diff -r 82d62dda5cc9 -r 14ba055f4bb7 main.cpp --- a/main.cpp Thu Mar 03 14:01:22 2016 +0000 +++ b/main.cpp Thu Mar 24 14:55:06 2016 +0000 @@ -17,7 +17,7 @@ //setup pwm function void buckpwm(int pwm){ - if (pwm <=100) { + if (pwm <=500) { buck.pulsewidth_ticks( pwm ); } } @@ -32,7 +32,7 @@ boost.period_ticks (period_ticks); //define duty cycle int duty1 = 10; - int duty2 = 3; + int duty2 = 5; buckpwm(duty1); boost.pulsewidth_us(duty2); //boostpwm(10); @@ -40,20 +40,25 @@ 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; + 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 ) { // If the value is greater than 2V then switch the LED on + if (duty1 <= 1 ) { // setting duty cycle limits duty1 = 1; } - if (sample1 > 2000 ) { // If the value is greater than 2V then switch the LED on - duty1 = duty1-1; + if (average > 2000 ) { // If the value is greater than 7Mv increase the duty cycle + duty1 = duty1+1; } - if (sample1 < 2000 ) { // If the value is greater than 2V then switch the LED on - duty1 = duty1+1; + if (average < 2000 ) { //If the value is smaller than 7Mv decrease the duty cycle + duty1 = duty1-1; } } }