142 lines
2.8 KiB
C++
142 lines
2.8 KiB
C++
#include "pwm.h"
|
|
|
|
//16bit
|
|
|
|
Pwm16b::Pwm16b( volatile unsigned char *timerControlRegisterA, volatile unsigned char *timerControlRegisterB,
|
|
volatile uint16_t *compareRegisterA, volatile uint16_t *compareRegisterB, volatile uint16_t *inputCaptureRegister,
|
|
const uint8_t speed, const bool enableA, const bool enableB)
|
|
{
|
|
|
|
_timerControlRegisterA = timerControlRegisterA;
|
|
_compareRegisterA = compareRegisterA;
|
|
_compareRegisterB = compareRegisterB;
|
|
|
|
_enableA =enableA;
|
|
_enableB =enableB;
|
|
|
|
*_timerControlRegisterA = 0x00;
|
|
*timerControlRegisterB = 0x00;
|
|
*inputCaptureRegister = 0xFFFF;
|
|
*timerControlRegisterB |= (1<<WGM13) | (1<<WGM12);
|
|
*timerControlRegisterB |= 0b00000111 & speed;
|
|
*_timerControlRegisterA|= (1<<WGM11);
|
|
|
|
*_compareRegisterA = 0;
|
|
*_compareRegisterB = 0;
|
|
}
|
|
|
|
bool Pwm16b::isOn()
|
|
{
|
|
return *_timerControlRegisterA != (1<<WGM11);
|
|
}
|
|
|
|
void Pwm16b::off()
|
|
{
|
|
*_timerControlRegisterA = 0x00;
|
|
*_timerControlRegisterA |= (1<<WGM11);
|
|
}
|
|
|
|
void Pwm16b::on()
|
|
{
|
|
off();
|
|
if(_enableA) *_timerControlRegisterA|= (1<<COM1A1);
|
|
if(_enableB) *_timerControlRegisterA|= (1<<COM1B1);
|
|
}
|
|
|
|
uint16_t Pwm16b::getValueA()
|
|
{
|
|
return *_compareRegisterA;
|
|
}
|
|
|
|
uint16_t Pwm16b::getValueB()
|
|
{
|
|
return *_compareRegisterB;
|
|
}
|
|
|
|
|
|
void Pwm16b::setDutyA(const uint16_t duty)
|
|
{
|
|
*_compareRegisterA = duty;
|
|
}
|
|
|
|
void Pwm16b::setDutyB(const uint16_t duty)
|
|
{
|
|
*_compareRegisterB = duty;
|
|
}
|
|
|
|
Pwm16b::~Pwm16b()
|
|
{
|
|
off();
|
|
}
|
|
|
|
//8bit
|
|
|
|
Pwm8b::Pwm8b( volatile unsigned char *timerControlRegisterA, volatile unsigned char *timerControlRegisterB,
|
|
volatile unsigned char *compareRegisterA, volatile unsigned char *compareRegisterB, const uint8_t speed,
|
|
const bool enableA, const bool enableB)
|
|
{
|
|
_timerControlRegisterA = timerControlRegisterA;
|
|
_compareRegisterA = compareRegisterA;
|
|
_compareRegisterB = compareRegisterB;
|
|
|
|
_enableA =enableA;
|
|
_enableB =enableB;
|
|
|
|
*_timerControlRegisterA = 0x00;
|
|
*timerControlRegisterB = 0x00;
|
|
|
|
//fast 8 bit PWM pwm A
|
|
if(_enableA) *_timerControlRegisterA|= (1<<COM0A1);
|
|
if(_enableB) *_timerControlRegisterA|= (1<<COM0B1);
|
|
|
|
*_timerControlRegisterA|= (1<<WGM01) | (1<<WGM00);
|
|
*timerControlRegisterB |= 0b00000111 & speed;
|
|
|
|
*_compareRegisterA = 0; //0% pwm to start0
|
|
*_compareRegisterB = 0; //0% pwm to start0
|
|
}
|
|
|
|
bool Pwm8b::isOn()
|
|
{
|
|
return (*_timerControlRegisterA & 0x11111100) != 0;
|
|
}
|
|
|
|
void Pwm8b::off()
|
|
{
|
|
*_timerControlRegisterA &= 0b00000011;
|
|
}
|
|
|
|
void Pwm8b::on()
|
|
{
|
|
off();
|
|
if(_enableA) *_timerControlRegisterA|= (1<<COM0A1);
|
|
if(_enableB) *_timerControlRegisterA|= (1<<COM0B1);
|
|
|
|
}
|
|
|
|
uint8_t Pwm8b::getValueA()
|
|
{
|
|
return *_compareRegisterA;
|
|
}
|
|
|
|
uint8_t Pwm8b::getValueB()
|
|
{
|
|
return *_compareRegisterB;
|
|
}
|
|
|
|
|
|
void Pwm8b::setDutyA(const uint8_t duty)
|
|
{
|
|
*_compareRegisterA = duty;
|
|
}
|
|
|
|
void Pwm8b::setDutyB(const uint8_t duty)
|
|
{
|
|
*_compareRegisterB = duty;
|
|
}
|
|
|
|
Pwm8b::~Pwm8b()
|
|
{
|
|
off();
|
|
}
|