199 lines
3.9 KiB
C++
199 lines
3.9 KiB
C++
/*
|
|
* SSR.hpp
|
|
*
|
|
* Created: 16.07.2016 00:56:08
|
|
* Author: netz
|
|
*/
|
|
|
|
|
|
#ifndef SSR_H_
|
|
#define SSR_H_
|
|
|
|
#include <util/delay.h>
|
|
#include <avr/interrupt.h>
|
|
#include "hardware/pin.hpp"
|
|
|
|
template <typename Port, int pin_out1, int pin_out2, int pin_out3, int pin_out4, int pin_out5, int pin_out6>
|
|
class SSR {
|
|
public:
|
|
SSR() {
|
|
init();
|
|
}
|
|
void off(uint8_t ausg = 255) {
|
|
if(ausg & OUT1) {
|
|
out1::make_low();
|
|
}
|
|
if(ausg & OUT2) {
|
|
out2::make_low();
|
|
}
|
|
if(ausg & OUT3) {
|
|
out3::make_low();
|
|
}
|
|
if(ausg & OUT4) {
|
|
out4::make_low();
|
|
}
|
|
if(ausg & OUT5) {
|
|
out5::make_low();
|
|
}
|
|
if(ausg & OUT6) {
|
|
out6::make_low();
|
|
}
|
|
}
|
|
void on(uint8_t ausg = 255) {
|
|
if(ausg & OUT1) {
|
|
out1::make_high();
|
|
}
|
|
if(ausg & OUT2) {
|
|
out2::make_high();
|
|
}
|
|
if(ausg & OUT3) {
|
|
out3::make_high();
|
|
}
|
|
if(ausg & OUT4) {
|
|
out4::make_high();
|
|
}
|
|
if(ausg & OUT5) {
|
|
out5::make_high();
|
|
}
|
|
if(ausg & OUT6) {
|
|
out6::make_high();
|
|
}
|
|
}
|
|
void toggle(uint8_t ausg = 255) {
|
|
if(ausg & OUT1) {
|
|
out1::toggle();
|
|
}
|
|
if(ausg & OUT2) {
|
|
out2::toggle();
|
|
}
|
|
if(ausg & OUT3) {
|
|
out3::toggle();
|
|
}
|
|
if(ausg & OUT4) {
|
|
out4::toggle();
|
|
}
|
|
if(ausg & OUT5) {
|
|
out5::toggle();
|
|
}
|
|
if(ausg & OUT6) {
|
|
out6::toggle();
|
|
}
|
|
}
|
|
uint8_t isOn(uint8_t ausg) {
|
|
if(ausg & OUT1) {
|
|
return out1::read();
|
|
}
|
|
if(ausg & OUT2) {
|
|
return out2::read();
|
|
}
|
|
if(ausg & OUT3) {
|
|
return out3::read();
|
|
}
|
|
if(ausg & OUT4) {
|
|
return out4::read();
|
|
}
|
|
if(ausg & OUT5) {
|
|
return out5::read();
|
|
}
|
|
if(ausg & OUT6) {
|
|
return out6::read();
|
|
}
|
|
return 0;
|
|
}
|
|
void test() {
|
|
off();
|
|
for (uint8_t i=0;i<6;i++) {
|
|
on((1<<i));
|
|
_delay_ms(100);
|
|
off((1<<i));
|
|
}
|
|
off();
|
|
}
|
|
void setup_timer() {
|
|
// Timer/Counter 1 initialization
|
|
// Clock source: System Clock
|
|
// Clock value: 250,000 kHz
|
|
// Mode: CTC top=OCR1A
|
|
// OC1A output: Disconnected
|
|
// OC1B output: Disconnected
|
|
// Noise Canceler: Off
|
|
// Input Capture on Falling Edge
|
|
// Timer Period: 0,1 s
|
|
// Timer1 Overflow Interrupt: Off
|
|
// Input Capture Interrupt: Off
|
|
// Compare A Match Interrupt: On
|
|
// Compare B Match Interrupt: Off
|
|
TCCR1A = 0;
|
|
TCCR1B |= (1<<WGM12) | (1<<CS11) | (1<<CS10);
|
|
TCNT1H = 0;
|
|
TCNT1L = 0;
|
|
ICR1H = 0;
|
|
ICR1L = 0;
|
|
OCR1AH = 0x61;
|
|
OCR1AL = 0xA8;
|
|
OCR1BH = 0;
|
|
OCR1BL = 0;
|
|
|
|
// Timer(s)/Counter(s) Interrupt(s) initialization
|
|
TIMSK |= (1<<OCIE1A);
|
|
sei();
|
|
}
|
|
void setPwm(uint8_t ausg, float val) {
|
|
if(ausg < 0)
|
|
ausg = 0;
|
|
if(ausg > 5)
|
|
ausg = 5;
|
|
if(val < 0)
|
|
val = 0;
|
|
if(val > 20)
|
|
val = 20;
|
|
this->pwm[ausg] = (uint8_t)val;
|
|
}
|
|
uint8_t getPwm(uint8_t ausg) {
|
|
if(ausg < 0)
|
|
ausg = 0;
|
|
if(ausg > 5)
|
|
ausg = 5;
|
|
return this->pwm[ausg];
|
|
}
|
|
void timer() {
|
|
if (this->pct++ >= 20) {
|
|
this->pct=0;
|
|
}
|
|
for(uint8_t i = 0; i < 6; i++) {
|
|
if(this->pwm[i] >= this->pct && this->pwm[i] != 0) {
|
|
this->on((1<<i));
|
|
} else {
|
|
this->off((1<<i));
|
|
}
|
|
}
|
|
//out1::toggle();
|
|
}
|
|
static const uint8_t OUT1 = (1<<0);
|
|
static const uint8_t OUT2 = (1<<1);
|
|
static const uint8_t OUT3 = (1<<2);
|
|
static const uint8_t OUT4 = (1<<3);
|
|
static const uint8_t OUT5 = (1<<4);
|
|
static const uint8_t OUT6 = (1<<5);
|
|
private:
|
|
void init() {
|
|
out1::make_output();
|
|
out2::make_output();
|
|
out3::make_output();
|
|
out4::make_output();
|
|
out5::make_output();
|
|
out6::make_output();
|
|
off();
|
|
}
|
|
uint8_t pct = 0;
|
|
uint8_t pwm[6] = {0, 0, 0, 0, 0, 0};
|
|
void uninit();
|
|
const typedef avrlib::pin<Port, pin_out1> out1;
|
|
const typedef avrlib::pin<Port, pin_out2> out2;
|
|
const typedef avrlib::pin<Port, pin_out3> out3;
|
|
const typedef avrlib::pin<Port, pin_out4> out4;
|
|
const typedef avrlib::pin<Port, pin_out5> out5;
|
|
const typedef avrlib::pin<Port, pin_out6> out6;
|
|
};
|
|
|
|
#endif /* SSR_H_ */ |