]> cloudbase.mooo.com Git - irmp.git/blob - irmp-main-pic-12F1840.c
Version 3.0.7 - added SAMSUNGAH protocol, improved some code for ESP8266
[irmp.git] / irmp-main-pic-12F1840.c
1 /*---------------------------------------------------------------------------------------------------------------------------------------------------
2 * irmp-main-pic-12F1840.c - example main module for PIC 12f1840
3 *
4 * IR decoder using IRMP
5 *
6 * (c) 2014 Wolfgang Strobl (news4 at mystrobl.de) 2014-03-12:2014-07-20
7 *
8 * $Id: irmp-main-pic-12F1840.c,v 1.6 2017/02/17 09:13:06 fm Exp $
9 *
10 * This demo module is runnable on a Microchip PIC 12F1840
11 *
12 * To be used with IRMP by Frank Meyer (frank(at)fli4l.de)
13 * <http://www.mikrocontroller.net/articles/IRMP>
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *---------------------------------------------------------------------------------------------------------------------------------------------------
20 */
21
22
23 /*
24
25 Hauptprogramm fuer den nachfolgenden Testaufbau, bestehend aus zwei mal
26
27
28 TSOP1736+ PIC12F1840
29 | __
30 1k | Vdd--o| o|o--Vss
31 ___ +--GP5--o| |o--GP0 / ICSPDAT
32 RS232 out -|___|-------GP4--o| |o--GP1 / ICSPCLK ___ LED
33 RS232 in -|___|---Vpp/GP3--o|__|o--GP2-----------|___|- ->|---Vss
34 10k
35
36 auf einem Steckbrett. (Genauer gesagt, dies ist die aktuelle Beschaltung
37 fuer V1.8, V1.0 ist aber bzgl. IRMP-Empfang funktional identisch. Nicht eingezeichnet
38 ist ein Abblockkondensator von 100nF ueber Vss und Vdd.
39
40 Uebersetzt mit Microchip MPLAB XC8 C Compiler (Free Mode) V1.31
41 im stark gecrippelten "Free Mode".
42
43 Memory Summary: (V 1.8)
44 Program space used C6Fh ( 3183) of 1000h words ( 77.7%)
45 Data space used 9Bh ( 155) of 100h bytes ( 60.5%)
46 EEPROM space used 0h ( 0) of 100h bytes ( 0.0%)
47 Data stack space used 0h ( 0) of 5Eh bytes ( 0.0%)
48 Configuration bits used 2h ( 2) of 2h words (100.0%)
49 ID Location space used 0h ( 0) of 4h bytes ( 0.0%)
50
51
52 Testaufbau:
53
54 Zwei Steckbretter,
55 urspruengliche Version des Programms als Empfaenger,
56 aktuelle Version als Sender, Aufzeichnung mit putty,
57 angeschlossen jeweils per USB2RS232-Kabel von Conrad
58 (972543, basierend auf Prolific PL2303). Soft-UART
59 fuer Input, da 12F1820 keine Kontrolle ueber Input-
60 Polaritaet erlaubt und ich fuer Testaufbauten eine
61 Minimalbeschaltung bevorzuge.
62
63 Kurze Distanz
64 (~30 cm) zwischen Sender und Empfaenger), keine genaue Ausrichtung.
65 Stromversorgung wahlwweise mit 5V via PICkit 2 oder 3x1.2V NiMH-AA.
66
67 Zunaechst
68 CD TAPE TUNER AUX OFF mit Philips FB,
69 OFF mit VAOVA TV-2900HDD FB
70 dann Eingabe . und n beim Sender.
71
72 Sender:
73
74 IRMP PIC 12F1840 1.8 ws
75 P 7 a=0x0014 c=0x003f f=0x00 (RC5)
76 P 7 a=0x0014 c=0x003f f=0x01 (RC5)
77 P 7 a=0x0012 c=0x003f f=0x00 (RC5)
78 P 7 a=0x0011 c=0x003f f=0x00 (RC5)
79 P 7 a=0x0015 c=0x003f f=0x00 (RC5)
80 P 7 a=0x0015 c=0x000c f=0x00 (RC5)
81 P 2 a=0xbf00 c=0x0059 f=0x00 (NEC)
82 P 2 a=0xbf00 c=0x0059 f=0x01 (NEC)
83 . MX115OFF PR2 221
84 n NEC PR2 209
85
86 Empfaenger:
87
88 IRMP PIC 12F1840 1.0 ws
89 P 7 a=0x0014 c=0x003f f=0x00 (RC5)
90 P 7 a=0x0014 c=0x003f f=0x01 (RC5)
91 P 7 a=0x0012 c=0x003f f=0x00 (RC5)
92 P 7 a=0x0011 c=0x003f f=0x00 (RC5)
93 P 7 a=0x0015 c=0x000c f=0x00 (RC5)
94 P 2 a=0xbf00 c=0x0059 f=0x00 (NEC)
95 P 2 a=0xbf00 c=0x0059 f=0x01 (NEC)
96 P 7 a=0x0015 c=0x000c f=0x00 (RC5)
97 P 7 a=0x0015 c=0x000c f=0x01 (RC5)
98 P 7 a=0x0015 c=0x000c f=0x01 (RC5)
99 P 2 a=0x0055 c=0x00aa f=0x00 (NEC)
100
101 Die via DSO an der LED gemessenen Frequenzen sind 36.0 resp. 38.0 kHz
102
103 */
104
105 #include <stdio.h>
106
107 #include "irmp.h"
108 #include "irsnd.h"
109
110 /******************************************************************************/
111 // "system.h"
112 /******************************************************************************/
113 #define SYS_FREQ 32000000L
114 #define _XTAL_FREQ 32000000 // for _delay
115 #define FCY (SYS_FREQ/4)
116
117 /******************************************************************************/
118 // "user.c"
119 /******************************************************************************/
120 void InitApp(void)
121 {
122 ANSELA=0;
123 TRISA4=0;
124 IRCF0=0; // p. 45
125 IRCF1=1;
126 IRCF2=1;
127 IRCF3=1;
128 SPLLEN=1; // p 46 and 54
129 }
130
131 /******************************************************************************/
132 // "configuration_bits.c" 12F1848
133 /******************************************************************************/
134 // CONFIG1
135 #pragma config FOSC = INTOSC // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
136 #pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled)
137 #pragma config PWRTE = ON // Power-up Timer Enable (PWRT enabled)
138 #pragma config MCLRE = OFF // MCLR Pin Function Select (MCLR/VPP pin function is digital input)
139 #pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled)
140 #pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled)
141 #pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled)
142 #pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
143 #pragma config IESO = OFF // Internal/External Switchover (Internal/External Switchover mode is disabled)
144 #pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled)
145
146 // CONFIG2
147 #pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)
148 #pragma config PLLEN = OFF // PLL Enable (4x PLL disabled)
149 #pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
150 #pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
151 #pragma config LVP = OFF // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)
152 /******************************************************************************/
153
154 /******************************************************************************/
155 // UART
156 /******************************************************************************/
157
158 #define GPIO3 RA3
159 #define GPIO4 RA4
160
161 #define SOFTUART_RXPIN GPIO3
162 #define SOFTUART_STDIO 1
163 #define SOFTUART_DI 1
164
165
166 #define BAUD 19200 // 38200 ginge auch noch
167 #define SOFTUART_BAUD BAUD
168 #include "softuart_pic.h"
169
170 #define kbhit softuartkbhit
171
172
173 void
174 RS232init(void)
175 {
176 // Transmit
177 TXCKSEL = 1; // put TX on pin 4 - not 0 -, p 102
178 SPBRGL = (_XTAL_FREQ/BAUD/64-1);
179 SPBRGH = 0;
180 BRGH = 0;
181 BRG16 = 0;
182 // p 259 manual
183 SYNC = 0; // 0 p. 267
184 SPEN = 1; // 26.1.1.7
185 SCKP = 1; // invert p 269
186 TXEN = 1;
187 }
188
189 // EUSART transmit
190 void
191 putch(char c)
192 {
193 while (!TRMT) _delay(1);
194 TXREG=c;
195 }
196
197 /******************************************************************************/
198 // Timer and ISR
199 /******************************************************************************/
200
201 void
202 timer1_init(void)
203 {
204 // p 154
205 TMR1=0xFC00; // p. 155 wait 1024 cycles for stabilization.
206 TMR1CS1=0; // Clock source == System Clock
207 TMR1CS0=1;
208 TMR1IE=1; // enable TMR1 interrupts
209 PEIE=1; // enable Pheripheral Interrupts
210 TMR1IF=0;
211 TMR1ON=1;
212 }
213
214
215 /******************************************************************************/
216 // Interrupt handler
217 /******************************************************************************/
218
219 void interrupt isr(void)
220 {
221 TMR1=0xffff-_XTAL_FREQ/F_INTERRUPTS;
222 TMR1IF=0; // clear timer 1 interrupt
223
224 if (!irsnd_ISR())
225 {
226 irmp_ISR();
227 }
228 }
229
230
231 IRMP_DATA irmp_data;
232
233 void RC5(uint16_t addr,uint16_t cmd, uint8_t repetitions)
234 {
235 irmp_data.protocol = IRMP_RC5_PROTOCOL;
236 irmp_data.address = addr;
237 irmp_data.command = cmd;
238 irmp_data.flags = repetitions;
239 irsnd_send_data (&irmp_data, FALSE);
240 }
241
242 void NEC(int addr,int cmd)
243 {
244 irmp_data.protocol = IRMP_NEC_PROTOCOL;
245 irmp_data.address = addr;
246 irmp_data.command = cmd;
247 irmp_data.flags = 0;
248 irsnd_send_data (&irmp_data, FALSE);
249 }
250
251
252 /******************************************************************************/
253 // MAIN
254 /******************************************************************************/
255
256 int
257 main (void)
258 {
259 IRMP_DATA irmp_data;
260 char c;
261 InitApp();
262
263 PWMoff();
264 RS232init();
265
266 __delay_ms(200);
267 printf("IRMP PIC 12F1840 1.8 ws\r\n");
268 irmp_init(); // initialize irmp
269 timer1_init(); // initialize timer1
270 ei(); // enable interrupts
271 TMR1ON=1; // start timer
272
273 for (;;)
274 {
275 if (kbhit())
276 {
277 c=getch();
278 if (c>32 && c<127) putch(c);
279 putch(' ');
280 if (c=='.')
281 {
282 printf("MX115OFF ");
283 RC5(0x15,0x0c,2); // Philips MC115 AUX OFF
284 }
285 else if (c=='n')
286 {
287 printf("NEC ");
288 NEC(0x55,0xaa);
289 }
290 else
291 {
292 putch('?');
293 continue;
294 }
295 while (irsnd_is_busy ()) ;
296 printf("PR2 %d\r\n",PR2);
297 continue;
298 }
299 if (irmp_get_data (&irmp_data))
300 {
301 printf("P ");
302 printf("%d a=0x%04x c=0x%04x f=0x%02x (",irmp_data.protocol, irmp_data.address,irmp_data.command,irmp_data.flags);
303
304 #if IRMP_PROTOCOL_NAMES
305 printf(irmp_protocol_names[irmp_data.protocol]);
306 #else
307 switch(irmp_data.protocol)
308 {
309 case 1:
310 printf("Sony");
311 break;
312 case 2:
313 printf("NEC");
314 break;
315 case 7:
316 printf("RC5");
317 break;
318 case 0x21:
319 printf("Ortek");
320 break;
321 }
322 #endif
323 printf(")\r\n");
324 }
325 }
326 }