-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
61 lines (51 loc) · 1.18 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// coding: utf-8
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include "rs232.h"
volatile uint8_t rxcmd[4]; // Empfangenes Kommando via serielle Schnittstelle
volatile uint8_t rxcmdcnt; // Zu beschreibendes Byte in rxcmd
volatile uint8_t cmdtimeout;
uint8_t relaisstate;
#define ADRESSE 0x00
void _timer_init(){
TCCR1B = (1<<CS02)|(1<<CS00);
TIMSK |= (1<<TOIE1);
}
void init(void){
_usart_init();
_timer_init();
DDRB |= (1<<PB0)|(1<<PB1)|(1<<PB2)|(1<<PB3)|(1<<PB4)|(1<<PB5)|(1<<PB6)|(1<<PB7); // Ausgänge für die Relais
sei();
}
int main(void) {
init();
while(1) {
// Eingangsbuffer prüfen, Relais schalten, Buffer töten
if (rxcmdcnt > 3){
if ((rxcmd[0] == 0x01) && (rxcmd[1] == ADRESSE) && (rxcmd[2] != relaisstate) && (rxcmd[0] ^ rxcmd[1] ^ rxcmd[2] == rxcmd[3])){
PORTB = rxcmd[2];
relaisstate = rxcmd[2];
}
rxcmdcnt = 0;
}
}
return 0;
}
ISR(USART_RXC_vect){
uint8_t chr = UDR;
if (rxcmdcnt < 4){
rxcmd[rxcmdcnt] = chr;
rxcmdcnt++;
} else {
// Vorheriger Wert noch in Bearheitung?
}
}
ISR(TIMER1_OVF_vect){
// Tötet den Kommandopuffer
if (rxcmdcnt == cmdtimeout){
rxcmdcnt = 0;
} else {
cmdtimeout = rxcmdcnt;
}
}