working pwm

This commit is contained in:
IMback
2017-05-16 10:37:01 +02:00
parent e2e53267c6
commit cb779f21d4
4 changed files with 115 additions and 147 deletions

58
pwm.cpp
View File

@ -1,10 +1,11 @@
#include "pwm.h"
//16bit
Pwm16b::Pwm16b()
{
DDRB |= (1<<PB1);
OCR1A = 0x0; //0% pwm to start0
OCR1A = 0x00; //0% pwm to start A
TCNT1=0;
TCCR1A|=(1<<COM1A1); //enable OCR1A only;
ICR1=100;
@ -20,7 +21,7 @@ void Pwm16b::on()
{
// Phase and freq correct 16 bit PWM start pwm with /1024 prescaller
TCCR1B|=(1<<WGM13) | (1<<WGM10);
TCCR1B|= (1 << CS12);
TCCR1B|= (1 << CS00) | (1 << CS01);
}
void Pwm16b::setDuty(const uint16_t duty)
@ -32,3 +33,56 @@ 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();
}