This is an example of PWM on the STM32L476 Discovery board using the default PWM pin of PB3. This counts up to 100% (On) then counts down 0% (Off) and then counts up, repeating the pattern over and over.

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
jblackann
Date:
Mon Mar 19 20:01:16 2018 +0000
Parent:
2:c198a0f824eb
Commit message:
PWM on the STM32L4 Discovery board with on PB3. Counts up to 100% and then down to 0 and repeats

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r c198a0f824eb -r 9aa9bfe031bb main.cpp
--- a/main.cpp	Wed Jun 07 13:22:25 2017 +0000
+++ b/main.cpp	Mon Mar 19 20:01:16 2018 +0000
@@ -4,15 +4,44 @@
 
 DigitalOut myled(LED1);
 
+#define UP 1
+#define DOWN 2
+
 int main() {
     
+    uint8_t i; 
+    uint8_t count_dir;
+    
+    
     mypwm.period_ms(10);
     mypwm.pulsewidth_ms(1);
   
     printf("pwm set to %.2f %%\n", mypwm.read() * 100);
     
+    count_dir = UP;
     while(1) {
         myled = !myled;
-        wait(1);
+        for(i = 0; i < 10; i++ )
+        {
+            wait(0.1);
+            if(count_dir == UP)
+            {
+                mypwm = mypwm + 0.01;
+                if(mypwm == 1.00) // if hit one, start counting down
+                {
+                    count_dir = DOWN;
+                }
+            }
+            else if(count_dir == DOWN)
+            {
+                mypwm = mypwm - 0.01;
+                if(mypwm == 0.0)    // if hit zero, start counting up
+                {
+                    count_dir = UP;
+                }
+            }
+            
+        }
+        printf("pwm set to %.2f %%\n\r", mypwm.read() * 100);
     }
 }