]> cloudbase.mooo.com Git - irmp.git/blob - main.c
bugfix in detection of multiple NEC repetitions, added irmpconfig.h
[irmp.git] / main.c
1 /*---------------------------------------------------------------------------------------------------------------------------------------------------
2 * main.c - demo main module to test irmp decoder
3 *
4 * Copyright (c) 2009-2010 Frank Meyer - frank(at)fli4l.de
5 *
6 * $Id: main.c,v 1.5 2010/03/29 09:33:29 fm Exp $
7 *
8 * ATMEGA88 @ 8 MHz
9 *
10 * Fuses: lfuse: 0xE2 hfuse: 0xDC efuse: 0xF9
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *---------------------------------------------------------------------------------------------------------------------------------------------------
17 */
18
19 /*---------------------------------------------------------------------------------------------------------------------------------------------------
20 * uncomment this for codevision compiler:
21 *---------------------------------------------------------------------------------------------------------------------------------------------------
22 */
23 // #define CODEVISION // to use Codevision Compiler instead of gcc
24
25 #ifdef CODEVISION
26 #include <mega88.h>
27 #include <stdio.h>
28 #define uint8_t unsigned char
29 #define uint16_t unsigned int
30 #define F_CPU 8000000 // change for Codevision here, if you use WinAVR, use Project -> Configuration Options instead
31
32 // register values from datasheet for ATMega88
33 #define OCIE1A 1
34 #define WGM12 3
35 #define CS10 0
36 #define UDRE0 5
37 #define TXEN0 3
38
39 #include "irmp.h"
40 #include "irmp.c"
41
42 #else // gcc compiler
43
44 #include <inttypes.h>
45 #include <avr/io.h>
46 #include <util/delay.h>
47 #include <avr/pgmspace.h>
48 #include <avr/interrupt.h>
49 #include "irmp.h"
50 #include "irmpconfig.h"
51
52
53 #endif // CODEVISION
54
55
56 #ifndef F_CPU
57 #error F_CPU unkown
58 #endif
59
60 void
61 timer_init (void)
62 {
63 #ifdef CODEVISION
64 OCR1AH = ((F_CPU / F_INTERRUPTS) >> 8) & 0xFF; // compare value: 1/10000 of CPU frequency (upper byte)
65 OCR1AL = ((F_CPU / F_INTERRUPTS) - 1) & 0xFF; // compare value: 1/10000 of CPU frequency (lower byte)
66 #else // gcc
67 OCR1A = (F_CPU / F_INTERRUPTS) - 1; // compare value: 1/10000 of CPU frequency
68 #endif // CODEVISION
69 TCCR1B = (1 << WGM12) | (1 << CS10); // switch CTC Mode on, set prescaler to 1
70
71 #if defined (__AVR_ATmega8__) || defined (__AVR_ATmega16__) || defined (__AVR_ATmega32__) || defined (__AVR_ATmega64__) || defined (__AVR_ATmega162__)
72 TIMSK = 1 << OCIE1A; // OCIE1A: Interrupt by timer compare
73 #else
74 TIMSK1 = 1 << OCIE1A; // OCIE1A: Interrupt by timer compare
75 #endif // __AVR...
76 }
77
78 /*---------------------------------------------------------------------------------------------------------------------------------------------------
79 * timer 1 compare handler, called every 1/10000 sec
80 *---------------------------------------------------------------------------------------------------------------------------------------------------
81 */
82 // Timer 1 output compare A interrupt service routine
83 #ifdef CODEVISION
84 interrupt [TIM1_COMPA] void timer1_compa_isr(void)
85 #else // CODEVISION
86 ISR(TIMER1_COMPA_vect)
87 #endif // CODEVISION
88 {
89 irmp_ISR(); // call irmp ISR
90 // call other timer interrupt routines...
91 }
92
93 /*---------------------------------------------------------------------------------------------------------------------------------------------------
94 * MAIN: main routine
95 *---------------------------------------------------------------------------------------------------------------------------------------------------
96 */
97 #ifdef CODEVISION
98 // This is the main routine if you use Codevision C Compiler
99 void
100 main (void)
101 {
102 IRMP_DATA irmp_data;
103
104 #pragma optsize-
105 // crystal oscillator division factor: 1
106 CLKPR=0x80;
107 CLKPR=0x00;
108 #ifdef _OPTIMIZE_SIZE_
109 #pragma optsize+
110 #endif
111 static uint8_t *Proto[]={"SIRCS","NEC","SAMSUNG","MATSUSH","KASEIKYO","RECS80","RC5(x)","DENON","RC6","SAMSG32","APPLE"};
112 #define IRMP_APPLE_ADDRESS 0x77E1
113
114
115 #if IRMP_LOGGING == 0
116 // USART initialization has to be done here if Logging is off
117 // Communication Parameters: 8 Data, 1 Stop, No Parity
118 // USART Receiver: Off
119 // USART Transmitter: On
120 // USART0 Mode: Asynchronous
121 // USART Baud Rate: 9600
122 #define BAUDRATE 9600L
123 UCSR0A=0x00;
124 UCSR0B=0x08;
125 UCSR0C=0x06;
126 UBRR0H = ((F_CPU+BAUDRATE*8)/(BAUDRATE*16)-1) >> 8; // store baudrate (upper byte)
127 UBRR0L = ((F_CPU+BAUDRATE*8)/(BAUDRATE*16)-1) & 0xFF;
128 #endif
129
130 irmp_init(); // initialize rc5
131
132 printf("IRMP V1.0\n");
133 #if IRMP_LOGGING == 1
134 printf("Logging Mode\n");
135 #endif
136
137 timer_init(); // initialize timer
138 #asm("sei"); // enable interrupts
139
140 for (;;)
141 {
142 if (irmp_get_data (&irmp_data))
143 {
144 // ir signal decoded, do something here...
145 // irmp_data.protocol is the protocol, see irmp.h
146 // irmp_data.address is the address/manufacturer code of ir sender
147 // irmp_data.command is the command code
148 #if IRMP_LOGGING != 1
149 if((irmp_data.protocol == IRMP_NEC_PROTOCOL) && (irmp_data.address == IRMP_APPLE_ADDRESS))
150 printf("Code: Apple\n");
151 else printf("Code: %s\n",Proto[irmp_data.protocol-1]);
152 printf("Address: 0x%.2X\n",irmp_data.address);
153 printf("Command: 0x%.2X\n\n",irmp_data.command);
154 #endif
155 }
156 }
157 }
158
159 #else // gcc
160
161 // This is the main routine if you use GCC Compiler
162 int
163 main (void)
164 {
165 IRMP_DATA irmp_data;
166
167 irmp_init(); // initialize rc5
168 timer_init(); // initialize timer
169 sei (); // enable interrupts
170
171 for (;;)
172 {
173 if (irmp_get_data (&irmp_data))
174 {
175 // ir signal decoded, do something here...
176 // irmp_data.protocol is the protocol, see irmp.h
177 // irmp_data.address is the address/manufacturer code of ir sender
178 // irmp_data.command is the command code
179 }
180 }
181 }
182
183 #endif // CODEVISION / gcc