TB3096 Pulse Code Modulated (PCM) Infrared Remote Control Using PIC16F1708 Author: Stephen Allen Microchip Technology Inc. INTRODUCTION This technical brief shows how to construct a simple, low-power IR (infrared) remote control transmitter using a PIC16F1708 microcontroller. The remote control transmitter makes use of the Configurable Logic Cell (CLC) and Pulse-Width Modulation (PWM) to create a Pulse Code Modulation (PCM) signal that will be transmitted via an infrared LED. The Peripheral Pin Select (PPS) feature is used to route the PCM signal to an infrared LED. PCM involves transmission of a carrier frequency that can be easily discerned from the background noise. This signal is then band-pass filtered and demodulated by the receiver to recreate the digital waveform. Television remote controls use different frequencies, but 30 kHz and 38 kHz are very popular. This project can be easily modified to output different carrier frequencies. A number of IR receivers are available in the marketplace. This project was tested using a Vishay Dale TWOP75230W receiver. This receiver is optimized for receiving a 30 kHz carrier, and also filters out many types of background noise. This technical brief will also demonstrate how the CLC module can be dynamically updated during operation. Updating the CLC during operation avoids external routing of signals, thereby reducing pin count. The pin count reduction may allow transitioning to lower-cost packages. An intuitive way to construct a PCM signal would be the following (see Figure 1): FIGURE 1: INTUITIVE CONSTRUCTION OF PCM REMOTE This will work, but it uses three pins. A simpler configuration can be constructed where the other input to the ‘AND’ gate is controlled from within the CLC module itself (see Figure 2). FIGURE 2: SIMPLIFIED CONSTRUCTION OF PCM REMOTE With the limited number of components, this project can easily be constructed on a breadboard. The schematic is included in Appendix A: “Schematic”. The project has been written in assembly, and can be easily ported to other PIC® microcontrollers which contain the CLC. Full source code is included in “Appendix B”. The project uses 124 locations of program memory and five bytes of RAM, leaving lots of room for customization. Power consumption has been minimized by keeping the microcontroller in a Sleep state when not in use. A button press wakes the device from Sleep, message transmission occurs and the device goes back to its Sleep state when transmission is completed. Very low Sleep current (in the nA range) serves to extend battery life. 2013 Microchip Technology Inc. This has the advantage of using two less I/O pins and simplifies the design. DS90003096A-page 1 TB3096 The PWM module will be used to create the 30 kHz signal, and this will then be gated (enabled or disabled) through an ‘AND’ gate in the CLC module. The modulation will be controlled directly by using one of the polarity bits in the CLC module. Using the PWM allows flexibility for creating different frequencies, while controlling the signal from within the CLC module allows for a variety of signal formats, including number of bits, parity, checksum, etc. FIGURE 3: The initial configuration of the CLC module is done using the CLC Designer Tool. We initially want the output signal “off”, so we design it such that GATE 4 output will control whether or not the PWM3OUT signal is present at the output pin. This polarity is switched through the CLC1POL<3> bit. This will be the initialized state of the CLC module, where the output signal is turned off (always 0) (see Figure 3). CLC1 MODULE CONFIGURED TO OUTPUT ‘0’ DS90003096A-page 2 2013 Microchip Technology Inc. TB3096 When the output of GATE 4 is inverted, we get the PWM3OUT signal coming out of the CLC module (see Figure 4): FIGURE 4: CLC1 MODULE CONFIGURED TO OUTPUT PWM3OUT SIGNAL With the CLC module configured, we also need to configure our PWM to output a 30 kHz waveform. PERIPHERAL PIN SELECT (PPS) SETTINGS EQUATIONS: Peripheral Pin Select (PPS) is a feature which allows digital peripheral input/output signals to be mapped to physical pins. The photo-diode (and associated resistor) are connected to the RC2 pin (see Figure 5). In order to connect the CLC1 output to the RC2 pin, we need to write a value of 0x04 to the RC2PPS register. For 30 kHz, we want a period of: TOSC = 1/FOSC = 1/30000 = 33.3 s PWM period is calculated from the following equation: PWM Period = PR2 + 1 4 Tosc TMR2 prescale Solving for PR2: FIGURE 5: SCHEMATIC SHOWING PHOTODIODE CONNECTED TO RC2 PIN PR2 = PWM Period 4 Tosc TMR2 prescale – 1 For TOSC = 62.5 ns (16 MHz oscillator clock) and TMR2 prescale = 1 (no prescale): PR2 = 33.3 s 4 62.5 ns 1 – 1 = 132 For a 50% duty cycle, we want to set our PWM duty cycle to half this value: (132/2) = 66 2013 Microchip Technology Inc. RC1 15 RC2 14 RB4 13 RB5 12 R3 100R D1 + + IR958-8C DS90003096A-page 3 TB3096 PUSH-BUTTON OPERATION DATA RECEPTION/DEMODULATION This simple example uses one push button, but could easily be extended to add more buttons. The single push button that causes transmission to occur is connected to the RB7 pin. In order to minimize external components, we are using the internal pull-up resistor to pull the pin high. Pressing the button (tied to GND) pulls the signal down and causes the device to wake from Sleep. The scope plot below shows the PCM data signal (yellow). Transmitted and received data are also shown (green and purple, respectively). It should be noted that the received data is inverted. Feeding the OUT (TSOP75230W) signal into a microcontroller and sampling in the middle of the bit time easily recreates the transmitted data (see Figure 8). FIGURE 8: PULSE CODE MODULATED (PCM) SIGNAL OUTPUT PCM TRANSMITTED AND RECEIVED WAVEFORMS With the CLC and PWM module now properly configured, we see that we get the PWM output when the control signal is high, and we get no output when the control signal is low. The top signal (green) is the same signal as CLC1POL<3>, but is replicated on a pin for visualization. It should be noted that a Scope Trigger signal is available on the RC6 pin (see Figure 6). FIGURE 6: PCM OUTPUT SIGNAL Zooming in on the start of the transmission, we can see how long it takes for the receiver to demodulate the signal. The receiver takes about eight cycles of the 30 kHz carrier before it transitions (see Figure 9). FIGURE 9: WAVEFORM SHOWING TIME FOR RECEIVED SIGNAL TO TRANSITION A closer view verifies that the PWM is generating a 30 kHz waveform (see Figure 7): FIGURE 7: CLOSE-UP OF PCM SIGNAL TO VERIFY 30 kHz OPERATION DS90003096A-page 4 2013 Microchip Technology Inc. TB3096 Capturing the same signal again, but with infinite persistence on the scope, we can see that the receiver chip operates very consistently and causes the signal transition to take place after about eight cycles (@ 30 kHz) (see Figure 10). FIGURE 11: REMOTE CONTROL TRANSMITTER BASIC OPERATION START FIGURE 10: INFINITE PERSISTENCE SHOWING STABLE TRANSITIONS Initialize Peripherals Enter low-power Sleep state Wait for button press LOW-POWER SLEEP STATE Wake-up and transmit data In order to reduce current consumption, we will have the device remain in Sleep mode, wake-up when the button is pressed, transmit data, and then return to the Sleep state. The flowchart below (see Figure 11) shows the basic operation of the remote control transmitter. 2013 Microchip Technology Inc. DS90003096A-page 5 TB3096 CONCLUSION This tech brief has demonstrated how to configure the CLC to work with the PWM and act as a PCM transmitter. The CLC module is used with no external pins required for signal routing. This project can be used as a starting point for low-power remote control transmitters. DS90003096A-page 6 2013 Microchip Technology Inc. 2013 Microchip Technology Inc. GND +3V BT1 S1 R1 100K +3V C1 1uF R4 10K +3V RC3 RC3 VPP +3V 10 9 8 7 6 5 4 3 2 1 RB6 RB5 RB4 RC2 RC1 RC0 RA2 RA1 RA0 Vss TRANSMITTER PIC16F1708 RB7 RC7 RC6 RC3 RC4 RC5 RA3 RA4 RA5 Vdd U1 20 11 12 13 14 15 16 17 18 19 100R R3 ICSPCLK ICSPDAT 1 2 3 4 5 6 IR958-8C D1 ICSPDAT ICSPCLK VPP DNP PICkit™ 2 VPP/MCLR VDD GND ICSPDAT ICSPCLK NC J1 TSOP75230W GND GND VS OUT 3 R2 C2 200R 1uF GND TP1 IR RECEIVER 4 1 2 U2 VDD APPENDIX A: GND +3V +3V TB3096 SCHEMATIC DS90003096A-page 7 TB3096 Software License Agreement The software supplied herewith by Microchip Technology Incorporated (the “Company”) is intended and supplied to you, the Company’s customer, for use solely and exclusively with products manufactured by the Company. The software is owned by the Company and/or its supplier, and is protected under applicable copyright laws. All rights are reserved. Any use in violation of the foregoing restrictions may subject the user to criminal sanctions under applicable laws, as well as to civil liability for the breach of the terms and conditions of this license. THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. APPENDIX B: #include "p16f1708.inc" #define TX_DATA0x5A ; this is the date that will be transmitted - example. tx_register tx_counter equ equ 0x70 0x71 ; RAM location for transmit data (Common RAM) ; counts bits in the transmission baud_counta baud_countb deb_count equ equ equ 0x72 0x73 0x74 ; counter for baud rate delay ; counter for debounce start org 0x0000 nop banksel clrf clrf clrf ANSELB ANSELB ANSELA ANSELC banksel clrf movlw movwf clrf TRISC TRISC 0x80 TRISB TRISA #include “pwm3and.inc” DS90003096A-page 8 ; make port B digital ; make port B digital ; make port C digital ; port C all outputs ; port B all outputs, except RB7 ; port A all outputs ; load Configurable Logic Cell settings. ; these settings allow PWM3 OUT to pass through, ; or pin outputs ‘0’. banksel movlw movwf RC2PSS 0x04 RC2PSS ; 0x04 ; selects CLC1 output for RC2 pin. banksel movlw movwf VREGCON 0x03 VREGCON ; low power mode for Sleep banksel movlw movwf banksel bcf banksel bsf bcf banksel movlw movwf WPUB 0x80 WPUB OPTION_REG OPTION_REG, 7 IOCBN IOCBN, 7 IOCBF, 7 OSCCON 0x78 OSCCON ; turn on weak pull-up on RB7 ; enable weak pull-ups ; enable interrupt-on-change (falling edge) on RB7 ; clear interrupt-on-change flag. ; 16 MHz oscillator 2013 Microchip Technology Inc. TB3096 banksel movlw movwf PR2 .132 PR2 banksel movlw movwf clrf banksel clrf bsf banksel bsf banksel bcf bsf PWM3DCH .66 PWM3DCH PWM3DCL T2CON T2CON T2CON, TMR2ON PWM3CON PWM3CON, 7 INTCON INTCON, IOCIF INTCON, IOCIE ; set up period of 30 kHz ; for Timer2. ; 50% duty cycle ; for PWM3. ; 1:1 prescaler for Timer2. ; turn on Timer2. ; turn on PWM3 ; make sure interrupt flag is clear ; and then enable the interrupt main_loop sleep nop nop nop ; go to sleep - low current mode. ; wake-up occurs here. ; does not go to interrupt vector because ; GIE is not enabled debounce movlw movwf 0xff deb_count call btfss goto decfsz goto nop nop movlw movwf call banksel bcf banksel bcf goto bit_delay PORTB, 7 debounce deb_count deb_a banksel bsf bcf movlw movwf LATC LATC, 6 LATC, 6 0x08 tx_counter banksel btfss goto goto CLC1POL tx_register, 7 transmit_zero transmit_one rlf decfsz goto bcf return tx_register, F tx_counter, F tx_a CLC1POL, 3 ; initialize debounce counter. deb_a ; ; ; ; ; TX_DATA tx_register transmit IOCBF IOCBF, 7 INTCON INTCON, IOCIF main_loop Has button been released? No. Yes. Has it been released for a while? No. interrupt will cause wake-up. ; transmit data ; moved to transmit register. ; clear interrupt-on-change flag. ; clear interrupt flag. transmit ; scope trigger tx_a next_bit 2013 Microchip Technology Inc. ; ; ; ; rotate left to get next bit. decrement bit counter. Am I done? No. Yes. Drop signal low at end of transmission. DS90003096A-page 9 TB3096 transmit_zero bcf call goto CLC1POL, 3 bit_delay next_bit ; CLC output = 0 bsf call goto CLC1POL, 3 bit_delay next_bit ; CLC outputs PWM3 OUT clrf clrf baud_counta baud_countb incf btfss goto incf movlw subwf btfss goto return baud_counta, F STATUS, Z bit_delay_loop baud_countb, F 0x08 baud_countb, W STATUS, Z bit_delay_loop transmit_one bit_delay ; clear counter bit_delay_loop ; did I roll over? ; No ; Yes. Increment higher byte ; Am I at end of bit time? ; No. ; Yes. end DS90003096A-page 10 2013 Microchip Technology Inc. TB3096 APPENDIX C: ; PPS Initialization BANKSEL CLCIN0PPS movlw H’00’ movwf CLCIN0PPS movlw H’00’ movwf CLCIN1PPS movlw H’00’ movwf CLCIN2PPS movlw H’00’ movwf CLCIN3PPS BANKSEL CLC1GLS0 movlw H’02’ movwf CLC1GLS0 movlw H’00’ movwf CLC1GLS1 movlw H’00’ movwf CLC1GLS2 movlw H’00’ movwf CLC1GLS3 movlw H’0E’ movwf CLC1SEL0 movlw H’00’ movwf CLC1SEL1 movlw H’00’ movwf CLC1SEL2 movlw H’00’ movwf CLC1SEL3 movlw H’06’ movwf CLC1POL movlw H’82’ movwf CLC1CON 2013 Microchip Technology Inc. DS90003096A-page 11 TB3096 NOTES: DS90003096A-page 12 2013 Microchip Technology Inc. Note the following details of the code protection feature on Microchip devices: • Microchip products meet the specification contained in their particular Microchip Data Sheet. • Microchip believes that its family of products is one of the most secure families of its kind on the market today, when used in the intended manner and under normal conditions. • There are dishonest and possibly illegal methods used to breach the code protection feature. All of these methods, to our knowledge, require using the Microchip products in a manner outside the operating specifications contained in Microchip’s Data Sheets. Most likely, the person doing so is engaged in theft of intellectual property. • Microchip is willing to work with the customer who is concerned about the integrity of their code. • Neither Microchip nor any other semiconductor manufacturer can guarantee the security of their code. Code protection does not mean that we are guaranteeing the product as “unbreakable.” Code protection is constantly evolving. We at Microchip are committed to continuously improving the code protection features of our products. Attempts to break Microchip’s code protection feature may be a violation of the Digital Millennium Copyright Act. If such acts allow unauthorized access to your software or other copyrighted work, you may have a right to sue for relief under that Act. Information contained in this publication regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer’s risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights. Trademarks The Microchip name and logo, the Microchip logo, dsPIC, FlashFlex, KEELOQ, KEELOQ logo, MPLAB, PIC, PICmicro, PICSTART, PIC32 logo, rfPIC, SST, SST Logo, SuperFlash and UNI/O are registered trademarks of Microchip Technology Incorporated in the U.S.A. and other countries. FilterLab, Hampshire, HI-TECH C, Linear Active Thermistor, MTP, SEEVAL and The Embedded Control Solutions Company are registered trademarks of Microchip Technology Incorporated in the U.S.A. Silicon Storage Technology is a registered trademark of Microchip Technology Inc. in other countries. Analog-for-the-Digital Age, Application Maestro, BodyCom, chipKIT, chipKIT logo, CodeGuard, dsPICDEM, dsPICDEM.net, dsPICworks, dsSPEAK, ECAN, ECONOMONITOR, FanSense, HI-TIDE, In-Circuit Serial Programming, ICSP, Mindi, MiWi, MPASM, MPF, MPLAB Certified logo, MPLIB, MPLINK, mTouch, Omniscient Code Generation, PICC, PICC-18, PICDEM, PICDEM.net, PICkit, PICtail, REAL ICE, rfLAB, Select Mode, SQI, Serial Quad I/O, Total Endurance, TSHARC, UniWinDriver, WiperLock, ZENA and Z-Scale are trademarks of Microchip Technology Incorporated in the U.S.A. and other countries. SQTP is a service mark of Microchip Technology Incorporated in the U.S.A. GestIC and ULPP are registered trademarks of Microchip Technology Germany II GmbH & Co. KG, a subsidiary of Microchip Technology Inc., in other countries. All other trademarks mentioned herein are property of their respective companies. © 2013, Microchip Technology Incorporated, Printed in the U.S.A., All Rights Reserved. Printed on recycled paper. ISBN: 9781620773192 QUALITY MANAGEMENT SYSTEM CERTIFIED BY DNV == ISO/TS 16949 == 2013 Microchip Technology Inc. Microchip received ISO/TS-16949:2009 certification for its worldwide headquarters, design and wafer fabrication facilities in Chandler and Tempe, Arizona; Gresham, Oregon and design centers in California and India. The Company’s quality system processes and procedures are for its PIC® MCUs and dsPIC® DSCs, KEELOQ® code hopping devices, Serial EEPROMs, microperipherals, nonvolatile memory and analog products. In addition, Microchip’s quality system for the design and manufacture of development systems is ISO 9001:2000 certified. DS90003096A-page 13 Worldwide Sales and Service AMERICAS ASIA/PACIFIC ASIA/PACIFIC EUROPE Corporate Office 2355 West Chandler Blvd. Chandler, AZ 85224-6199 Tel: 480-792-7200 Fax: 480-792-7277 Technical Support: http://www.microchip.com/ support Web Address: www.microchip.com Asia Pacific Office Suites 3707-14, 37th Floor Tower 6, The Gateway Harbour City, Kowloon Hong Kong Tel: 852-2401-1200 Fax: 852-2401-3431 India - Bangalore Tel: 91-80-3090-4444 Fax: 91-80-3090-4123 India - New Delhi Tel: 91-11-4160-8631 Fax: 91-11-4160-8632 Austria - Wels Tel: 43-7242-2244-39 Fax: 43-7242-2244-393 Denmark - Copenhagen Tel: 45-4450-2828 Fax: 45-4485-2829 India - Pune Tel: 91-20-2566-1512 Fax: 91-20-2566-1513 France - Paris Tel: 33-1-69-53-63-20 Fax: 33-1-69-30-90-79 Japan - Osaka Tel: 81-6-6152-7160 Fax: 81-6-6152-9310 Germany - Munich Tel: 49-89-627-144-0 Fax: 49-89-627-144-44 Atlanta Duluth, GA Tel: 678-957-9614 Fax: 678-957-1455 Boston Westborough, MA Tel: 774-760-0087 Fax: 774-760-0088 Chicago Itasca, IL Tel: 630-285-0071 Fax: 630-285-0075 Cleveland Independence, OH Tel: 216-447-0464 Fax: 216-447-0643 Dallas Addison, TX Tel: 972-818-7423 Fax: 972-818-2924 Detroit Farmington Hills, MI Tel: 248-538-2250 Fax: 248-538-2260 Indianapolis Noblesville, IN Tel: 317-773-8323 Fax: 317-773-5453 Los Angeles Mission Viejo, CA Tel: 949-462-9523 Fax: 949-462-9608 Santa Clara Santa Clara, CA Tel: 408-961-6444 Fax: 408-961-6445 Toronto Mississauga, Ontario, Canada Tel: 905-673-0699 Fax: 905-673-6509 Australia - Sydney Tel: 61-2-9868-6733 Fax: 61-2-9868-6755 China - Beijing Tel: 86-10-8569-7000 Fax: 86-10-8528-2104 China - Chengdu Tel: 86-28-8665-5511 Fax: 86-28-8665-7889 China - Chongqing Tel: 86-23-8980-9588 Fax: 86-23-8980-9500 Korea - Daegu Tel: 82-53-744-4301 Fax: 82-53-744-4302 China - Hangzhou Tel: 86-571-2819-3187 Fax: 86-571-2819-3189 Korea - Seoul Tel: 82-2-554-7200 Fax: 82-2-558-5932 or 82-2-558-5934 China - Hong Kong SAR Tel: 852-2943-5100 Fax: 852-2401-3431 Malaysia - Kuala Lumpur Tel: 60-3-6201-9857 Fax: 60-3-6201-9859 China - Nanjing Tel: 86-25-8473-2460 Fax: 86-25-8473-2470 Malaysia - Penang Tel: 60-4-227-8870 Fax: 60-4-227-4068 China - Qingdao Tel: 86-532-8502-7355 Fax: 86-532-8502-7205 Philippines - Manila Tel: 63-2-634-9065 Fax: 63-2-634-9069 China - Shanghai Tel: 86-21-5407-5533 Fax: 86-21-5407-5066 Singapore Tel: 65-6334-8870 Fax: 65-6334-8850 China - Shenyang Tel: 86-24-2334-2829 Fax: 86-24-2334-2393 Taiwan - Hsin Chu Tel: 886-3-5778-366 Fax: 886-3-5770-955 China - Shenzhen Tel: 86-755-8864-2200 Fax: 86-755-8203-1760 Taiwan - Kaohsiung Tel: 886-7-213-7828 Fax: 886-7-330-9305 China - Wuhan Tel: 86-27-5980-5300 Fax: 86-27-5980-5118 Taiwan - Taipei Tel: 886-2-2508-8600 Fax: 886-2-2508-0102 China - Xian Tel: 86-29-8833-7252 Fax: 86-29-8833-7256 Thailand - Bangkok Tel: 66-2-694-1351 Fax: 66-2-694-1350 Italy - Milan Tel: 39-0331-742611 Fax: 39-0331-466781 Netherlands - Drunen Tel: 31-416-690399 Fax: 31-416-690340 Spain - Madrid Tel: 34-91-708-08-90 Fax: 34-91-708-08-91 UK - Wokingham Tel: 44-118-921-5869 Fax: 44-118-921-5820 China - Xiamen Tel: 86-592-2388138 Fax: 86-592-2388130 China - Zhuhai Tel: 86-756-3210040 Fax: 86-756-3210049 DS90003096A-page 14 Japan - Tokyo Tel: 81-3-6880- 3770 Fax: 81-3-6880-3771 11/29/12 2013 Microchip Technology Inc.