The PWM output by software. Can be output to any pin. It can be used to replace the Pwmout.

Dependents:   SoftPWM_Example XYZ_Joystick_PWM mbed_pwm mbed_Ahan_robocon ... more

#include "mbed.h"
#include "math.h"
#include "SoftPWM.h"

SoftPWM led[] = { LED1,LED2,LED3,LED4 };

int main()
{
    for ( int i=0; i<4; i++ ) led[i].period_ms( 1 );
    while (1)   {
        for ( int j=0; j<360; j+=10 ) {
            for ( int k=0; k<4; k++ ) {
                led[k] = cos( (j+k*90)*2.0*3.14/360 ) * 0.5 + 0.5;
                wait(0.01);
            }
        }
    }
}
Committer:
komaida424
Date:
Wed Oct 23 19:31:14 2013 +0000
Revision:
0:7918ce37626c
The PWM output by software. Can be output to any pin. It can be used to replace the Pwmout.
;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
komaida424 0:7918ce37626c 1 #ifndef SoftPWM_H
komaida424 0:7918ce37626c 2 #define SoftPWM_H
komaida424 0:7918ce37626c 3 #define POSITIVE true
komaida424 0:7918ce37626c 4 #define NEGATIVE false
komaida424 0:7918ce37626c 5
komaida424 0:7918ce37626c 6 #include "mbed.h"
komaida424 0:7918ce37626c 7
komaida424 0:7918ce37626c 8 class SoftPWM
komaida424 0:7918ce37626c 9 {
komaida424 0:7918ce37626c 10 private:
komaida424 0:7918ce37626c 11 Timeout _timeout;
komaida424 0:7918ce37626c 12 Ticker _ticker;
komaida424 0:7918ce37626c 13 void end();
komaida424 0:7918ce37626c 14 DigitalOut pulse;
komaida424 0:7918ce37626c 15 bool positive;
komaida424 0:7918ce37626c 16 void TickerInterrapt();
komaida424 0:7918ce37626c 17 float width;
komaida424 0:7918ce37626c 18 float interval;
komaida424 0:7918ce37626c 19 public:
komaida424 0:7918ce37626c 20 SoftPWM(PinName,bool mode=true);
komaida424 0:7918ce37626c 21 // void attach_us(int);
komaida424 0:7918ce37626c 22 void start();
komaida424 0:7918ce37626c 23 void write(float);
komaida424 0:7918ce37626c 24 float read();
komaida424 0:7918ce37626c 25 void pulsewidth(float);
komaida424 0:7918ce37626c 26 void pulsewidth_ms(int);
komaida424 0:7918ce37626c 27 void pulsewidth_us(int);
komaida424 0:7918ce37626c 28 void period(float);
komaida424 0:7918ce37626c 29 void period_ms(int);
komaida424 0:7918ce37626c 30 void period_us(int);
komaida424 0:7918ce37626c 31 void stop();
komaida424 0:7918ce37626c 32 operator float() {
komaida424 0:7918ce37626c 33 if ( width <= 0.0 ) return 0.0;
komaida424 0:7918ce37626c 34 if ( width > 1.0 ) return 1.0;
komaida424 0:7918ce37626c 35 return width / interval;
komaida424 0:7918ce37626c 36 }
komaida424 0:7918ce37626c 37 SoftPWM& operator=(float duty) {
komaida424 0:7918ce37626c 38 width = interval * duty;
komaida424 0:7918ce37626c 39 if ( duty <= 0.0 ) width = 0.0;
komaida424 0:7918ce37626c 40 if ( duty > 1.0 ) width = interval;
komaida424 0:7918ce37626c 41 return *this;
komaida424 0:7918ce37626c 42 }
komaida424 0:7918ce37626c 43
komaida424 0:7918ce37626c 44 };
komaida424 0:7918ce37626c 45 #endif