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

Files at this revision

API Documentation at this revision

Comitter:
komaida424
Date:
Wed Oct 23 19:31:14 2013 +0000
Commit message:
The PWM output by software. Can be output to any pin. It can be used to replace the Pwmout.
;

Changed in this revision

SoftPWM.cpp Show annotated file Show diff for this revision Revisions of this file
SoftPWM.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 7918ce37626c SoftPWM.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SoftPWM.cpp	Wed Oct 23 19:31:14 2013 +0000
@@ -0,0 +1,99 @@
+#include "mbed.h"
+#include "InterruptIn.h"
+#include "SoftPWM.h"
+
+SoftPWM::SoftPWM(PinName _outpin,bool _positive) : pulse(_outpin)     //constructa  
+{
+    if ( _positive )
+        pulse = 0;
+    else
+        pulse = 1;
+    positive = _positive;
+    interval = 0.02;
+    width = 0;
+    start(); 
+}
+
+float SoftPWM::read()
+{
+    if ( width <= 0.0 ) return 0.0;
+    if ( width > 1.0 )  return 1.0;
+    return width / interval;    
+}
+
+void SoftPWM::write(float duty)
+{
+    width = interval * duty;
+    if ( duty <= 0.0 ) width =  0.0;
+    if ( duty > 1.0 )  width =  interval;
+}
+
+void SoftPWM::start()
+{
+    _ticker.attach(this,&SoftPWM::TickerInterrapt,interval);
+}
+
+void SoftPWM::stop()
+{
+    _ticker.detach();
+    if ( positive )
+        pulse = 0;
+    else
+        pulse = 1;
+    wait(width);
+}
+
+void SoftPWM::period(float _period)
+{
+    interval = _period;
+    start();
+}
+
+void SoftPWM::period_ms(int _period)
+{
+    period((float)_period / 1000);
+    start();
+}
+
+void SoftPWM::period_us(int _period)
+{
+    period((float)_period / 1000000);
+    start();
+}
+
+void SoftPWM::pulsewidth(float _width)
+{
+    width = _width;
+   if ( width < 0.0 ) width = 0.0;
+}
+
+void SoftPWM::pulsewidth_ms(int _width)
+{
+     pulsewidth((float)_width / 1000);
+}
+
+void SoftPWM::pulsewidth_us(int _width)
+{
+    pulsewidth((float)_width / 1000000);
+}
+
+void SoftPWM::TickerInterrapt()
+{ 
+    if ( width <= 0 ) return;
+    _timeout.attach(this,&SoftPWM::end,width);
+    if ( positive )
+        pulse = 1;
+    else
+        pulse = 0;    
+}
+
+void SoftPWM::end()
+{
+    if ( positive )
+        pulse = 0;
+    else
+        pulse = 1;    
+//    _timeout.detach();
+}
+;
+
diff -r 000000000000 -r 7918ce37626c SoftPWM.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SoftPWM.h	Wed Oct 23 19:31:14 2013 +0000
@@ -0,0 +1,45 @@
+#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
\ No newline at end of file