PwmOut
Example 1
The on-board LEDs can be dimmed via 4 of the 6 PWM found in LPC1768
#include "mbed.h" PwmOut led1(LED1); PwmOut led2(LED2); PwmOut led3(LED3); PwmOut led4(LED4); int main() { while (1) { for (float f = 0.0; f < 1.0; f += 0.001) { led1 = f; led2 = f*f; led3 = (f+1.0)/2.0; led4 = 1.0 - f; wait(0.01); } } }
Example 2
The changes in light intensity is a bit awkward. Let's smooth it using sinusoidal function
It seems that M_PI
is not defined in the standard math.h
header.
We need to ensure that the value (duty cycle) to PwmOut
is positive. As sinusoidal function gives value between -1 to 1, we just need to add 1.0 to it.
#include "mbed.h" #include <math.h> #ifndef M_PI #define M_PI 3.1415 #endif PwmOut led1(LED1); PwmOut led2(LED2); PwmOut led3(LED3); PwmOut led4(LED4); int main() { float f = 0.0; while (1) { // Add 1.0 to ensure all values are positive led1 = sin( (f ) * M_PI / 180.0) + 1.0; led2 = sin( (f + 90.0) * M_PI / 180.0) + 1.0; led3 = sin( (f + 180.0) * M_PI / 180.0) + 1.0; led4 = sin( (f + 270.0) * M_PI / 180.0) + 1.0; f += 1.0; wait(0.01); } }
Information
Memory usage of the code above:
Flash (kB) | RAM (kB) | |
---|---|---|
Code | 0.8 | 0.0 |
Libraries | 15 | 0.3 |
Total | 15 | 0.3 |
Let's commented out the wait(0.01)
and time the program by letting it to display 1000 sets of value.
#include "mbed.h" #include <math.h> #ifndef M_PI #define M_PI 3.1415 #endif int main() { Timer t; int count = 0; printf("Timer start!\n"); t.start(); PwmOut led1(LED1); PwmOut led2(LED2); PwmOut led3(LED3); PwmOut led4(LED4); float f = 0.0; while (count < 1000) { // Add 1.0 to ensure all values are positive led1 = sin( (f ) * M_PI / 180.0) + 1.0; led2 = sin( (f + 90.0) * M_PI / 180.0) + 1.0; led3 = sin( (f + 180.0) * M_PI / 180.0) + 1.0; led4 = sin( (f + 270.0) * M_PI / 180.0) + 1.0; f += 1.0; count++; //wait(0.01); } t.stop(); printf("The time taken was %f seconds\n", t.read()); }
Information
Memory usage of the code above:
Flash (kB) | RAM (kB) | |
---|---|---|
Code | 0.8 | 0.0 |
Libraries | 18 | 0.3 |
Total | 18 | 0.3 |
The output of the program:
Timer start! The time taken was 0.140636 seconds
Now let's precompute the values to be display and store the results in an array. Let choose 256 records for each LED.
#include "mbed.h" #include <math.h> #ifndef M_PI #define M_PI 3.1415 #endif #define STP 256 /* Step */ #define LED 4 /* Number of LEDs that use PWM */ int main() { Timer t; int count = 0; printf("Timer start!\n"); t.start(); PwmOut Leds[] = {(LED1), (LED2), (LED3), (LED4)}; float y[STP][LED]; // Store all values float res = 360 / STP; // Resolution (degree per step) float x = 0.0; // Current x float ph = 360 / LED; // Phase different btw two successive LEDs for (int i = 0; i < STP; i++) { for (int j = 0; j < LED; j++) { y[i][j] = sin( (x + ph * j) * M_PI / 180.0) + 1.0; } x += res; // increment x by each resolution } while (count < 1000) { for (int i = 0; i < STP; i++) { for (int j = 0; j < LED; j++) { Leds[j] = y[i][j]; } count++; //wait(0.01); } } t.stop(); printf("The time taken was %f seconds\n", t.read()); }
Information
Memory usage of the code above:
Flash (kB) | RAM (kB) | |
---|---|---|
Code | 0.8 | 0.0 |
Libraries | 18 | 0.3 |
Total | 19 | 0.3 |
The output of the program:
Timer start! The time taken was 0.045611 seconds
There is a slight increase in memory usage to store the value but there is a great improvement in speed.
Please log in to post comments.