FRDM-K64F Code Share / Mbed 2 deprecated PWM_LED_RGB

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 //Basic PWM Led Control In Digital Port
00003 
00004 //DigitalOut RLED(PTB22);
00005 //DigitalOut GLED(PTE26);
00006 DigitalOut BLED(PTB21);
00007 float period=0.01;//Config. Period of PWM
00008 float t_on;
00009 float t_off;
00010 double duty=0; //Duty Cycle (%)
00011 unsigned int count=0;
00012 
00013 int main()
00014 {
00015     BLED=0;
00016     //Calculate on and off time
00017     t_on= period*duty;
00018     t_off= period - t_on;
00019     
00020     while (true) 
00021     {
00022             if(count<=2)//Time between changes in brightness = count*period
00023     count++;
00024     else
00025     {
00026     count=0;  
00027         if(duty<1)            
00028             duty+=0.01;//Adjust dimming speed
00029         else
00030         {
00031          duty=0;   
00032         }
00033     t_on= period*duty;
00034     t_off= period - t_on; 
00035     }
00036     //LED control
00037         wait(t_on);
00038         BLED=1;
00039     
00040         wait(t_off); 
00041         BLED=0;
00042     }
00043     
00044 }