1 | #include <avr/io.h>
|
2 | #include <avr/wdt.h>
|
3 |
|
4 |
|
5 | #define BAUD 9600UL
|
6 |
|
7 | // F_CPU im Makefile auf 8000000 gesetzt
|
8 |
|
9 | #define UBRRVAL (F_CPU/(BAUD*16)-1)
|
10 |
|
11 | void init() {
|
12 | // setting PC0 - PC2 to output
|
13 | DDRB = (1 << PB2) | (1 << PB3) | (1 << PB4);
|
14 |
|
15 | // 00011100
|
16 | PORTB |= 0x1c;
|
17 |
|
18 |
|
19 | /* set baud rate */
|
20 | UBRRH = UBRRVAL >> 8;
|
21 | UBRRL = UBRRVAL & 0xff;
|
22 |
|
23 | UCSRB = _BV(RXEN) ;
|
24 |
|
25 | /* set frame format: 8 bit, no parity, 1 bit */
|
26 | UCSRC = (1 << UCSZ1) | (1 << UCSZ0);
|
27 |
|
28 | }
|
29 |
|
30 | void uartSendByte(uint8_t c) {
|
31 | /* Wait for empty transmit buffer */
|
32 | loop_until_bit_is_set(UCSRA, UDRE);
|
33 | UDR = c;
|
34 | }
|
35 |
|
36 |
|
37 | uint8_t uartGetByte() {
|
38 | loop_until_bit_is_set(UCSRA, RXC);
|
39 | checkErrors();
|
40 | return UDR;
|
41 | }
|
42 |
|
43 |
|
44 |
|
45 | void setError1() {
|
46 | PORTB &= ~(1 << PB4);
|
47 | }
|
48 | void resetError1() {
|
49 | PORTB |= (1 << PB4);
|
50 | }
|
51 |
|
52 | void setError2() {
|
53 | PORTB &= ~(1 << PB2);
|
54 | }
|
55 | void resetError2() {
|
56 | PORTB |= (1 << PB2);
|
57 | }
|
58 |
|
59 | void setOk() {
|
60 | PORTB &= ~(1 << PB3);
|
61 | }
|
62 |
|
63 | void resetOk() {
|
64 | PORTB |= (1 << PB3);
|
65 | }
|
66 |
|
67 | int checkErrors() {
|
68 | if (UCSRA & _BV(FE)) {
|
69 | // red led on
|
70 | setError1();
|
71 | return -1;
|
72 | }
|
73 | if (UCSRA & _BV(DOR)) {
|
74 | return -2;
|
75 | }
|
76 | // red led off
|
77 | resetError1();
|
78 | return 0;
|
79 | }
|
80 |
|
81 |
|
82 | int main(void) {
|
83 | uint8_t c;
|
84 | init();
|
85 | wdt_enable(WDTO_1S);
|
86 |
|
87 | OSCCAL = 0x10;
|
88 | while(1){
|
89 |
|
90 | c = uartGetByte();
|
91 |
|
92 | if(c == '$') {
|
93 | // green led on
|
94 | setOk();
|
95 | wdt_reset();
|
96 | }
|
97 | }
|
98 | return 0;
|
99 | }
|