NUCLEO-F042K6 Simple demo using PWM to controll LED brightness

Dependencies:   mbed

main.cpp

Committer:
vodsejak
Date:
2018-01-31
Revision:
2:d5ecbd5f9886
Parent:
1:458a648ecbde
Child:
3:03bf4cc245ce

File content as of revision 2:d5ecbd5f9886:

#include "mbed.h" // import of mbed library (required)

/*******************************************************************************

  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.
  
*******************************************************************************/

PwmOut PWM(PA_8); // definition of PWM pin

int main()
{

    PWM.period_ms(5); // period of PWM - f=1/T -> f=1/0.005=200 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);
        }
        for(int i=5000;i>=0;i=i-10){
            PWM.pulsewidth_us(i);
            wait_ms(1000);
        }
    }
}