Basic PWM Control in Digital Port that does not have PWM function. I use the RGB LED on board.

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
NghiaJenius
Date:
Thu Aug 07 12:20:10 2014 +0000
Commit message:
Publish Code;

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r c7e6fec3dbe9 main.cpp
--- /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
diff -r 000000000000 -r c7e6fec3dbe9 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Aug 07 12:20:10 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/6213f644d804
\ No newline at end of file