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.

131 lines
2.8 KiB
C

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/eeprom.h>
#include "alarmhw.h"
#include "rs485.h"
#include "eb.h"
uint8_t reedstate;
uint8_t buzzerstate;
uint8_t waitforack;
uint8_t waitforsend;
uint8_t acktimer;
int main(void)
{
uint8_t i;
init_rs485();
DDRD |= (1<<RedLED);
DDRD |= (1<<GreenLED);
DDRD |= (1<<BuzzerON);
DDRD &= ~(1<<ReedSwitch);
PORTD &= ~(1<<RedLED);
PORTD |= (1<<GreenLED);
PORTD &= ~(1<<BuzzerON);
PORTD |= (1<<ReedSwitch);
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);
buzzerstate = 0;
reedstate = 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 = 2; /* 2 LED keys + 4 ADCs */
}
if ((rcommand == 0x60) && (rchannel == 1)) {
if (rvalue & 0x02) {
buzzerstate = ~0;
PORTD |= (1<<BuzzerON);
} else {
buzzerstate = 0;
PORTD &= ~(1<<BuzzerON);
}
scommand = 0x40;
svalue = (reedstate & 0x01) | (buzzerstate & 0x02);
}
if ((rcommand == 0x40) && (rchannel == 1)) {
scommand = 0x40;
svalue = (reedstate & 0x01) | (buzzerstate & 0x02);
}
if ((rcommand == 0x40) && ((rchannel > 1) && (rchannel < 6))) {
ADMUX = (ADMUX & ~(0x1F)) | (rchannel - 2);
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 = (reedstate & 0x01) | (buzzerstate & 0x02);
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, ReedSwitch) && (!reedstate)) {
reedstate = ~0;
waitforsend = ~0;
PORTD |= (1<<RedLED);
PORTD &= ~(1<<GreenLED);
}
if (bit_is_set(PIND, ReedSwitch) && reedstate) {
reedstate = 0;
waitforsend = ~0;
PORTD &= ~(1<<RedLED);
PORTD |= (1<<GreenLED);
}
}
}