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.

PwmOutAny.cpp

Committer:
mr63
Date:
2013-12-08
Revision:
0:9eb94736e774

File content as of revision 0:9eb94736e774:

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