pwm

Dependencies:   mbed

Fork of PWM_LED_RGB by FRDM-K64F Code Share

Revision:
0:c7e6fec3dbe9
Child:
1:4b22168798e5
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Aug 07 12:20:10 2014 +0000
@@ -0,0 +1,44 @@
+#include "mbed.h"
+//Basic PWM Led Control In Digital Port
+
+//DigitalOut RLED(PTB22);
+//DigitalOut GLED(PTE26);
+DigitalOut BLED(PTB21);
+float period=0.01;//Config. Period of PWM
+float t_on;
+float t_off;
+double duty=0; //Duty Cycle (%)
+unsigned int count=0;
+
+int main()
+{
+    BLED=0;
+    //Calculate on and off time
+    t_on= period*duty;
+    t_off= period - t_on;
+    
+    while (true) 
+    {
+            if(count<=2)//Time between changes in brightness = count*period
+    count++;
+    else
+    {
+    count=0;  
+        if(duty<1)            
+            duty+=0.01;//Adjust dimming speed
+        else
+        {
+         duty=0;   
+        }
+    t_on= period*duty;
+    t_off= period - t_on; 
+    }
+    //LED control
+        wait(t_on);
+        BLED=1;
+    
+        wait(t_off); 
+        BLED=0;
+    }
+    
+} 
\ No newline at end of file