PWM with regular digital IO and with digital pin that supports PWM generation
Dependencies: mbed
Revision 1:56cc90ae5080, committed 2015-05-12
- Comitter:
- Foxnec
- Date:
- Tue May 12 09:46:06 2015 +0000
- Parent:
- 0:ee9aab897b59
- Commit message:
- Changes to comments
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Mon Apr 20 15:07:18 2015 +0000 +++ b/main.cpp Tue May 12 09:46:06 2015 +0000 @@ -3,7 +3,8 @@ * @author Petr Douša * @version V1.0 * @date 20-April-2015 -* @brief This program use digital output and regular PWM output to modulate PWM +* @brief This program uses digital output (software) and hardware PWM output +* to generate two separate PWMs ***********************************************************************************/ /**********************************************************************************/ @@ -57,7 +58,7 @@ int off_delay = 0; //mbed - initialization of peripherals -Timeout timer; // timer use with digital IO +Timeout timer; // timer used with digital IO DigitalOut my_pwm(LED1); // digital IO used by pwm_io function PwmOut real_pwm(PA_8); // regular PWM output @@ -65,7 +66,7 @@ /******************************************************************************* * Function Name : toggleOn. -* Description : Set high on digital pin and set and start timer. +* Description : Set high level on digital pin and timer startup. * Input : None. * Output : None. * Return : None. @@ -78,7 +79,7 @@ /******************************************************************************* * Function Name : toggleOff. -* Description : Set low on digital pin and set and start timer. +* Description : Set low level on digital pin and set and start timer. * Input : None. * Output : None. * Return : None. @@ -91,23 +92,23 @@ /******************************************************************************* * Function Name : pwm_io. -* Description : Set variables and start PWM on digital output. -* Input : p_us: signal period in micro_seconds ,dc: signal duty-cycle (0.0 to 1.0) . -* Output : on_delay: time to be digital output on high ,off_delay: time to be digital output on low: . +* Description : Set variables and start software PWM on digital output. +* Input : p_us: signal period in micro_seconds, dc: signal duty-cycle (0.0 to 1.0) . +* Output : on_delay: output ON time, off_delay: output OFF time * Return : None. *******************************************************************************/ void pwm_io(int p_us, float dc) { timer.detach(); // shutdown timer if ((p_us == 0) || (dc == 0)) { // if duty-cycle is 0 - my_pwm = 0; // set low regular IO to low + my_pwm = 0; // set high IO to low return; } if (dc >= 1) { // if duty-cycle is 1 and more - my_pwm = 1; // set high regular IO to low + my_pwm = 1; // set high IO to low return; } - on_delay = (int)(p_us * dc); // calculate time to be digital output on high and low + on_delay = (int)(p_us * dc); // calculate the time off_delay = p_us - on_delay; toggleOn(); // call function toggleOn to start PWM } @@ -123,24 +124,24 @@ { double a=0; // variable to set duty-cycle - real_pwm.period_us(2000); // set period of real PWM to 2 milliseconds - real_pwm.write(a); // set duty-cycle as it is in variable a (0 at this point) + real_pwm.period_us(2000); // set period of hardware PWM to 2 milliseconds + real_pwm.write(a); // set duty-cycle from a (0 at this point) - pwm_io(2000, a); // set period of digital IO PWM to 2 ms and duty-cycle as in variable a (0 at this point) + pwm_io(2000, a); // set period of software PWM to 2 ms and duty-cycle as in variable a (0 at this point) - while(1) { // main cykl of program + while(1) { // main cycle of the program - if(a>=1) { // if variable is grater then 1 set it 0 else attributable 0.01 + if(a>=1) { // if variable is grater then 1 set it 0 else increment 0.01 a=0; } else { a=a+0.01; } - real_pwm.write(a); // set duty-cycle of real PWM as as it is in variable a + real_pwm.write(a); // set duty-cycle of hardware PWM as as it is in variable a - pwm_io(2000, a); // set duty-cycle of digital IO PWM as as it is in variable a and period to 2 ms + pwm_io(2000, a); // set duty-cycle of software PWM as as it is in variable a and period to 2 ms wait(0.05); // wait some time in milliseconds }