89 lines
1.8 KiB
C++
89 lines
1.8 KiB
C++
#include "pwm.h"
|
|
|
|
//16bit
|
|
|
|
Pwm16b::Pwm16b()
|
|
{
|
|
DDRB |= (1<<PB1);
|
|
OCR1A = 0x00; //0% pwm to start A
|
|
TCNT1=0;
|
|
TCCR1A|=(1<<COM1A1); //enable OCR1A only;
|
|
ICR1=100;
|
|
on();
|
|
}
|
|
|
|
void Pwm16b::off()
|
|
{
|
|
TCCR1B = 0x00;
|
|
}
|
|
|
|
void Pwm16b::on()
|
|
{
|
|
// Phase and freq correct 16 bit PWM start pwm with /1024 prescaller
|
|
TCCR1B|=(1<<WGM13) | (1<<WGM10);
|
|
TCCR1B|= (1 << CS00) | (1 << CS01);
|
|
}
|
|
|
|
void Pwm16b::setDuty(const uint16_t duty)
|
|
{
|
|
OCR1A = 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, const uint8_t dutyA, const uint8_t dutyB)
|
|
{
|
|
_timerControlRegisterA = timerControlRegisterA;
|
|
_timerControlRegisterB = timerControlRegisterB;
|
|
_compareRegisterA = compareRegisterA;
|
|
_compareRegisterB = compareRegisterB;
|
|
_speed=speed;
|
|
|
|
*_timerControlRegisterA = 0x00;
|
|
off();
|
|
|
|
//fast 8 bit PWM pwm A
|
|
if(enableA) *_timerControlRegisterA|= (1<<COM0A1);
|
|
if(enableB) *_timerControlRegisterA|= (1<<COM0B1);
|
|
|
|
*_timerControlRegisterA|= (1<<WGM01) | (1<<WGM00);
|
|
|
|
*_compareRegisterA = 0; //0% pwm to start0
|
|
*_compareRegisterB = 0; //0% pwm to start0
|
|
|
|
on();
|
|
}
|
|
|
|
void Pwm8b::off()
|
|
{
|
|
*_timerControlRegisterB = 0x00;
|
|
}
|
|
|
|
void Pwm8b::on()
|
|
{
|
|
off();
|
|
//start with /1024 prescaller
|
|
//*_timerControlRegisterB =(1 << CS00) | (1 << CS01);
|
|
*_timerControlRegisterB |= _speed;
|
|
}
|
|
|
|
void Pwm8b::setDutyA(const uint8_t duty)
|
|
{
|
|
*_compareRegisterA = duty;
|
|
}
|
|
|
|
void Pwm8b::setDutyB(const uint8_t duty)
|
|
{
|
|
*_compareRegisterB = duty;
|
|
}
|
|
|
|
Pwm8b::~Pwm8b()
|
|
{
|
|
off();
|
|
}
|