NUCLEO-F042K6 Simple demo using PWM to controll LED brightness
Dependencies: mbed
main.cpp@3:03bf4cc245ce, 2018-01-31 (annotated)
- Committer:
- vodsejak
- Date:
- Wed Jan 31 21:48:21 2018 +0000
- Revision:
- 3:03bf4cc245ce
- Parent:
- 2:d5ecbd5f9886
- Child:
- 4:79eee74e1bbd
v2.0; added commentry
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vodsejak | 1:458a648ecbde | 1 | #include "mbed.h" // import of mbed library (required) |
vodsejak | 0:b2a1f2577916 | 2 | |
vodsejak | 1:458a648ecbde | 3 | /******************************************************************************* |
vodsejak | 1:458a648ecbde | 4 | |
vodsejak | 1:458a648ecbde | 5 | EXAMPLE DESCRIPTION |
vodsejak | 0:b2a1f2577916 | 6 | |
vodsejak | 3:03bf4cc245ce | 7 | Sets PWM out on pin PA_8 with f=200 Hz. Then changes duty cycle from 0 to 1 |
vodsejak | 3:03bf4cc245ce | 8 | in 500 steps with 4 ms wait (from 0 to 1 in 1 s) and back from 1 to 0. |
vodsejak | 3:03bf4cc245ce | 9 | The best use is to connect LED to pin PA_8 (using wiring with transitor) |
vodsejak | 3:03bf4cc245ce | 10 | to see effect of PWM. |
vodsejak | 3:03bf4cc245ce | 11 | |
vodsejak | 1:458a648ecbde | 12 | *******************************************************************************/ |
vodsejak | 1:458a648ecbde | 13 | |
vodsejak | 1:458a648ecbde | 14 | PwmOut PWM(PA_8); // definition of PWM pin |
vodsejak | 1:458a648ecbde | 15 | |
vodsejak | 1:458a648ecbde | 16 | int main() |
vodsejak | 1:458a648ecbde | 17 | { |
vodsejak | 1:458a648ecbde | 18 | |
vodsejak | 3:03bf4cc245ce | 19 | PWM.period_ms(5); // period of PWM - f=1/T -> f=1/0.005=4200 Hz |
vodsejak | 2:d5ecbd5f9886 | 20 | PWM.pulsewidth_us(0); // duration of active pulse |
vodsejak | 1:458a648ecbde | 21 | //sets duty cycle - duty cycle = pulsewidth/period |
vodsejak | 1:458a648ecbde | 22 | |
vodsejak | 0:b2a1f2577916 | 23 | while(1) { |
vodsejak | 2:d5ecbd5f9886 | 24 | for(int i=0;i<=5000;i=i+10){ |
vodsejak | 3:03bf4cc245ce | 25 | PWM.pulsewidth_us(i); // increment duty cycle |
vodsejak | 3:03bf4cc245ce | 26 | wait_ms(4); // wait 4 ms (whole for takes 1 s) |
vodsejak | 2:d5ecbd5f9886 | 27 | } |
vodsejak | 2:d5ecbd5f9886 | 28 | for(int i=5000;i>=0;i=i-10){ |
vodsejak | 3:03bf4cc245ce | 29 | PWM.pulsewidth_us(i); // decrement duty cycle |
vodsejak | 3:03bf4cc245ce | 30 | wait_ms(4); // wait 4 ms (whole for takes 1 s) |
vodsejak | 2:d5ecbd5f9886 | 31 | } |
vodsejak | 0:b2a1f2577916 | 32 | } |
vodsejak | 0:b2a1f2577916 | 33 | } |