You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
175 lines
3.8 KiB
C
175 lines
3.8 KiB
C
#include <avr/io.h>
|
|
#include <avr/interrupt.h>
|
|
#include <avr/eeprom.h>
|
|
#include "relaishw.h"
|
|
#include "rs485.h"
|
|
#include "eb.h"
|
|
|
|
uint8_t testled;
|
|
uint8_t testbutton;
|
|
uint8_t relais1state;
|
|
uint8_t relais2state;
|
|
uint8_t pc0state;
|
|
uint8_t pc1state;
|
|
|
|
uint8_t waitforack;
|
|
uint8_t waitforsend;
|
|
uint8_t acktimer;
|
|
|
|
int main(void)
|
|
{
|
|
uint8_t i;
|
|
|
|
init_rs485();
|
|
DDRD |= (1<<TestLED);
|
|
DDRD &= ~(1<<TestButton);
|
|
|
|
DDRB |= (1<<Relais1);
|
|
DDRB |= (1<<Relais2);
|
|
|
|
DDRC &= ~(1<<PC0);
|
|
DDRC &= ~(1<<PC1);
|
|
|
|
PORTD &= ~(1<<TestLED);
|
|
PORTD |= (1<<TestButton);
|
|
|
|
PORTB &= ~(1<<Relais1);
|
|
PORTB &= ~(1<<Relais2);
|
|
|
|
PORTC |= (1<<PC0);
|
|
PORTC |= (1<<PC1);
|
|
|
|
ADMUX = (0<<REFS1) | (1<<REFS0);
|
|
ADCSRA = (1<<ADPS2) | (1<<ADPS1);
|
|
ADCSRA |= (1<<ADEN);
|
|
ADCSRA |= (1<<ADSC);
|
|
loop_until_bit_is_clear(ADCSRA, ADSC);
|
|
svalue = ADCW;
|
|
|
|
my_address = eeprom_read_byte((const uint8_t *)0);
|
|
my_prio = eeprom_read_byte((const uint8_t *)1);
|
|
master = eeprom_read_byte((const uint8_t *)2);
|
|
|
|
testled = 0;
|
|
relais1state = 0;
|
|
relais2state = 0;
|
|
pc0state = 0;
|
|
pc1state = 0;
|
|
|
|
waitforsend = 0;
|
|
acktimer = 0;
|
|
|
|
sei();
|
|
while(1) {
|
|
rs485_loop();
|
|
if (rflag) {
|
|
if (rsender)
|
|
srecv = rsender;
|
|
else
|
|
srecv = master;
|
|
smode = 0;
|
|
if ((rrecv == my_address) && (rmode == 2)) {
|
|
acktimer = 0;
|
|
waitforsend = 0;
|
|
}
|
|
|
|
if ((rrecv == my_address) && (rmode == 0)) {
|
|
for (i=0; i<4; i++) {
|
|
decodepart(i);
|
|
scommand = 0;
|
|
schannel = rchannel;
|
|
if ((rcommand == 0x40) && (rchannel == 0)) {
|
|
scommand = 0x40;
|
|
svalue = 4; /* Relais mit 2 Schalt-IO und 2 ADC*/
|
|
}
|
|
if ((rcommand == 0x60) && (rchannel == 1)) {
|
|
if (rvalue & 0x01) {
|
|
testled = ~0;
|
|
PORTD |= (1<<TestLED);
|
|
} else {
|
|
testled = 0;
|
|
PORTD &= ~(1<<TestLED);
|
|
}
|
|
if (rvalue & 0x02) {
|
|
relais1state = ~0;
|
|
PORTB |= (1<<Relais1);
|
|
} else {
|
|
relais1state = 0;
|
|
PORTB &= ~(1<<Relais1);
|
|
}
|
|
if (rvalue & 0x04) {
|
|
relais2state = ~0;
|
|
PORTB |= (1<<Relais2);
|
|
} else {
|
|
relais2state = 0;
|
|
PORTB &= ~(1<<Relais2);
|
|
}
|
|
scommand = 0x40;
|
|
svalue = (testled & 0x01) | (relais1state & 0x02) | (relais2state & 0x04) | (pc0state & 0x08) | (pc1state & 0x10);
|
|
}
|
|
if ((rcommand == 0x40) && (rchannel == 1)) {
|
|
scommand = 0x40;
|
|
svalue = (testled & 0x01) | (relais1state & 0x02) | (relais2state & 0x04) | (pc0state & 0x08) | (pc1state & 0x10);
|
|
}
|
|
if ((rcommand == 0x40) && ((rchannel > 1) && (rchannel < 4))) {
|
|
ADMUX = (ADMUX & ~(0x1F)) | (rchannel);
|
|
ADCSRA |= (1<<ADSC);
|
|
loop_until_bit_is_clear(ADCSRA, ADSC);
|
|
scommand = 0x40;
|
|
svalue = ADCW;
|
|
}
|
|
encodepart(i);
|
|
}
|
|
smode = 0;
|
|
sendmsg();
|
|
acktimer = 0;
|
|
waitforsend = 0;
|
|
}
|
|
if ((rrecv == 0) && (rmode == 0) && waitforsend) {
|
|
if (!acktimer) {
|
|
scommand = 0x40;
|
|
schannel = 1;
|
|
svalue = (testled & 0x01) | (relais1state & 0x02) | (relais2state & 0x04) | (pc0state & 0x08) | (pc1state & 0x10);
|
|
encodepart(0);
|
|
scommand = 0;
|
|
encodepart(1);
|
|
encodepart(2);
|
|
encodepart(3);
|
|
smode = 1;
|
|
sendmsg();
|
|
acktimer = my_prio;
|
|
} else
|
|
acktimer--;
|
|
}
|
|
rflag = 0;
|
|
}
|
|
if (bit_is_clear(PIND, TestButton) && (!testbutton)) {
|
|
testbutton = ~0;
|
|
testled = ~testled;
|
|
waitforsend = ~0;
|
|
if (testled)
|
|
PORTD |= (1<<TestLED);
|
|
else
|
|
PORTD &= ~(1<<TestLED);
|
|
}
|
|
if (bit_is_set(PIND, TestButton) && testbutton)
|
|
testbutton = 0;
|
|
if (bit_is_clear(PINC, PC0) && (!pc0state)) {
|
|
pc0state = ~0;
|
|
waitforsend = ~0;
|
|
}
|
|
if (bit_is_set(PINC, PC0) && (pc0state)) {
|
|
pc0state = 0;
|
|
waitforsend = ~0;
|
|
}
|
|
if (bit_is_clear(PINC, PC1) && (!pc1state)) {
|
|
pc1state = ~0;
|
|
waitforsend = ~0;
|
|
}
|
|
if (bit_is_set(PINC, PC1) && (pc1state)) {
|
|
pc1state = 0;
|
|
waitforsend = ~0;
|
|
}
|
|
}
|
|
}
|