+/*---------------------------------------------------------------------------------------------------------------------------------------------------\r
+ * main_pic12f1840.c - example main module for PIC 12f1840\r
+ * \r
+ * IR decoder using IRMP\r
+ *\r
+ * (c) 2014 Wolfgang Strobl (news4 at mystrobl.de) 2014-03-12:2014-07-20\r
+ *\r
+ * This demo module is runnable on a Microchip PIC 12F1840\r
+ *\r
+ * To be used with IRMP by Frank Meyer (frank(at)fli4l.de)\r
+ * <http://www.mikrocontroller.net/articles/IRMP>\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
+ * the Free Software Foundation; either version 2 of the License, or\r
+ * (at your option) any later version.\r
+ *---------------------------------------------------------------------------------------------------------------------------------------------------\r
+ */\r
+ \r
+ \r
+/* \r
+\r
+Hauptprogramm fuer den nachfolgenden Testaufbau, bestehend aus zwei mal \r
+\r
+\r
+ TSOP1736+ PIC12F1840\r
+ | __\r
+ 1k | Vdd--o| o|o--Vss\r
+ ___ +--GP5--o| |o--GP0 / ICSPDAT\r
+RS232 out -|___|-------GP4--o| |o--GP1 / ICSPCLK ___ LED\r
+RS232 in -|___|---Vpp/GP3--o|__|o--GP2-----------|___|- ->|---Vss\r
+ 10k\r
+ \r
+auf einem Steckbrett. (Genauer gesagt, dies ist die aktuelle Beschaltung\r
+fuer V1.8, V1.0 ist aber bzgl. IRMP-Empfang funktional identisch. Nicht eingezeichnet\r
+ist ein Abblockkondensator von 100nF ueber Vss und Vdd.\r
+\r
+Uebersetzt mit Microchip MPLAB XC8 C Compiler (Free Mode) V1.31 \r
+im stark gecrippelten "Free Mode".\r
+\r
+Memory Summary: (V 1.8)\r
+ Program space used C6Fh ( 3183) of 1000h words ( 77.7%)\r
+ Data space used 9Bh ( 155) of 100h bytes ( 60.5%)\r
+ EEPROM space used 0h ( 0) of 100h bytes ( 0.0%)\r
+ Data stack space used 0h ( 0) of 5Eh bytes ( 0.0%)\r
+ Configuration bits used 2h ( 2) of 2h words (100.0%)\r
+ ID Location space used 0h ( 0) of 4h bytes ( 0.0%)\r
+\r
+\r
+Testaufbau: \r
+\r
+Zwei Steckbretter,\r
+urspruengliche Version des Programms als Empfaenger, \r
+aktuelle Version als Sender, Aufzeichnung mit putty,\r
+angeschlossen jeweils per USB2RS232-Kabel von Conrad\r
+(972543, basierend auf Prolific PL2303). Soft-UART \r
+fuer Input, da 12F1820 keine Kontrolle ueber Input-\r
+Polaritaet erlaubt und ich fuer Testaufbauten eine\r
+Minimalbeschaltung bevorzuge.\r
+\r
+Kurze Distanz\r
+(~30 cm) zwischen Sender und Empfaenger), keine genaue Ausrichtung.\r
+Stromversorgung wahlwweise mit 5V via PICkit 2 oder 3x1.2V NiMH-AA.\r
+\r
+Zunaechst \r
+CD TAPE TUNER AUX OFF mit Philips FB, \r
+OFF mit VAOVA TV-2900HDD FB\r
+dann Eingabe . und n beim Sender.\r
+\r
+Sender:\r
+\r
+IRMP PIC 12F1840 1.8 ws\r
+P 7 a=0x0014 c=0x003f f=0x00 (RC5)\r
+P 7 a=0x0014 c=0x003f f=0x01 (RC5)\r
+P 7 a=0x0012 c=0x003f f=0x00 (RC5)\r
+P 7 a=0x0011 c=0x003f f=0x00 (RC5)\r
+P 7 a=0x0015 c=0x003f f=0x00 (RC5)\r
+P 7 a=0x0015 c=0x000c f=0x00 (RC5)\r
+P 2 a=0xbf00 c=0x0059 f=0x00 (NEC)\r
+P 2 a=0xbf00 c=0x0059 f=0x01 (NEC)\r
+. MX115OFF PR2 221\r
+n NEC PR2 209\r
+\r
+Empfaenger:\r
+\r
+IRMP PIC 12F1840 1.0 ws\r
+P 7 a=0x0014 c=0x003f f=0x00 (RC5)\r
+P 7 a=0x0014 c=0x003f f=0x01 (RC5)\r
+P 7 a=0x0012 c=0x003f f=0x00 (RC5)\r
+P 7 a=0x0011 c=0x003f f=0x00 (RC5)\r
+P 7 a=0x0015 c=0x000c f=0x00 (RC5)\r
+P 2 a=0xbf00 c=0x0059 f=0x00 (NEC)\r
+P 2 a=0xbf00 c=0x0059 f=0x01 (NEC)\r
+P 7 a=0x0015 c=0x000c f=0x00 (RC5)\r
+P 7 a=0x0015 c=0x000c f=0x01 (RC5)\r
+P 7 a=0x0015 c=0x000c f=0x01 (RC5)\r
+P 2 a=0x0055 c=0x00aa f=0x00 (NEC)\r
+\r
+Die via DSO an der LED gemessenen Frequenzen sind 36.0 resp. 38.0 kHz\r
+\r
+*/\r
+\r
+#include <stdio.h>\r
+\r
+#include "irmp.h"\r
+#include "irsnd.h"\r
+\r
+/******************************************************************************/\r
+// "system.h"\r
+/******************************************************************************/\r
+#define SYS_FREQ 32000000L\r
+#define _XTAL_FREQ 32000000 // for _delay\r
+#define FCY (SYS_FREQ/4)\r
+\r
+/******************************************************************************/\r
+// "user.c"\r
+/******************************************************************************/\r
+void InitApp(void)\r
+{\r
+ ANSELA=0;\r
+ TRISA4=0;\r
+ IRCF0=0; // p. 45\r
+ IRCF1=1;\r
+ IRCF2=1;\r
+ IRCF3=1;\r
+ SPLLEN=1; // p 46 and 54 \r
+}\r
+\r
+/******************************************************************************/\r
+// "configuration_bits.c" 12F1848\r
+/******************************************************************************/\r
+// CONFIG1\r
+#pragma config FOSC = INTOSC // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)\r
+#pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled)\r
+#pragma config PWRTE = ON // Power-up Timer Enable (PWRT enabled)\r
+#pragma config MCLRE = OFF // MCLR Pin Function Select (MCLR/VPP pin function is digital input)\r
+#pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled)\r
+#pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled)\r
+#pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled)\r
+#pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)\r
+#pragma config IESO = OFF // Internal/External Switchover (Internal/External Switchover mode is disabled)\r
+#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled)\r
+\r
+// CONFIG2\r
+#pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)\r
+#pragma config PLLEN = OFF // PLL Enable (4x PLL disabled)\r
+#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)\r
+#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)\r
+#pragma config LVP = OFF // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)\r
+/******************************************************************************/\r
+\r
+/******************************************************************************/\r
+// UART\r
+/******************************************************************************/\r
+\r
+#define GPIO3 RA3 \r
+#define GPIO4 RA4 \r
+\r
+#define SOFTUART_RXPIN GPIO3\r
+#define SOFTUART_STDIO 1\r
+#define SOFTUART_DI 1\r
+\r
+\r
+#define BAUD 19200 // 38200 ginge auch noch\r
+#define SOFTUART_BAUD BAUD\r
+#include "softuart_pic.h"\r
+\r
+#define kbhit softuartkbhit\r
+\r
+\r
+void \r
+RS232init(void)\r
+ {\r
+ // Transmit\r
+ TXCKSEL = 1; // put TX on pin 4 - not 0 -, p 102\r
+ SPBRGL = (_XTAL_FREQ/BAUD/64-1); \r
+ SPBRGH = 0;\r
+ BRGH = 0;\r
+ BRG16 = 0;\r
+ // p 259 manual\r
+ SYNC = 0; // 0 p. 267\r
+ SPEN = 1; // 26.1.1.7\r
+ SCKP = 1; // invert p 269\r
+ TXEN = 1;\r
+ }\r
+\r
+ // EUSART transmit\r
+ void \r
+ putch(char c)\r
+ {\r
+ while (!TRMT) _delay(1);\r
+ TXREG=c;\r
+ }\r
+ \r
+/******************************************************************************/\r
+// Timer and ISR\r
+/******************************************************************************/\r
+ \r
+void \r
+timer1_init(void)\r
+{\r
+ // p 154\r
+ TMR1=0xFC00; // p. 155 wait 1024 cycles for stabilization.\r
+ TMR1CS1=0; // Clock source == System Clock\r
+ TMR1CS0=1; \r
+ TMR1IE=1; // enable TMR1 interrupts\r
+ PEIE=1; // enable Pheripheral Interrupts\r
+ TMR1IF=0;\r
+ TMR1ON=1;\r
+}\r
+\r
+\r
+/******************************************************************************/\r
+// Interrupt handler\r
+/******************************************************************************/\r
+\r
+void interrupt isr(void)\r
+{\r
+ TMR1=0xffff-_XTAL_FREQ/F_INTERRUPTS; \r
+ TMR1IF=0; // clear timer 1 interrupt\r
+ \r
+ if (!irsnd_ISR())\r
+ {\r
+ irmp_ISR();\r
+ }\r
+}\r
+\r
+\r
+IRMP_DATA irmp_data;\r
+\r
+void RC5(uint16_t addr,uint16_t cmd, uint8_t repetitions)\r
+{\r
+ irmp_data.protocol = IRMP_RC5_PROTOCOL; \r
+ irmp_data.address = addr;\r
+ irmp_data.command = cmd;\r
+ irmp_data.flags = repetitions;\r
+ irsnd_send_data (&irmp_data, FALSE); \r
+}\r
+\r
+void NEC(int addr,int cmd)\r
+{\r
+ irmp_data.protocol = IRMP_NEC_PROTOCOL; \r
+ irmp_data.address = addr;\r
+ irmp_data.command = cmd;\r
+ irmp_data.flags = 0;\r
+ irsnd_send_data (&irmp_data, FALSE); \r
+}\r
+\r
+\r
+/******************************************************************************/\r
+// MAIN\r
+/******************************************************************************/\r
+\r
+int\r
+main (void)\r
+{\r
+ IRMP_DATA irmp_data;\r
+ char c;\r
+ InitApp(); \r
+\r
+ PWMoff();\r
+ RS232init();\r
+ \r
+ __delay_ms(200);\r
+ printf("IRMP PIC 12F1840 1.8 ws\r\n");\r
+ irmp_init(); // initialize irmp\r
+ timer1_init(); // initialize timer1\r
+ ei(); // enable interrupts\r
+ TMR1ON=1; // start timer\r
+ \r
+ for (;;)\r
+ {\r
+ if (kbhit())\r
+ {\r
+ c=getch();\r
+ if (c>32 && c<127) putch(c);\r
+ putch(' ');\r
+ if (c=='.')\r
+ {\r
+ printf("MX115OFF ");\r
+ RC5(0x15,0x0c,2); // Philips MC115 AUX OFF\r
+ }\r
+ else if (c=='n')\r
+ {\r
+ printf("NEC ");\r
+ NEC(0x55,0xaa); \r
+ }\r
+ else\r
+ {\r
+ putch('?');\r
+ continue;\r
+ }\r
+ while (irsnd_is_busy ()) ;\r
+ printf("PR2 %d\r\n",PR2);\r
+ continue;\r
+ }\r
+ if (irmp_get_data (&irmp_data))\r
+ {\r
+ printf("P ");\r
+ printf("%d a=0x%04x c=0x%04x f=0x%02x (",irmp_data.protocol, irmp_data.address,irmp_data.command,irmp_data.flags); \r
+ \r
+#if IRMP_PROTOCOL_NAMES\r
+ printf(irmp_protocol_names[irmp_data.protocol]);\r
+#else \r
+ switch(irmp_data.protocol)\r
+ {\r
+ case 1:\r
+ printf("Sony");\r
+ break;\r
+ case 2:\r
+ printf("NEC");\r
+ break;\r
+ case 7:\r
+ printf("RC5");\r
+ break;\r
+ case 0x21:\r
+ printf("Ortek");\r
+ break;\r
+ }\r
+#endif\r
+ printf(")\r\n");\r
+ }\r
+ }\r
+}\r