2014年10月17日 星期五

TLC5940 PWM Driver

TLC5940 PWM Driver

Views: 7972 Difficulty: 3 Status: Complete
Screen_shot_2012-10-08_at_2.05.45_pm
 Think of it like a shift register for Pulse Width Modulation (PWM).
 The TLC5940 chip allows you to drive 16 Pulse Width Modulation PWM pins from 1 PWM and 4 control lines. Sort of like a serial-in, parallel out shift register, the TLC5940 gives 16 12-channel PWM outputs using only 5 lines on the microcontroller. We use these chips in our RGB flower necklace, to light up vibrant rainbow color around your sweetheart's clavicle. There is a great library to control these chips available for arduino. Another nice feature of these chips is they allow daisy-chaining. Want to see a tutorial about daisy-chaining TLC5940's? Let us know in the comments!

TLC5940 Nightlight 3

Tlc5940_nitelight3
 TLC5940 Nightlight 3

TLC5940 Driver and Module Video

 Video demonstrating 4 TLC5940 modules being driven by an Arduino clone. As the potentiometer spins the speed of the rainbow changes. The second part of the video shows the tree PCB with its 16 LEDs also powered by a TLC5940.

Code

 This code uses the TLC5940 arduino library available here. It also uses our arduino color library available here. The code outputs a rainbow to five RGB LEDs hooked up to the TLC5940.
/* LucidTronix HSB RGB necklace
 * uses TLC5940 chip. for details see:
 * http://www.lucidtronix.com/tutorials/6
 * Control rgb color by specifying
 * Hue saturation and brightness.
 * This code shows a rainbow.
 * It is okay to daiy-chain the chips
 * just change the NUM_TLCS constant
 * in the Tlc5940.h file.
 * This code uses an arduino color library
 * available here:
 * http://www.lucidtronix.com/tutorials/19
 */
#include "Tlc5940.h"
#include "tlc_fades.h"
#include "color.h"
// how many millis to strobe over all the LEDs

TLC_CHANNEL_TYPE channel;

Color cur_color(1,1,1);
int scope_period = (200 * NUM_TLCS);
int led_period = scope_period / (NUM_TLCS * 16);
float hue = 0;

void setup()
{
  Tlc.init();
}

void loop()
{
  hue += 0.001;
  if ( hue >=1 ) hue = 0;
  float sat = 1.0;
  float val = 0.5;
  cur_color.convert_hcl_to_rgb(hue,sat,val);

  
  uint32_t lastMillis = millis();
  int cur_val = channel % 3;
  int color_component = 0;
  if ( cur_val == 0) color_component = cur_color.red*16;
  else if  ( cur_val == 1) color_component = cur_color.green*16;
  else if ( cur_val == 2) color_component = cur_color.blue*16;
  tlc_addFade(channel,                      // led channel
              color_component,   // start fade value (0-4095)
              color_component,                            // end fade value (0-4095)
              lastMillis + 2,               // start millis
              lastMillis + (uint16_t)scope_period / 4  // end millis
             );
  if (channel++ == NUM_TLCS * 16) {
    channel = 0;
  }
  uint32_t currentMillis;
  do {
    currentMillis = millis();
    tlc_updateFades(currentMillis);
  } while (currentMillis - lastMillis <= led_period);
}

Parts

TitleDescription#CostLinkPicture
TLC5940IC LED DRIVER PWM CONTROL 32-QFN Value: 3 V ~ 5.5 V1$3.37LinkScreen_shot_2012-12-28_at_7.34.51_pm
ATMEGA168A-AUIC MCU AVR 16K FLASH 32TQFP Value: 1.8 V ~ 5.5 V 20MHz1$2.43LinkScreen_shot_2012-12-28_at_7.31.53_pm
PotentiometerPOT ROTARY, Linear 10K OHM 9MM SNAPIN Value: 10k1$0.76LinkScreen_shot_2012-12-28_at_7.41.04_pm
RGB LEDLED RGB SMD DIFF 4-PLCC Value: 1.9V Red, 3V Green, 3V Blue5$0.55LinkClvba-fka
SwitchSWITCH SLIDE DPDT 6VDC 0.3A SMT Value: DPDT1$0.44LinkMfg_js202011scqn
LED SMDLED 3X1.5MM 568NM Green Clear SMD Value: 2.2V Green16$0.18LinkApl3015sgc-f01
Button 6mm Through HoleTactile Switch Through Hole SPST-NO 0.05A 12V Value: 0.05A @ 12VDC2$0.1LinkScreen_shot_2012-12-28_at_7.41.40_pm
Permalink: http://lucidtronix.com/tutorials/4
Rainbow flower necklace using the TLC5940....
RGB Flashlight, wearable sound art, rainbow lamp....
Wearable digital compass displays the direction in green LEDs....
32 buttons for a fully-functioning hand-held USB keyboard....
Write messages on Liquid Crystal Displays with potentiometers or keyboards....
Control a 120v outlet with a 5V arduino....
Handheld gaming machine, based on the Arduino Leonardo, equipped with joystick, SD card and more....
An arduino library for the MMA8453 Triple Axis Accelerometer...

Shift registers & microchip PIC16F84

http://www.romux.com/tutorials/pic-tutorial/shift-registers

Shift registers

There are two types of shift registers: input and output. Input shift registers receive data in parallel, through 8 lines and then send it serially through two lines to a microcontroller. Output shift registers work in the opposite direction; they receive serial data and on a "latch" line signal, they turn it into parallel data. Shift registers are generally used to expand the number of input-output lines of a microcontroller. They are not so much in use any more though, because most modern microcontrollers have a large number of pins. However, their use with microcontrollers such as PIC16F84 is very important.
7.7.1 Input shift register 74HC597
Input shift registers transform parallel data into serial data and transfers it to a microcontroller. Their working is quite simple. There are four lines for the transfer of data: Clock, Latch, Load and Data. Data is first read from the input pins by an internal register through a 'latch' signal. Then, with a 'load' signal, data is transferred from the input latch register to the shift register, and from there it is serially transferred to a microcontroller via 'data' and 'clock' lines.
An outline of the connection of the shift register 74HC597 to a micro, is shown below.
In order to simplify the main program, a macro can be used for the input shift register. Macro HC597 has two parameters:

HC597 macro Var, Var1

Var variable where data from shift register input pins is transferred
Var1 loop counter

Example: HC597 data, counter

Data from the input pins of the shift register is stored in data variable. Timer/counter variable is used as a loop counter.

Macro listing:
/*  SOURCE : WWW.ROMUX.COM
    AUTHOR : romux team     */ 
    
    
HC597 MACRO    VAR,VARL                        
    LOCAL    LOOP                ;Local  label        
            MOVLW .8            ;Transfer  eight bits
            MOVWF     VARL         ;initializing  counter    
                                
            BSF LATCH            ;Receive  pin states  into  input latch    
            NOP 
            BCF    LATCH     
        
            BCF LOAD            ;Transfer  the  contents  of  input latch to 
            NOP                 ;SHIFT reg.    
            BSF    LOAD     
LOOP
            RLF    VAR,F            ;Rotate  "Var"  one place  to  the  left        
            BTFSS DATAA            ;Is  Data line  =  "1"   ?
            BCF VAR,0             ;If  not,   clear bit   '0'   in Var
            BTFSC DATAA         ;Is  Data line  =  "0"   ?
            BSF    VAR,0            ;If  not,   set bit   '0'    

            BSF CLOCK            ;Generate  one  clock    
            NOP 
            BCF    CLOCK     
    
            DECFSZ VARL,F        ;Has  8 bits been received  ?
            GOTO LOOP            ;If  not,   repeat    
            ENDM      
      
Example of how to use the HC597 macro is given in the following program. Program receives data from a parallel input of the shift register and moves it serially into the RX variable of the microcontroller. LEDs connected to port B will indicate the result of the data input.
/*  SOURCE : WWW.ROMUX.COM
    AUTHOR : romux team     */ 
    
    
PROCESSOR  16f84 
#include "P16F84.inc"
__CONFIG _CP_0FF  &  _WDT_0FF  &  _PWRTE_0N  &  _XT_0SC

            CBLOCK    OXOC          ;RAM  starting address
            RX
            COUNTSPI
            ENDC

#DEFINE DATAA  PORTA,0             ;Any other  I/O pin could be used
#DEFINE CLOCK PORTA, 1 
#DEFINE LATCH PORTA, 2 
#DEFINE LOAD  PORTA,3


            ORG        0X00            ;Reset vector
            GOTO     MAIN
            ORG        0X04            ;Interrupt vector
            GOTO     MAIN             ;no  interrupt  routine

            #include  "hc597.inc"
MAIN                                ;Beginning of   the program
            BANKSEL TRISA
            MOVLW   B'00010001'     ;initializing port A
            MOVWF   TRISA             ;TRISA <-  0x11
            CLRF     TRISB             ;Pins  of  porta B   are  output
            BANKSEL  PORTA

            CLRF     PORTA            ;PORTA <-  0x00 
            BSF        LOAD            ;Enable   SHIFT  register

LOOP     
            HC597   RX,   COUNTSPI    ;States   of  input pins   of  SHIFT reg.
            MOVF     RX,W            ;are  stored in variable RX
            MOVWF    P0RTB            ;Set the   content of RX  reg.   to port B
            GOTO     LOOP             ;Repeat  the  loop
            END                     ;End of program

      
      

7.7.2 Output shift register
Output shift registers transform serial data into parallel data. On every rising edge of the clock, the shift register reads the value from data line, stores it in temporary register, and then repeats this cycle 8 times. On a signal from 'latch' line, data is copied from the shift register to input register, thus data is transformed from serial into parallel data.
An outline of the 74HC595 shift register connections is shown on the diagram below:
Macro used in this example can be found in hc595.inc file, and is called HC595.

Macro HC595 has two parameters:

HC595 macro Var, Var1

Var variable whose contents is transferred to outputs of shift register.
Var1 loop counter

Example: HC595 Data, counter

The data we want to transfer is stored in data variable, and counter variable is used as a loop counter.
/*  SOURCE : WWW.ROMUX.COM
    AUTHOR : romux team     */ 
    
    
      

HC595 MACRO VAR,VARL                    
    LOCAL LOOP                    ;Local  label
            MOVLW .8             ;Transfer  eight bits
            MOVWF VARL            ;Initialising  counter
 

            RLF    VAR,F            ;Rotate "Var" on place to the  left        
            
            BTFSS STATUS,C         ;Is  carry =  "1"   ?
            BCF DATAA            ;If not,   set Dataa line   to  "0"
            BTFSC STATUS,C        ;Is  carry =  "0"   ? 
            BSF    DATAA            ;If not,   set Dataa line   to  "1"
                      
            BSF CLOCK            ;Generate  one   clock    
            NOP 
            BCF CLOCK    
     
            DECFSZ VARL,F        ;Has  eight bits been sent  ?
            GOTO LOOP              ;If not,   repeat
        
            BSF LATCH            ;If  all  8 bits have been sent,
            NOP                    ;contents  of  SHIFT register  to    send the output latch    
            BCF     LATCH
            ENDM 
                


Read more: http://www.romux.com/tutorials/pic-tutorial/shift-registers#ixzz3GO7ids91

  清除項目編譯快取 執行 flutter clean 重新取得套件依賴 執行 flutter pub get 完整重新編譯並執行 flutter run 1. 新增專案套件 (pubspec.yaml) 在 Flutter 專案根目錄下的 pubspec.yaml 中,新增 ...