NUCLEO-F042K6 Simple demo using PWM to controll LED brightness

Dependencies:   mbed

Committer:
vodsejak
Date:
Sat Feb 17 20:28:45 2018 +0000
Revision:
4:79eee74e1bbd
Parent:
3:03bf4cc245ce
v3.0; bugfix

Who changed what in which revision?

UserRevisionLine numberNew 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 4:79eee74e1bbd 19 PWM.period_ms(5); // period of PWM - f=1/T -> f=1/0.005=200 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 4:79eee74e1bbd 26 wait_ms(2); // 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 4:79eee74e1bbd 30 wait_ms(2); // wait 4 ms (whole for takes 1 s)
vodsejak 2:d5ecbd5f9886 31 }
vodsejak 0:b2a1f2577916 32 }
vodsejak 0:b2a1f2577916 33 }