NUCLEO-F042K6 Simple demo using PWM to controll LED brightness
Dependencies: mbed
Diff: main.cpp
- Revision:
- 3:03bf4cc245ce
- Parent:
- 2:d5ecbd5f9886
- Child:
- 4:79eee74e1bbd
--- a/main.cpp Wed Jan 31 21:15:53 2018 +0000 +++ b/main.cpp Wed Jan 31 21:48:21 2018 +0000 @@ -4,9 +4,11 @@ EXAMPLE DESCRIPTION - Sets PWM out on pin PA_8 with f=2 Hz and duty cycle = 0.5. The best use is to - connect LED to pin PA_8 (using wiring with transitor) to see effect of PWM. - + Sets PWM out on pin PA_8 with f=200 Hz. Then changes duty cycle from 0 to 1 + in 500 steps with 4 ms wait (from 0 to 1 in 1 s) and back from 1 to 0. + The best use is to connect LED to pin PA_8 (using wiring with transitor) + to see effect of PWM. + *******************************************************************************/ PwmOut PWM(PA_8); // definition of PWM pin @@ -14,18 +16,18 @@ int main() { - PWM.period_ms(5); // period of PWM - f=1/T -> f=1/0.005=200 Hz + PWM.period_ms(5); // period of PWM - f=1/T -> f=1/0.005=4200 Hz PWM.pulsewidth_us(0); // duration of active pulse //sets duty cycle - duty cycle = pulsewidth/period while(1) { for(int i=0;i<=5000;i=i+10){ - PWM.pulsewidth_us(i); - wait_ms(1000); + PWM.pulsewidth_us(i); // increment duty cycle + wait_ms(4); // wait 4 ms (whole for takes 1 s) } for(int i=5000;i>=0;i=i-10){ - PWM.pulsewidth_us(i); - wait_ms(1000); + PWM.pulsewidth_us(i); // decrement duty cycle + wait_ms(4); // wait 4 ms (whole for takes 1 s) } } }