This Library allows you to use any pin on the mbed micro as PWM out. It also allows the user to update the Period and Pulse width in realtime.

Files at this revision

API Documentation at this revision

Comitter:
mr63
Date:
Sun Dec 08 19:04:44 2013 +0000
Commit message:
This Library allows you to use any pin on the mbed micro as PWM out. It also allows the user to update the Period and Pulse width in realtime.

Changed in this revision

PwmOutAny.cpp Show annotated file Show diff for this revision Revisions of this file
PwmOutAny.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 9eb94736e774 PwmOutAny.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PwmOutAny.cpp	Sun Dec 08 19:04:44 2013 +0000
@@ -0,0 +1,49 @@
+#include "PwmOutAny.h"
+
+float _period;
+float _PercentHigh;
+
+PwmOutAny::PwmOutAny(PinName pin1, float Period, float PercentHigh):_PwmOut(pin1) 
+{
+	_period=Period;
+	_PercentHigh=PercentHigh ;
+	_PwmTicker.attach(this,&PwmOutAny::TickerFlipper,_period/2);
+}
+
+void PwmOutAny::TickerFlipper()
+{
+	static float CurrentPeriod;
+	if(!_PwmOut)
+	{
+			_PwmTicker.detach();
+			_PwmTicker.attach(this,&PwmOutAny::TickerFlipper,CurrentPeriod*(_PercentHigh));
+			_PwmOut = true;
+	}
+	else
+	{
+		   CurrentPeriod = _period;
+			_PwmTicker.detach();
+			_PwmTicker.attach(this,&PwmOutAny::TickerFlipper,CurrentPeriod*(1.0-_PercentHigh));
+			_PwmOut = false;
+	}
+}
+
+
+ float PwmOutAny::Get_PulseWidth()
+{
+	return (_PercentHigh);
+	
+}
+ void  PwmOutAny::Set_PulseWidth(float PW)
+{
+		_PercentHigh = PW;
+}
+ float PwmOutAny::Get_Period()
+{
+	return _period;
+}
+ void  PwmOutAny::Set_Period(float ChangePeriod)
+{
+	_period = ChangePeriod;
+}
+
diff -r 000000000000 -r 9eb94736e774 PwmOutAny.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PwmOutAny.h	Sun Dec 08 19:04:44 2013 +0000
@@ -0,0 +1,20 @@
+#include "mbed.h"
+
+
+class PwmOutAny {
+public:													
+ 
+ PwmOutAny(PinName pin1, float Period, float PercentHigh);
+
+ float Get_PulseWidth();
+ void  Set_PulseWidth(float PW);
+ float Get_Period();
+ void  Set_Period(float ChangePeriod);
+
+
+private:
+	void TickerFlipper();
+	Ticker _PwmTicker;
+	DigitalOut _PwmOut;
+
+};
diff -r 000000000000 -r 9eb94736e774 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sun Dec 08 19:04:44 2013 +0000
@@ -0,0 +1,60 @@
+#include "mbed.h"
+#include "PwmOutAny.h"
+
+ Timer t;
+
+ InterruptIn SquareIn (p22);
+ DigitalOut status (LED1);
+
+
+
+PwmOutAny newPwm (p25,.0001,.50);
+
+void measure_Period()    // this is a test function it looks at a square wave on pin p22 and uses its freq. as the base freq. for the PWM.
+{
+	static bool first = true;
+	static float InputPeriod = .0001;
+  static unsigned int count = 0;
+	if(first)
+	{
+		t.reset();
+		t.start();
+		first = false;
+	}
+	else
+	{
+		
+		InputPeriod = (InputPeriod + t.read())/2;
+		newPwm.Set_Period(InputPeriod);
+		status = ! status;
+		t.reset();
+		t.start();
+		count++;
+	}
+	
+}
+
+
+int main()
+	{
+	
+    SquareIn.rise(&measure_Period);
+		
+		while(1)
+		{
+    for(float t=0;t<=1;t+=.01)
+			{
+				newPwm.Set_PulseWidth(t);
+				wait(.1);
+			}
+		for(float t=1;t>=0;t-=.01)
+			{
+				newPwm.Set_PulseWidth(t);
+				wait(.1);
+			}
+
+			
+		}
+		
+	}	
+