AN1606 Using the Configurable Logic Cell (CLC) to Interface a PIC16F1509 and WS2811 LED Driver Author: Driving an I/O pin in software could create this serial protocol, but this creates two problems: Joseph Julicher Microchip Technology Inc. 1. 100% of the CPU is used for the entire duration of the LED string update. (15.3 ms for 256 LEDs at the low rate). Very little time is allowed to decide the color for the next LED. INTRODUCTION 2. The Configurable Logic Cell (CLC) peripheral in the PIC16F1509 device is a powerful way to create custom interfaces that would otherwise be very difficult. One example is the single-wire PWM signal, used by the WS2811 LEDs, well known in LED video display systems. This application note will provide a simple demonstration of a WS2811 LED Strip driver. The ideal condition is to create a custom serial communications peripheral to transmit entire bytes in the correct format. For more information see the CLC chapter in the PIC16F1509 data sheet, “20-Pin Flash, 8-Bit Microcontrollers with nanoWatt XLP Technology” (DS41609). The serial protocol used by the WS2811 has three states. State 1 is a logical ‘0’, state 2 is a logical ‘1’ and state 3 is a latch. The data is accepted and used to drive the LED intensity. State 1 is a high output for 500 ns ± 150 ns, followed by 2000 ns ± 150 ns. State 2 is a high output for 120 ns ± 150 ns, followed by a low output for 130 ns ± 150 ns. State 3 is a low output for more than 50 us. There is a fast mode that reduces all of these times (except for state 3) by a factor of two, so that more LEDs can be driven in the same period of time. FIGURE 1: CUSTOM PERIPHERAL The CLC connects to a variety of peripherals in the PIC16F1509, including the MSSP. Because the MSSP can shift out eight bits at a time, this is the perfect starting point for this new peripheral. Looking at the MSSP output waveforms, Figure 1 illustrates the following: STATE 2, LOGIC ‘1’ GENERATION SCK SDO 1 0 0 1 1 0 0 0 SCK & SDO 2013 Microchip Technology Inc. DS00001606A-page 1 AN1606 If SCK and SDO are simply combined with AND, then the logic ‘1’ (state 2) is covered. In order to create the shorter logic ‘0’ (state 1) pulse, another signal is needed. If PWM is included as a short pulse generator, the following signals are obtained (see Figure 2). FIGURE 2: STATE 1, LOGIC ‘0’ GENERATION SCK 1 0 0 1 SDO 1 0 0 0 PWM SCK & nSDO & PWM When both logic ‘0’ and logic ‘1’ are obtained, they need to be ‘ORed’ together to produce the desired waveform (see Figure 3). FIGURE 3: LOGIC ‘0’ AND ‘1’ GENERATION SCK SDO 1 0 0 1 1 0 0 0 PWM SCK & SDO SCK & nSDO & PWM (SCK & nSDO & PWM) (SCK & SDO) Figure 3 appears to be the correct waveform, but there is still one issue to be resolved. How can the PWM and the MSSP be synchronized so that the rising edge of the PWM pulse matches the start of a data bit? It turns out the MSSP can be set to a baud rate of ½ the TMR2 period, and the PWM period can be one TMR2 period. DS00001606A-page 2 2013 Microchip Technology Inc. AN1606 Taking into consideration this new information, Figure 2 and Figure 3 above are incorrect. The new combined waveform can be seen in Figure 4: FIGURE 4: FINAL WAVEFORMS SCK SDO 1 0 0 1 1 0 0 0 PWM SCK & SDO SCK & nSDO & PWM (SCK & nSDO & PWM) (SCK & SDO) Note that the only difference is double the number of PWM pulses, but none of them has any effect because they occur while the SCK signal is low. 2013 Microchip Technology Inc. DS00001606A-page 3 AN1606 IMPLEMENTATION To create the final configuration, the layout of the CLC peripherals in PIC16F1509 has to be studied. TABLE 1: Data Input CLCx DATA INPUT SELECTION Icxd1 D1S Icxd2 Icxd3 Icxd4 D1S D1S D1S CLC 1 CLC 2 CLC 3 CLC 4 CLC4IN0 CLCxIN0 000 — — 100 CLC1IN0 CLC2IN0 CLC3IN0 CLCxIN1 001 — — 101 CLC1IN1 CLC2IN1 CLC3IN1 CLC4IN1 — 110 SYNCC1OUT SYNCC1OUT SYNCC1OUT SYNCC1OUT SYNCC2OUT SYNCC2OUT SYNCC2OUT SYNCC2OUT CLCxIN2 010 — CLCxIN3 011 — — 111 CLCxIN4 100 000 — — FOSC FOSC FOSC FOSC CLCxIN5 101 001 — — TMR0IF TMR0IF TMR0IF TMR0IF CLCxIN6 110 010 — — TMR1IF TMR1IF TMR1IF TMR1IF CLCxIN7 111 011 — — TMR2 = PR2 TMR2 = PR2 TMR2 = PR2 TMR2 = PR2 CLCxIN8 — 100 000 — Ic1_out Ic1_out Ic1_out Ic1_out CLCxIN9 — 101 001 — Ic2_out Ic2_out Ic2_out Ic2_out CLCxIN10 — 110 010 — Ic3_out Ic3_out Ic3_out Ic3_out CLCxIN11 — 111 011 — Ic4_out Ic4_out Ic4_out Ic4_out CLCxIN12 — — 100 000 NCO1OUT LFINTOSC TX (EUSART) SCK (MSSP) CLCxIN13 — — 101 001 HFINTOSC ADFRC LFINTOSC SDO (MSSP) CLCxIN14 — — 110 010 PWM3OUT PWM1OUT PWM2OUT PWM1OUT CLCxIN15 — — 111 011 PWM4OUT PWM2OUT PWM3OUT PWM4OUT The MSSP outputs are both available in CLC4. Note that PWM1 and PWM 4 are also on CLC4. However, they are all in the same logic group and only two of the four signals can be used in each group. The CLCs can be combined using LCx_OUT signals in a different logic group. The selection will be to combine PWM1OUT in CLC2 and send it to CLC4 via LC2_OUT. Therefore, the logic will be built in CLC4 as: LC4_OUT = (SDO & SCK) || (!SDO & SCK & LC2_OUT) and the logic in CLC2 will be: LC2_OUT = PWM1OUT The final configuration requires that the TMR2 be activated with the correct bit time: Configuring SSP1CON1 to use the TMR2 Period/2 as the clock results in: SSP1CON1 = 0x23. To allow the pin to output the signal, TRISC4 must be cleared to 0. CONCLUSION The CLC easily allows multiple peripherals in the PIC16F1509 to work together to produce a new composite peripheral that is suitable for the user’s application. With a dozen lines of code, the MSSP is now a custom WS2811 driver. Please refer to the Example A for more information. T2CON = 0x04 PR2 = (bit time/2) * (FOSC/4) If the slow bit time of 2.5 us and the 16 MHz internal oscillator are run, then: PR2 = 5. The PWM duty cycle must be configured for the ‘0’ pulse width. PWM1DCH = pr2/3. The slow bit rate requires that the value be 2 for 16 MHz. PWM1DCH = 2. DS00001606A-page 4 2013 Microchip Technology Inc. AN1606 APPENDIX A: EXAMPLE 1: SOURCE CODE void WS2811_Init(void) { // Initialize PIC16(L)F1509 CLC2, CLC4, Timer2, and MSSP // for WS2811 signal transmission protocol // Loading SSP1BUF register sends that data byte out the RC4/CLC4 pin // PWM1 routed straight through CLC2 CLC2GLS0 CLC2GLS1 CLC2GLS2 CLC2GLS3 CLC2SEL0 CLC2SEL1 CLC2POL CLC2CON // // = = = = = = = = 0x20; 0x00; 0x00; 0x00; 0x00; 0x06; 0x0E; 0x82; (SPI SDO & SPI CLK) || (nSPI SDO & SPI CLK & PWM1) PWM1 comes through CLC2 CLC4GLS0 CLC4GLS1 CLC4GLS2 CLC4GLS3 CLC4SEL0 CLC4SEL1 CLC4POL CLC4CON = = = = = = = = 0x44; 0x10; 0x80; 0x20; 0x50; 0x05; 0x01; 0xC0; // Adjust Timer2 period for desired baud rate // One bit period is two Timer2 periods T2CON PR2 = 0x04; = 5; // Adjust PWM1 duty cycle for desired "0" data-bit duty cycle // ‘1’ data-bit duty cycle is automatically 50% PWM1CON PWM1DCH PWM1DCL = 0xE0; = 2; = 0; // MSSP configured for SPI master with Timer2_Period/2 clock SSP1CON1 = 0x23; // Output on RC4/CLC4 TRISC &= 0xEF; } 2013 Microchip Technology Inc. DS00001606A-page 5 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: 9781620777671 QUALITY MANAGEMENT SYSTEM CERTIFIED BY DNV == ISO/TS 16949 == DS00001606A-page 6 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. 2013 Microchip Technology Inc. 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 Austria - Wels Tel: 43-7242-2244-39 Fax: 43-7242-2244-393 Denmark - Copenhagen Tel: 45-4450-2828 Fax: 45-4485-2829 Atlanta Duluth, GA Tel: 678-957-9614 Fax: 678-957-1455 Austin, TX Tel: 512-257-3370 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 Novi, MI Tel: 248-848-4000 Houston, TX Tel: 281-894-5983 Indianapolis Noblesville, IN Tel: 317-773-8323 Fax: 317-773-5453 Los Angeles Mission Viejo, CA Tel: 949-462-9523 Fax: 949-462-9608 New York, NY Tel: 631-435-6000 San Jose, CA Tel: 408-735-9110 Canada - Toronto Tel: 905-673-0699 Fax: 905-673-6509 2013 Microchip Technology Inc. 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 China - Hangzhou Tel: 86-571-2819-3187 Fax: 86-571-2819-3189 China - Hong Kong SAR Tel: 852-2943-5100 Fax: 852-2401-3431 China - Nanjing Tel: 86-25-8473-2460 Fax: 86-25-8473-2470 China - Qingdao Tel: 86-532-8502-7355 Fax: 86-532-8502-7205 China - Shanghai Tel: 86-21-5407-5533 Fax: 86-21-5407-5066 China - Shenyang Tel: 86-24-2334-2829 Fax: 86-24-2334-2393 China - Shenzhen Tel: 86-755-8864-2200 Fax: 86-755-8203-1760 China - Wuhan Tel: 86-27-5980-5300 Fax: 86-27-5980-5118 China - Xian Tel: 86-29-8833-7252 Fax: 86-29-8833-7256 India - New Delhi Tel: 91-11-4160-8631 Fax: 91-11-4160-8632 India - Pune Tel: 91-20-3019-1500 Japan - Osaka Tel: 81-6-6152-7160 Fax: 81-6-6152-9310 Japan - Tokyo Tel: 81-3-6880- 3770 Fax: 81-3-6880-3771 Korea - Daegu Tel: 82-53-744-4301 Fax: 82-53-744-4302 Korea - Seoul Tel: 82-2-554-7200 Fax: 82-2-558-5932 or 82-2-558-5934 France - Paris Tel: 33-1-69-53-63-20 Fax: 33-1-69-30-90-79 Germany - Dusseldorf Tel: 49-2129-3766400 Germany - Munich Tel: 49-89-627-144-0 Fax: 49-89-627-144-44 Germany - Pforzheim Tel: 49-7231-424750 Italy - Milan Tel: 39-0331-742611 Fax: 39-0331-466781 Italy - Venice Tel: 39-049-7625286 Malaysia - Kuala Lumpur Tel: 60-3-6201-9857 Fax: 60-3-6201-9859 Netherlands - Drunen Tel: 31-416-690399 Fax: 31-416-690340 Malaysia - Penang Tel: 60-4-227-8870 Fax: 60-4-227-4068 Poland - Warsaw Tel: 48-22-3325737 Philippines - Manila Tel: 63-2-634-9065 Fax: 63-2-634-9069 Singapore Tel: 65-6334-8870 Fax: 65-6334-8850 Taiwan - Hsin Chu Tel: 886-3-5778-366 Fax: 886-3-5770-955 Spain - Madrid Tel: 34-91-708-08-90 Fax: 34-91-708-08-91 Sweden - Stockholm Tel: 46-8-5090-4654 UK - Wokingham Tel: 44-118-921-5800 Fax: 44-118-921-5820 Taiwan - Kaohsiung Tel: 886-7-213-7830 Taiwan - Taipei Tel: 886-2-2508-8600 Fax: 886-2-2508-0102 Thailand - Bangkok Tel: 66-2-694-1351 Fax: 66-2-694-1350 China - Xiamen Tel: 86-592-2388138 Fax: 86-592-2388130 China - Zhuhai Tel: 86-756-3210040 Fax: 86-756-3210049 10/28/13 DS00001606A-page 7