/*---------------------------------------------------------------------------------------------------------------------------------------------------\r
- * irsndmain.c - demo main module to test irmp decoder\r
+ * irsndmain.c - demo main module to test IRSND encoder on AVRs\r
*\r
- * Copyright (c) 2010 Frank Meyer - frank(at)fli4l.de\r
+ * Copyright (c) 2010-2014 Frank Meyer - frank(at)fli4l.de\r
*\r
- * ATMEGA88 @ 8 MHz\r
- *\r
- * Fuses: lfuse: 0xE2 hfuse: 0xDC efuse: 0xF9\r
+ * ATMEGA88 @ 8 MHz internal RC Osc with BODLEVEL 4.3V: lfuse: 0xE2 hfuse: 0xDC efuse: 0xF9\r
+ * ATMEGA88 @ 8 MHz external Crystal Osc with BODLEVEL 4.3V: lfuse: 0xFF hfuse: 0xDC efuse: 0xF9\r
*\r
* This program is free software; you can redistribute it and/or modify\r
* it under the terms of the GNU General Public License as published by\r
* (at your option) any later version.\r
*---------------------------------------------------------------------------------------------------------------------------------------------------\r
*/\r
-\r
-/*---------------------------------------------------------------------------------------------------------------------------------------------------\r
- * uncomment this for codevision compiler:\r
- *---------------------------------------------------------------------------------------------------------------------------------------------------\r
- */\r
-// #define CODEVISION // to use Codevision Compiler instead of gcc\r
-\r
-#ifdef CODEVISION\r
-#include <mega88.h>\r
-#include <stdio.h>\r
-#define uint8_t unsigned char\r
-#define uint16_t unsigned int\r
-#define F_CPU 8000000 // change for Codevision here, if you use WinAVR, use Project -> Configuration Options instead\r
-\r
-// register values from datasheet for ATMega88\r
-#define OCIE1A 1\r
-#define WGM12 3\r
-#define CS10 0\r
-#define UDRE0 5\r
-#define TXEN0 3\r
-\r
-#include "irmp.h"\r
-#include "isnd.h"\r
-#include "irmp.c"\r
-#include "isnd.c"\r
-\r
-#else // gcc compiler\r
-\r
-#include <inttypes.h>\r
-#include <avr/io.h>\r
-#include <util/delay.h>\r
-#include <avr/pgmspace.h>\r
-#include <avr/interrupt.h>\r
-#include "irmp.h"\r
-#include "irsndconfig.h"\r
#include "irsnd.h"\r
\r
-#endif // CODEVISION\r
-\r
#ifndef F_CPU\r
-#error F_CPU unkown\r
+# error F_CPU unkown\r
#endif\r
\r
void\r
-timer_init (void)\r
+timer1_init (void)\r
{\r
-#ifdef CODEVISION\r
- OCR1AH = ((F_CPU / F_INTERRUPTS) >> 8) & 0xFF; // compare value: 1/10000 of CPU frequency (upper byte)\r
- OCR1AL = ((F_CPU / F_INTERRUPTS) - 1) & 0xFF; // compare value: 1/10000 of CPU frequency (lower byte)\r
-#else // gcc\r
- OCR1A = (F_CPU / F_INTERRUPTS) - 1; // compare value: 1/10000 of CPU frequency\r
-#endif // CODEVISION\r
- TCCR1B = (1 << WGM12) | (1 << CS10); // switch CTC Mode on, set prescaler to 1\r
+#if defined (__AVR_ATtiny45__) || defined (__AVR_ATtiny85__) // ATtiny45 / ATtiny85:\r
+ OCR1C = (F_CPU / F_INTERRUPTS / 4) - 1; // compare value: 1/15000 of CPU frequency, presc = 4\r
+ TCCR1 = (1 << CTC1) | (1 << CS11) | (1 << CS10); // switch CTC Mode on, set prescaler to 4\r
+#else // ATmegaXX:\r
+ OCR1A = (F_CPU / F_INTERRUPTS) - 1; // compare value: 1/15000 of CPU frequency\r
+ TCCR1B = (1 << WGM12) | (1 << CS10); // switch CTC Mode on, set prescaler to 1\r
+#endif\r
\r
-#if defined (__AVR_ATmega8__) || defined (__AVR_ATmega16__) || defined (__AVR_ATmega32__) || defined (__AVR_ATmega64__) || defined (__AVR_ATmega162__)\r
- TIMSK = 1 << OCIE1A; // OCIE1A: Interrupt by timer compare (use TIMSK for ATMEGA162)\r
+#ifdef TIMSK1\r
+ TIMSK1 = 1 << OCIE1A; // OCIE1A: Interrupt by timer compare\r
#else\r
- TIMSK1 = 1 << OCIE1A; // OCIE1A: Interrupt by timer compare (use TIMSK for ATMEGA162)\r
-#endif // __AVR...\r
+ TIMSK = 1 << OCIE1A; // OCIE1A: Interrupt by timer compare\r
+#endif\r
}\r
\r
+#ifdef TIM1_COMPA_vect // ATtiny84\r
+#define COMPA_VECT TIM1_COMPA_vect\r
+#else\r
+#define COMPA_VECT TIMER1_COMPA_vect // ATmega\r
+#endif\r
+\r
/*---------------------------------------------------------------------------------------------------------------------------------------------------\r
* timer 1 compare handler, called every 1/10000 sec\r
*---------------------------------------------------------------------------------------------------------------------------------------------------\r
*/\r
-// Timer 1 output compare A interrupt service routine\r
-#ifdef CODEVISION\r
-interrupt [TIM1_COMPA] void timer1_compa_isr(void)\r
-#else // CODEVISION\r
-ISR(TIMER1_COMPA_vect)\r
-#endif // CODEVISION\r
+ISR(COMPA_VECT) // Timer1 output compare A interrupt service routine, called every 1/15000 sec\r
{\r
(void) irsnd_ISR(); // call irsnd ISR\r
- // call other timer interrupt routines...\r
+ // call other timer interrupt routines here...\r
}\r
\r
/*---------------------------------------------------------------------------------------------------------------------------------------------------\r
* MAIN: main routine\r
*---------------------------------------------------------------------------------------------------------------------------------------------------\r
*/\r
-#ifdef CODEVISION\r
-// This is the main routine if you use Codevision C Compiler\r
-void\r
-main (void)\r
-{\r
- IRMP_DATA irmp_data;\r
-\r
- #pragma optsize-\r
- // crystal oscillator division factor: 1\r
- CLKPR=0x80;\r
- CLKPR=0x00;\r
- #ifdef _OPTIMIZE_SIZE_\r
- #pragma optsize+\r
- #endif\r
-\r
- irsnd_init(); // initialize irsnd\r
- timer_init(); // initialize timer\r
- #asm("sei"); // enable interrupts\r
-\r
- for (;;)\r
- {\r
- irmp_data.protocol = IRMP_NEC_PROTOCOL;\r
- irmp_data.address = 0x00FF;\r
- irmp_data.command = 0x0001;\r
- irmp_data.flags = 0;\r
-\r
- irsnd_send_data (&irmp_data);\r
- _delay_ms (1000);\r
- }\r
-}\r
-\r
-#else // gcc\r
-\r
-// This is the main routine if you use GCC Compiler\r
int\r
main (void)\r
{\r
- IRMP_DATA irmp_data;\r
-\r
- irsnd_init(); // initialize irsnd\r
- timer_init(); // initialize timer\r
- sei (); // enable interrupts\r
-\r
- for (;;)\r
- {\r
- irmp_data.protocol = IRMP_NEC_PROTOCOL;\r
- irmp_data.address = 0x00FF;\r
- irmp_data.command = 0x0001;\r
- irmp_data.flags = 0;\r
-\r
- irsnd_send_data (&irmp_data);\r
- _delay_ms (1000);\r
- }\r
+ IRMP_DATA irmp_data;\r
+\r
+ irsnd_init(); // initialize irsnd\r
+ timer1_init(); // initialize timer\r
+ sei (); // enable interrupts\r
+\r
+ for (;;)\r
+ {\r
+ irmp_data.protocol = IRMP_NEC_PROTOCOL; // use NEC protocol\r
+ irmp_data.address = 0x00FF; // set address to 0x00FF\r
+ irmp_data.command = 0x0001; // set command to 0x0001\r
+ irmp_data.flags = 0; // don't repeat frame\r
+\r
+ irsnd_send_data (&irmp_data, TRUE); // send frame, wait for completion\r
+ _delay_ms (1000);\r
+ }\r
}\r
-\r
-#endif // CODEVISION / gcc\r