sample program for dimming LED with PWM.

Dependencies:   mbed

if your platform is F401 or F411, you can use LED1 on board. else connect pwm pin to LED.

Files at this revision

API Documentation at this revision

Comitter:
Match314
Date:
Sun Feb 22 03:38:45 2015 +0000
Commit message:
Up

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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sun Feb 22 03:38:45 2015 +0000
@@ -0,0 +1,27 @@
+/* sample for dimming led with pwm.
+ * Copyright (c) 2015 Match
+ */
+
+#include "mbed.h"
+
+// F401, F411 can use LED1.
+// Other platform, use pwm pin and connect led.
+PwmOut myled(LED1);
+
+int main() {
+    int duty = 0;   // unit:%
+    myled.period_ms(1);     // pwm frequency set 1kHz.
+    
+    while(1) {
+        // until 100%, increase by 5% in 50ms intervals
+        for (; duty <= 100; duty += 5) {
+            myled = duty / 100.0;
+            wait_ms(50);
+        }
+        // until 0%, decrease by 5% in 50ms intervals
+        for (; duty >= 0; duty -= 5) {
+            myled = duty / 100.0;
+            wait_ms(50);
+        }
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sun Feb 22 03:38:45 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/9ad691361fac
\ No newline at end of file