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 #if !defined(__AVR_AT94K__)
\r
8 #error you forgot to put -mmcu=at94k on the command line
\r
11 #include <avr/wdt.h>
\r
12 #include <util/delay.h>
\r
14 #include <avr/interrupt.h>
\r
16 unsigned char val = 0;
\r
18 volatile unsigned char dataReady = 0;
\r
19 volatile unsigned char data = 0;
\r
21 void initUART1(unsigned int baudRate, unsigned int doubleRate) {
\r
22 UBRRHI = (((baudRate) >> 8) & 0x000F);
\r
23 UBRR1 = ((baudRate) & 0x00FF);
\r
24 UCSR1B |= ((1 << RXEN1) | (1 << TXEN1) | (1 << RXCIE1));
\r
27 UCSR1A |= (1 << U2X1);
\r
29 UCSR1A &= ~(1 << U2X1);
\r
33 #define BUFSIZE (1024 *2)
\r
34 static char buf[BUFSIZE];
\r
35 volatile static int head = 0;
\r
36 volatile static int tail = 0;
\r
39 while(!(UCSR1A & (1 << UDRE1))); /* Wait for data Regiester to be empty */
\r
43 inline void portd(int bit, int on) {
\r
50 inline void cts(int c) {
\r
60 inline int inc(int x) { x++; if (x>=BUFSIZE) x=0; return x; }
\r
61 inline int full() { return inc(tail)==head; }
\r
62 inline int nearlyFull() {
\r
63 if (tail==head) return 0;
\r
64 if (tail < head) return (head-tail) < (BUFSIZE/2);
\r
65 return (tail-head) > (BUFSIZE/2);
\r
67 inline int empty() { return head==tail; }
\r
69 inline char recv() {
\r
72 while(empty()) cts(1);
\r
75 if (!nearlyFull()) cts(0);
\r
80 EIMF = 0xFF; /* Enalbe External Interrrupt*/
\r
81 DDRD = 0xFF; /* Configure PORTD as Output */
\r
82 DDRE = 1 << 4; /* ability to write to E */
\r
90 void conf(int z, int y, int x, int d) {
\r
99 for(i=0; i<5; i++) {
\r
110 wdt_enable(WDTO_250MS);
\r
114 #define TIMERVAL 100
\r
115 ISR(SIG_OVERFLOW0) {
\r
117 TCNT0 = TIMERVAL; // load the nearest-to-one-second value into the timer0
\r
118 TIMSK |= (1<<TOIE0); //enable the compare match1 interrupt and the timer/counter0 overflow interrupt
\r
121 void init_timer() {
\r
122 TCCR0 |= (1<<CS00); // set the timer0 prescaler to CK
\r
123 TCNT0 = TIMERVAL; // load the nearest-to-one-second value into the timer0
\r
124 TIMSK |= (1<<TOIE0); //enable the compare match1 interrupt and the timer/counter0 overflow interrupt
\r
127 ISR(SIG_INTERRUPT1) { // use interrupt1 since interrupt0 is sent by the watchdog (I think)
\r
131 void die() { cli(); cts(0); _delay_ms(2000); while(1) { } }
\r
132 ISR(SIG_UART1_RECV) {
\r
133 if (UCSR1A & (1 << FE1)) { portd(2,0); portd(3,1); die(); } // framing error, lock up with LED=01
\r
134 if ((UCSR1A & (1 << OR1))) { portd(2,1); portd(3,0); die(); } // overflow; lock up with LED=10
\r
135 if (full()) { portd(2,1); portd(3,1); die(); } // buffer overrun
\r
140 if (nearlyFull()) cts(0);
\r
143 inline int hex(char c) {
\r
144 if (c >= '0' && c <= '9') return (c - '0');
\r
145 if (c >= 'a' && c <= 'f') return ((c - 'a') + 0xa);
\r
146 if (c >= 'A' && c <= 'F') return ((c - 'A') + 0xa);
\r
162 int i, x=0, y=0, z=0, d=0;
\r