2 // YOU MUST COMPILE THIS WITH -O3 OR THE AVR WILL NOT BE ABLE TO KEEP UP!!!!
\r
5 #define F_CPU 3960000
\r
7 #include <avr/delay.h>
\r
9 #include <avr/signal.h>
\r
10 #include <avr/interrupt.h>
\r
12 unsigned char val = 0;
\r
14 volatile unsigned char dataReady = 0;
\r
15 volatile unsigned char data = 0;
\r
17 void initUART1(unsigned int baudRate, unsigned int doubleRate) {
\r
18 UBRRHI = (((baudRate) >> 8) & 0x000F);
\r
19 UBRR1 = ((baudRate) & 0x00FF);
\r
20 UCSR1B |= ((1 << RXEN1) | (1 << TXEN1) | (1 << RXCIE1));
\r
23 UCSR1A |= (1 << U2X1);
\r
25 UCSR1A &= ~(1 << U2X1);
\r
29 #define BUFSIZE (1024 *2)
\r
30 static char buf[BUFSIZE];
\r
31 volatile static int head = 0;
\r
32 volatile static int tail = 0;
\r
35 while(!(UCSR1A & (1 << UDRE1))); /* Wait for data Regiester to be empty */
\r
39 inline void portd(int bit, int on) {
\r
46 inline void cts(int c) {
\r
56 inline int inc(int x) { x++; if (x>=BUFSIZE) x=0; return x; }
\r
57 inline int full() { return inc(tail)==head; }
\r
58 inline int nearlyFull() {
\r
59 if (tail==head) return 0;
\r
60 if (tail < head) return (head-tail) < (BUFSIZE/2);
\r
61 return (tail-head) > (BUFSIZE/2);
\r
63 inline int empty() { return head==tail; }
\r
65 inline char recv() {
\r
68 while(empty()) cts(1);
\r
71 if (!nearlyFull()) cts(0);
\r
76 EIMF = 0xFF; /* Enalbe External Interrrupt*/
\r
77 DDRD = 0xFF; /* Configure PORTD as Output */
\r
78 DDRE = 1 << 4; /* ability to write to E */
\r
84 void conf(int z, int y, int x, int d) {
\r
93 for(i=0; i<5; i++) {
\r
104 wdt_enable(WDTO_250MS);
\r
108 SIGNAL(SIG_INTERRUPT1) {
\r
112 void die() { cli(); cts(0); _delay_ms(2000); while(1) { } }
\r
113 SIGNAL(SIG_UART1_RECV) {
\r
114 if (UCSR1A & (1 << FE1)) { portd(2,0); portd(3,1); die(); } // framing error, lock up with LED=01
\r
115 if ((UCSR1A & (1 << OR1))) { portd(2,1); portd(3,0); die(); } // overflow; lock up with LED=10
\r
116 if (full()) { portd(2,1); portd(3,1); die(); } // buffer overrun
\r
121 if (nearlyFull()) cts(0);
\r
124 inline int hex(char c) {
\r
125 if (c >= '0' && c <= '9') return (c - '0');
\r
126 if (c >= 'a' && c <= 'f') return ((c - 'a') + 0xa);
\r
127 if (c >= 'A' && c <= 'F') return ((c - 'A') + 0xa);
\r
143 int i, x=0, y=0, z=0, d=0;
\r