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);
            }
        }
    }
}

SoftPWM.h

Committer:
komaida424
Date:
2013-10-23
Revision:
0:7918ce37626c

File content as of revision 0:7918ce37626c:

#ifndef SoftPWM_H
#define SoftPWM_H
#define POSITIVE true
#define NEGATIVE false

#include "mbed.h"

class SoftPWM  
{
private:
    Timeout _timeout;
    Ticker _ticker;
    void end();
    DigitalOut pulse;
    bool positive;
    void TickerInterrapt();
    float width;
    float interval;
public:
    SoftPWM(PinName,bool mode=true); 
//    void attach_us(int);
    void start();
    void write(float);
    float read();
    void pulsewidth(float);
    void pulsewidth_ms(int);
    void pulsewidth_us(int);
    void period(float);
    void period_ms(int);
    void period_us(int);
    void stop();
    operator float()  { 
        if ( width <= 0.0 ) return 0.0;
        if ( width > 1.0 )  return 1.0;
        return width / interval;
    }
    SoftPWM& operator=(float duty)  {
        width = interval * duty;
        if ( duty <= 0.0 ) width =  0.0;
        if ( duty > 1.0 )  width =  interval;
        return *this;
    }
                
};
#endif