PWM ansteuerbar und TOP wird returned

This commit is contained in:
Tristan Krause 2019-05-28 12:32:31 +02:00
parent 1f53117577
commit 2c47159df3
78 changed files with 459 additions and 148 deletions

View file

@ -8,4 +8,5 @@ volatile TLC5615 dac0(spi, SPIADR::AA0);
volatile TLC5615 dac1(spi, SPIADR::AA1);
volatile ADU adu;
volatile USART usart;
volatile PWM pwm;
volatile bool nextRequest = false;

View file

@ -5,6 +5,7 @@
#include "tlc5615.h"
#include "adu.h"
#include "usart.h"
#include "pwm.h"
#define WDT_TIMEOUT WDTO_15MS
@ -17,6 +18,7 @@ extern volatile TLC5615 dac0;
extern volatile TLC5615 dac1;
extern volatile ADU adu;
extern volatile USART usart;
extern volatile PWM pwm;
extern volatile bool nextRequest;
#endif // GLOBAL_VARS_H

View file

@ -28,6 +28,8 @@ int main()
usart.init();
usart.initRX();
pwm.init();
// Hauptschleife, Verarbeitung der Requests
while(1)

View file

@ -1,18 +1,32 @@
#include "pwm.h"
const uint16_t PWM::PRESCALERS[] = {0, 1, 8, 64, 256, 1024};
const uint8_t PWM::PRESCALER_COUNT = sizeof(PRESCALERS) / sizeof(uint16_t);
void pwmSetFrequency(uint32_t freq)
void PWM::init() const volatile
{
// fast pwm mode, top = OCR0A, non inverting mode
TCCR0A = _BV(COM0B1) | _BV(WGM00) | _BV(WGM01);
DDRB |= _BV(PB4);
TCCR0B = _BV(WGM02);
uint16_t p_ideal = ceil(float(F_CPU) / (freq * 0x100));
for(int8_t i = PWM_PRESCALER_COUNT - 1; i >= 0 && PWM_PRESCALERS[i] >= p_ideal; i--)
TCCR0B = _BV(WGM02) | i; // set prescaler
if(p_ideal)
OCR0A = (uint8_t) (float(F_CPU) / (freq * PWM_PRESCALERS[TCCR0B & 0x07]));
// output signal on PB4
DDRB |= _BV(PB4);
}
void pwmSetValue(uint8_t value)
void PWM::setFrequency(uint32_t freq) const volatile
{
uint16_t p_ideal = ceil(float(F_CPU) / (freq * 0x100));
for(int8_t i = PRESCALER_COUNT - 1; i >= 0 && PRESCALERS[i] >= p_ideal; i--)
TCCR0B = (TCCR0B & 0xF8) | i; // set prescaler
if(p_ideal)
OCR0A = (uint8_t) (float(F_CPU) / (freq * PRESCALERS[TCCR0B & 0x07]));
}
void PWM::setValue(uint8_t value) const volatile
{
OCR0B = value;
}
uint8_t PWM::getTop() const volatile
{
return OCR0A;
}

View file

@ -4,10 +4,17 @@
#include <avr/io.h>
#include <math.h>
const uint16_t PWM_PRESCALERS[] = {0, 1, 8, 64, 256, 1024};
const uint8_t PWM_PRESCALER_COUNT = sizeof(PWM_PRESCALERS) / sizeof(uint16_t);
void pwmSetFrequency(uint32_t);
void pwmSetValue(uint8_t);
class PWM
{
public:
void init(void) const volatile;
void setFrequency(uint32_t) const volatile;
void setValue(uint8_t) const volatile;
uint8_t getTop(void) const volatile;
private:
const static uint16_t PRESCALERS[];
const static uint8_t PRESCALER_COUNT;
};
#endif // PWM_H

View file

@ -62,6 +62,14 @@ void handleRequest()
case RQ_ADC_DAC_STROKE:
rqAdcDacStroke();
break;
case RQ_PWM_SET_FREQ:
rqPwmSetFreq();
break;
case RQ_PWM_SET_VALUE:
rqPwmSetValue();
break;
default:
break;
@ -217,11 +225,21 @@ void rqAdcDacStroke()
usart.flush();
}
void rqSetPwm()
void rqPwmSetFreq()
{
usart.initTX();
uint32_t freq = usart.readU32();
pwm.setFrequency(freq);
usart.writeByte(pwm.getTop());
usart.flush();
}
void rqPwmSetValue()
{
usart.initTX();
uint16_t value = usart.readByte();
OCR0A = value;
pwm.setValue(value);
usart.writeByte(USART::MSG_OK);
usart.flush();

View file

@ -21,7 +21,8 @@ constexpr static uint8_t RQ_AA0 = 10;
constexpr static uint8_t RQ_AA1 = 11;
constexpr static uint8_t RQ_ADC = 12;
constexpr static uint8_t RQ_ADC_DAC_STROKE = 13;
constexpr static uint8_t RQ_SET_PWM = 14;
constexpr static uint8_t RQ_PWM_SET_FREQ = 14;
constexpr static uint8_t RQ_PWM_SET_VALUE = 15;
uint8_t const rq_len[] = {
/* RQ_DISC */ 1,
@ -38,7 +39,8 @@ uint8_t const rq_len[] = {
/* RQ_AA1 */ 3,
/* RQ_ADC */ 2,
/* RQ_ADC_DAC_STROKE */ 9,
/* RQ_SET_PWM */ 2
/* RQ_PWM_SET_FREQ */ 5,
/* RQ_PWM_SET_VALUE */ 2
};
/**
@ -61,6 +63,7 @@ void rqAnalogWrite0(void);
void rqAnalogWrite1(void);
void rqAnalogRead(void);
void rqAdcDacStroke(void);
void rqSetPwm(void);
void rqPwmSetFreq(void);
void rqPwmSetValue(void);
#endif // REQUESTS_H

View file

@ -130,3 +130,10 @@ uint16_t USART::readInt() volatile
v |= readByte() << 8;
return v;
}
uint32_t USART::readU32() volatile
{
uint32_t v = readInt();
v |= ((uint32_t) readInt()) << 16;
return v;
}

View file

@ -102,6 +102,12 @@ public:
* \return gelesenes Integer
*/
uint16_t readInt(void) volatile;
/**
* Liest ein uint32_t aus dem Eingangspuffer
* \return gelesenes uint32_t
*/
uint32_t readU32(void) volatile;
/**********************/