Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed mbed-rtos X_NUCLEO_IHM02A1
Diff: Papierkorb/PWM.cpp
- Revision:
- 33:de144094bdd1
- Parent:
- 27:23bd03a6a6f6
diff -r e464b2bb2376 -r de144094bdd1 Papierkorb/PWM.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Papierkorb/PWM.cpp Mon Apr 22 11:56:38 2019 +0000 @@ -0,0 +1,77 @@ +#include "mbed.h" +#include "PWM.h" + +Timeout timer; +Timeout timer2; + +DigitalOut my_pwm(D10); // IO used by pwm_io function +DigitalOut my_pwm2(D5); + + +int on_delay = 0; +int off_delay = 0; +int on_delay2 = 0; +int off_delay2 = 0; + +void toggleOff(void); + +void toggleOn(void) { + my_pwm = 1; + timer.attach_us(toggleOff, on_delay); +} + +void toggleOff(void) { + my_pwm = 0; + timer.attach_us(toggleOn, off_delay); +} + +// p_us = signal period in micro_seconds +// dc = signal duty-cycle (0.0 to 1.0) +void pwm_io(int p_us, float dc) { + timer.detach(); + if ((p_us == 0) || (dc == 0)) { + my_pwm = 0; + return; + } + if (dc >= 1) { + my_pwm = 1; + return; + } + on_delay = (int)(p_us * dc); + off_delay = p_us - on_delay; + toggleOn(); +} + +////PMW2 +void toggleOff2(void); + +void toggleOn2(void) { + my_pwm2 = 1; + timer2.attach_us(toggleOff2, on_delay2); +} + +void toggleOff2(void) { + my_pwm2 = 0; + timer2.attach_us(toggleOn2, off_delay2); +} + +// p_us = signal period in micro_seconds +// dc = signal duty-cycle (0.0 to 1.0) +void pwm_io2(int p_us, float dc) { + timer2.detach(); + if ((p_us == 0) || (dc == 0)) { + my_pwm2 = 0; + return; + } + if (dc >= 1) { + my_pwm2 = 1; + return; + } + on_delay2 = (int)(p_us * dc); + off_delay2 = p_us - on_delay2; + toggleOn2(); +} + + + +