]> 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_pic.c - example main module
3 *
4 * IR decoder using IRMP
5 *
6 * (c) 2014 Wolfgang Strobl (ws at mystrobl.de) 2014-03-12:2014-07-05
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 PIC12F1840
23 ___ __
24 10k -|___|-+ Vdd -o| o|o- Vss
25 ___ +-RS232in / GP5 -o| |o- GP0 / ICSPDAT
26 1k -|___|-- RS232out/ GP4 -o| |o- GP1 / ICSPCLK
27 Vpp / GP3 -o|__|o- GP2 / TS TSOP1736
28 Example output, using a bunch of different remote controls
29 IRMP PIC 12F1840 1.1 ws
30 P 7 a=0x0011 c=0x000c f=0x00 (RC5)
31 P 6 a=0x0001 c=0x0018 f=0x00 (RECS80)
32 P 2 a=0xbf00 c=0x0019 f=0x00 (NEC)
33 P 2 a=0xeb14 c=0x0001 f=0x00 (NEC)
34 P 7 a=0x001c c=0x0005 f=0x00 (RC5)
35 P 7 a=0x000a c=0x0057 f=0x00 (RC5)
36 P 7 a=0x000a c=0x0057 f=0x01 (RC5)
37 P 2 a=0xfb04 c=0x0008 f=0x00 (NEC)
38
39 */
40
41 #include <stdio.h>
42
43 #include "irmp.h"
44
45 /******************************************************************************/
46 // "system.h"
47 /******************************************************************************/
48 #define SYS_FREQ 32000000L
49 #define _XTAL_FREQ 32000000 // for _delay
50 #define FCY (SYS_FREQ/4)
51
52 /******************************************************************************/
53 // "user.c"
54 /******************************************************************************/
55 void InitApp(void)
56 {
57 ANSELA=0;
58 TRISA4=0;
59 IRCF0=0; // p. 45
60 IRCF1=1;
61 IRCF2=1;
62 IRCF3=1;
63 SPLLEN=1; // p 46 and 54
64 }
65
66 /******************************************************************************/
67 // "configuration_bits.c" 12F1848
68 /******************************************************************************/
69 // CONFIG1
70 #pragma config FOSC = INTOSC // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
71 #pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled)
72 #pragma config PWRTE = ON // Power-up Timer Enable (PWRT enabled)
73 #pragma config MCLRE = OFF // MCLR Pin Function Select (MCLR/VPP pin function is digital input)
74 #pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled)
75 #pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled)
76 #pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled)
77 #pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
78 #pragma config IESO = OFF // Internal/External Switchover (Internal/External Switchover mode is disabled)
79 #pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled)
80
81 // CONFIG2
82 #pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)
83 #pragma config PLLEN = OFF // PLL Enable (4x PLL disabled)
84 #pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
85 #pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
86 #pragma config LVP = OFF // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)
87 /******************************************************************************/
88
89 /******************************************************************************/
90 // UART
91 /******************************************************************************/
92
93 // This demo module uses RS232 TX via EUSART, only
94
95 #define useEUSART 1
96 #define BAUD 19200
97
98
99 void
100 RS232init(void)
101 {
102 // Transmit
103 TXCKSEL = 1; // put TX on pin 4 - not 0 -, p 102
104 SPBRGL = (_XTAL_FREQ/BAUD/64-1);
105 SPBRGH = 0;
106 BRGH = 0;
107 BRG16 = 0;
108 // p 259 manual
109 SYNC = 0; // 0 p. 267
110 SPEN = 1; // 26.1.1.7
111 SCKP = 1; // invert p 269
112 TXEN = 1;
113 }
114
115 // EUSART transmit
116 void
117 putch(char c)
118 {
119 while (!TRMT) _delay(1);
120 TXREG=c;
121 }
122
123 /******************************************************************************/
124 // Timer and ISR
125 /******************************************************************************/
126
127 void
128 timer1_init(void)
129 {
130 // p 154
131 TMR1=0xFC00; // p. 155 wait 1024 cycles for stabilization.
132 TMR1CS1=0; // Clock source == System Clock
133 TMR1CS0=1;
134 TMR1IE=1; // enable TMR1 interrupts
135 PEIE=1; // enable Pheripheral Interrupts
136
137 }
138
139
140 /******************************************************************************/
141 // Interrupt handler
142 /******************************************************************************/
143
144 void interrupt isr(void)
145 {
146 irmp_ISR();
147 TMR1=0xffff-_XTAL_FREQ/F_INTERRUPTS;
148 TMR1IF=0; // clear timer 1 interrupt
149 }
150
151 /******************************************************************************/
152 // MAIN
153 /******************************************************************************/
154
155 int
156 main (void)
157 {
158 IRMP_DATA irmp_data;
159 InitApp(); // später inlinen
160
161 #if useEUSART
162 RS232init();
163 #endif
164 __delay_ms(200);
165 printf("IRMP PIC 12F1840 1.1 ws\r\n");
166 irmp_init(); // initialize irmp
167 timer1_init(); // initialize timer1
168 ei(); // enable interrupts
169 TMR1ON=1; // start timer
170
171 for (;;)
172 {
173 if (irmp_get_data (&irmp_data))
174 {
175 printf("P ");
176 printf("%d a=0x%04x c=0x%04x f=0x%02x (",irmp_data.protocol, irmp_data.address,irmp_data.command,irmp_data.flags);
177
178
179 #if IRMP_PROTOCOL_NAMES
180 printf(irmp_protocol_names[irmp_data.protocol]);
181 #else
182 switch(irmp_data.protocol)
183 {
184 case 1:
185 printf("Sony");
186 break;
187 case 2:
188 printf("NEC");
189 break;
190 case 7:
191 printf("RC5");
192 break;
193 case 0x21:
194 printf("Ortek");
195 break;
196 }
197 #endif
198 printf(")\r\n");
199 }
200 }
201 }