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