PES_4_Spleisser / Mbed 2 deprecated SpleisserProgramm_V11

Dependencies:   mbed mbed-rtos X_NUCLEO_IHM02A1

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PWM_alt.cpp Source File

PWM_alt.cpp

00001 #include "mbed.h"
00002 #include "PWM.h"
00003 
00004 Timeout timer;
00005 Timeout timer2;
00006 
00007 DigitalOut my_pwm(D10); // IO used by pwm_io function
00008 DigitalOut my_pwm2(D5);
00009 
00010 
00011 int on_delay = 0;
00012 int off_delay = 0;
00013 int on_delay2 = 0;
00014 int off_delay2 = 0;
00015 
00016 void toggleOff(void);
00017 
00018 void toggleOn(void) {
00019     my_pwm = 1;
00020     timer.attach_us(toggleOff, on_delay);
00021 }
00022 
00023 void toggleOff(void) {
00024     my_pwm = 0;
00025     timer.attach_us(toggleOn, off_delay);
00026 }
00027 
00028 // p_us = signal period in micro_seconds
00029 // dc   = signal duty-cycle (0.0 to 1.0)
00030 void pwm_io(int p_us, float dc) {
00031     timer.detach();
00032     if ((p_us == 0) || (dc == 0)) {
00033         my_pwm = 0;
00034         return;
00035     }
00036     if (dc >= 1) {
00037         my_pwm = 1;
00038         return;
00039     }
00040     on_delay = (int)(p_us * dc);
00041     off_delay = p_us - on_delay;
00042     toggleOn();
00043 }
00044 
00045 ////PMW2
00046 void toggleOff2(void);
00047 
00048 void toggleOn2(void) {
00049     my_pwm2 = 1;
00050     timer2.attach_us(toggleOff2, on_delay2);
00051 }
00052 
00053 void toggleOff2(void) {
00054     my_pwm2 = 0;
00055     timer2.attach_us(toggleOn2, off_delay2);
00056 }
00057 
00058 // p_us = signal period in micro_seconds
00059 // dc   = signal duty-cycle (0.0 to 1.0)
00060 void pwm_io2(int p_us, float dc) {
00061     timer2.detach();
00062     if ((p_us == 0) || (dc == 0)) {
00063         my_pwm2 = 0;
00064         return;
00065     }
00066     if (dc >= 1) {
00067         my_pwm2 = 1;
00068         return;
00069     }
00070     on_delay2 = (int)(p_us * dc);
00071     off_delay2 = p_us - on_delay2;
00072     toggleOn2();
00073 }
00074 
00075 
00076 
00077