diff --git a/src/uart/uart.c b/src/uart/uart.c index ec7faa5..b8af431 100644 --- a/src/uart/uart.c +++ b/src/uart/uart.c @@ -204,7 +204,8 @@ LICENSE: #define UART0_CONTROL UCSR0B #define UART0_DATA UDR0 #define UART0_UDRIE UDRIE0 -#elif defined(__AVR_ATmega164P__) || defined(__AVR_ATmega324P__) || defined(__AVR_ATmega644P__) +#elif defined(__AVR_ATmega164__) || defined(__AVR_ATmega324__) || defined(__AVR_ATmega1284__) || \ + defined(__AVR_ATmega164P__) || defined(__AVR_ATmega324P__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega1284P__) /* ATmega with two USART */ #define ATMEGA_USART0 #define ATMEGA_USART1 diff --git a/src/util.c b/src/util.c index 73e74dd..70d1087 100644 --- a/src/util.c +++ b/src/util.c @@ -24,18 +24,51 @@ extern jmp_buf newmode_jmpbuf; #endif void wait(int ms){ -/* Always use Timer1 except for the Arduino/LoL Shield platform. */ -#ifndef USER_TIMER0_FOR_WAIT + // initialize timer +#if defined (__AVR_ATmega48__) || \ + defined (__AVR_ATmega48P__) || \ + defined (__AVR_ATmega88__) || \ + defined (__AVR_ATmega88P__) || \ + defined (__AVR_ATmega168__) || \ + defined (__AVR_ATmega168P__) || \ + defined (__AVR_ATmega328__) || \ + defined (__AVR_ATmega328P__) || \ + defined (__AVR_ATmega164__) || \ + defined (__AVR_ATmega164P__) || \ + defined (__AVR_ATmega324__) || \ + defined (__AVR_ATmega324P__) || \ + defined (__AVR_ATmega644__) || \ + defined (__AVR_ATmega644P__) || \ + defined (__AVR_ATmega1284__) || \ + defined (__AVR_ATmega1284P__) || \ + defined (__AVR_ATmega32U4__) || \ + defined (__AVR_ATmega1280__) || \ + defined (__AVR_ATmega2560__) + +# ifndef USER_TIMER0_FOR_WAIT + /* Timer1 for the masses */ TCCR1B = _BV(WGM12) | _BV(CS12); //CTC Mode, clk/256 OCR1A = (F_CPU/256000); //1000Hz -/* Some Arduino/LoL Shield variants require Timer1 for multiplexing. Timer0, on - * the other hand, is free to use, which makes it a perfect candidate for our - * wait() function. */ -#else - // disconnect OC0A and OC0B pins, turn on CTC mode, clk/256 - TCCR0A = _BV(WGM01); - TCCR0B = _BV(CS02); +# else + /* Timer0 + * Some Arduino/LoL Shield variants require Timer1 for multiplexing. Timer0, + * on the other hand, is free to use, which makes it a perfect candidate for + * our wait() function. */ + TCCR0A = _BV(WGM01); // CTC mode + TCCR0B = _BV(CS02); // clk/256 OCR0A = (F_CPU/256000); //1000Hz +# endif +#else +# ifndef USER_TIMER0_FOR_WAIT + /* Timer1 */ + TCCR1B = _BV(WGM12) | _BV(CS12); //CTC Mode, clk/256 + OCR1A = (F_CPU/256000); //1000Hz +# else + /* Timer0 */ + /* CTC mode, clk/256 */ + TCCR0 = _BV(WGM01) | _BV(CS02); + OCR0 = (F_CPU/256000); //1000Hz +# endif #endif for(;ms>0;ms--){ @@ -62,19 +95,47 @@ void wait(int ms){ } #endif -#if defined (__AVR_ATmega1280__) || defined (__AVR_ATmega2560__) || defined (__AVR_ATmega32U4__) || defined (__AVR_ATmega644P__) || defined (__AVR_ATmega644__) || defined (__AVR_ATmega328__) || defined (__AVR_ATmega328P__) || (__AVR_ATmega1284P__) || defined (__AVR_ATmega1284__) -/* Timer1 for the masses */ + // busy waiting for compare match interrupt flag +#if defined (__AVR_ATmega48__) || \ + defined (__AVR_ATmega48P__) || \ + defined (__AVR_ATmega88__) || \ + defined (__AVR_ATmega88P__) || \ + defined (__AVR_ATmega168__) || \ + defined (__AVR_ATmega168P__) || \ + defined (__AVR_ATmega328__) || \ + defined (__AVR_ATmega328P__) || \ + defined (__AVR_ATmega164__) || \ + defined (__AVR_ATmega164P__) || \ + defined (__AVR_ATmega324__) || \ + defined (__AVR_ATmega324P__) || \ + defined (__AVR_ATmega644__) || \ + defined (__AVR_ATmega644P__) || \ + defined (__AVR_ATmega1284__) || \ + defined (__AVR_ATmega1284P__) || \ + defined (__AVR_ATmega32U4__) || \ + defined (__AVR_ATmega1280__) || \ + defined (__AVR_ATmega2560__) # ifndef USER_TIMER0_FOR_WAIT - while(!(TIFR1 & _BV(OCF1A))); //wait for compare match flag - TIFR1 |= _BV(OCF1A); //reset flag -/* Timer0 for e.g. Arduino/LoL Shield */ + /* Timer1 for the masses */ + while(!(TIFR1 & _BV(OCF1A))); // wait for compare match flag + TIFR1 |= _BV(OCF1A); // reset that flag # else - while(!(TIFR0 & _BV(OCF0A))); //wait for compare match flag - TIFR0 |= _BV(OCF0A); //reset flag + /* Timer0 for e.g. Arduino/LoL Shield */ + while(!(TIFR0 & _BV(OCF0A))); // wait for compare match flag + TIFR0 |= _BV(OCF0A); // reset that flag # endif #else - while(!(TIFR&(1<