example for using fast pwm

Dependencies:   FastPWM mbed

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

Files at this revision

API Documentation at this revision

Comitter:
rouaze
Date:
Thu Mar 03 14:01:22 2016 +0000
Parent:
0:e89048c4d596
Child:
3:14ba055f4bb7
Commit message:
working pwm max 10% and 2x analog in

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Tue Mar 01 10:39:08 2016 +0000
+++ b/main.cpp	Thu Mar 03 14:01:22 2016 +0000
@@ -4,25 +4,16 @@
     FastPWM buck(D11,1);
     FastPWM boost(D10,1);
 //seting analog input  
-    AnalogIn analog_value(A0);
+    AnalogIn analog0_value(A0);
+    AnalogIn analog1_value(A1);
 //setup serial coms
 Serial pc(USBTX, USBRX);
 //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){
@@ -40,11 +31,29 @@
     buck.period_ticks (period_ticks);
     boost.period_ticks (period_ticks); 
 //define duty cycle
-    buckpwm(1);   
-    boost.pulsewidth_us(3);
+ int duty1 = 10;
+ int duty2 = 3;
+    buckpwm(duty1);   
+    boost.pulsewidth_us(duty2);
 //boostpwm(10);   
     while(1) {
-                toggle_sample_ticker.attach(&sampling, 0.0000011); // will cal mycode every 1/8000 second
-                pc.printf("\t%i\n",SampleSum);
+               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;
+                                    }
     }
 }