/* avr_misc.c provide functions to set up hardware Copyright 2005 EIT European Infotech AB www.eit.se. This file is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. For a list ov available signals see: http://www.nongnu.org/avr-libc/user-manual/index.html History 2005-02-16 Created. Henrik Björkman 2005-02-20 Set up timer0 interrupt Henrik Björkman 2005-02-22 Using timer1 to generate 38 MHz. Will need that to send IR later. Henrik Björkman 2005-02-22 Using timer2 to get a timer with a 1 second resolution Henrik Björkman 2005-02-22 Will try to use timer2 interrupt to make a software uart together with timer1 2005-07-01 cleanup Henrik 2005-07-02 Moved timer drivers to own files. Henrik */ // When reading 16bit reg remember to read low byte first then high byte // when writing write high byte first then low byte but hopefully the C compiler does this. // includes #include #include #include #include "avr_cfg.h" #include "avr_misc.h" #include "avr_uart.h" #if (!(defined(__AVR_ATmega8__) || defined(__AVR_ATmega88__))) #error /* well it should work with other atmega mcus to but that is not tested. */ #endif // local variables and defines /* compatibilty macros for old style */ /* for new design instead of "sbi(DDRC,PC5);" use "DDRC|= _BV(PC5);" */ #if 0 #ifndef cbi #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) #endif #ifndef sbi #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit)) #endif #ifndef outp #define outp(value,sfr) (_SFR_BYTE(sfr) = value) #endif #ifndef inp #define inp(sfr) (_SFR_BYTE(sfr)) #endif #endif // This function will set CPU in idle mode, saving energy. inline void avr_idle(void) { #ifdef __AVR_ATmega8__ // enable sleep MCUCR|=(1<0) { for (i=0; i<(AVR_FOSC/6000); i++) { asm ("nop"); } ms--; } } #else void avr_delay_ms(int ms) { while(ms>0) { delay_us(990); ms--; } } #endif void avr_delay_us(int us) { while(us>0) { #if AVR_FOSC==16000000L asm ("nop"); asm ("nop"); asm ("nop"); asm ("nop"); asm ("nop"); asm ("nop"); asm ("nop"); asm ("nop"); asm ("nop"); #elif AVR_FOSC==8000000L asm ("nop"); #else #error selected AVR_FOSC not supported by delay_us #endif us--; } } // set up hardware (port directions, registers etc.) void avr_init() { // Analog comparator ACSR|=1<