My Example PWM program with method 2

Dependencies:   mbed

Committer:
neutron_striker
Date:
Sun Feb 15 20:01:59 2015 +0000
Revision:
0:77fda962237d
My Example PWM program with method 2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
neutron_striker 0:77fda962237d 1 #include "mbed.h"
neutron_striker 0:77fda962237d 2
neutron_striker 0:77fda962237d 3 PwmOut mypwm(LED1);
neutron_striker 0:77fda962237d 4
neutron_striker 0:77fda962237d 5
neutron_striker 0:77fda962237d 6
neutron_striker 0:77fda962237d 7 int main() {
neutron_striker 0:77fda962237d 8
neutron_striker 0:77fda962237d 9
neutron_striker 0:77fda962237d 10
neutron_striker 0:77fda962237d 11 printf("pwm set to %.2f %%\n", mypwm.read() * 100);
neutron_striker 0:77fda962237d 12
neutron_striker 0:77fda962237d 13 float pwm=0.0;
neutron_striker 0:77fda962237d 14
neutron_striker 0:77fda962237d 15 while(1) {
neutron_striker 0:77fda962237d 16
neutron_striker 0:77fda962237d 17 pwm = pwm + 0.05; // instead of this pattern we can also write like check below
neutron_striker 0:77fda962237d 18 mypwm.write(pwm);
neutron_striker 0:77fda962237d 19 if(pwm >= 1.0)
neutron_striker 0:77fda962237d 20 pwm = 0.0;
neutron_striker 0:77fda962237d 21 wait(0.03);
neutron_striker 0:77fda962237d 22
neutron_striker 0:77fda962237d 23 /*
neutron_striker 0:77fda962237d 24 mypwm = mypwm + 0.01;
neutron_striker 0:77fda962237d 25 if(mypwm >= 1.0)
neutron_striker 0:77fda962237d 26 mypwm = 0.0;
neutron_striker 0:77fda962237d 27 wait(0.05);
neutron_striker 0:77fda962237d 28 //and here we will not need any "float pwm=0.0;" type vars
neutron_striker 0:77fda962237d 29 //another thing is that even if we don't set the pwm.period default period will be used which will be
neutron_striker 0:77fda962237d 30 //same across the whole program for any number of pwms used on that chip.
neutron_striker 0:77fda962237d 31 */
neutron_striker 0:77fda962237d 32
neutron_striker 0:77fda962237d 33 }
neutron_striker 0:77fda962237d 34 }