Single PIC to Blink 5 LEDs Independently?


Closed Thread
Results 1 to 40 of 69

Hybrid View

  1. #1


    Did you find this post helpful? Yes | No

    Default Re: Single PIC to Blink 5 LEDs Independently?

    Quote Originally Posted by wdmagic View Post
    you can do that easily if you want them all to dim at the same rate using the PICs PWM going into the gate of a seperate FET with its drain tied to all the fets, sources. and its source to ground, this adds a ~.5v voltage drop but you can compensate by adjusting your pots.

    I did this on a guys truck, he wanted 2 sets of floorboard lights, operated from a single pushbutton, but he also wanted to control their brightness, it was easy enought to set a 2 bit binary output to 2 FETS, and have them tied to a 3rd FET that was drivin by the PWM pin. and I just used a POT to set the PWM, and a button to select the lights (left, right, both, off).
    But did the LEDs blink? It sounds like your LEDs were steady-on with brightness control. For my application, I need to control the brightness of blinking LEDs. You can see the full code file above (created by Darrel) but I think the relevant portion is:

    Code:
    CCPR1val      CON EXT      : @CCPR1val = (OSC*1000000/4)/ BLINKYFREQ
    ;CCPR1         VAR WORD EXT : @CCPR1 = CCPR1L
    Timer1        VAR WORD EXT : @Timer1 = TMR1L
    CCPIF         VAR PIR1.2
    
    ;----[Initialize on/off periods & random sequencing (if enabled)]---------------
    ;    (only randomize first 5 blinkies; 6th is strobe which must remaing constant)
    ;    (running lights (7th) are set up separately)
    FOR x = 0 to (LEDcount - 2)           ; Load the periods from EEPROM
        READ OnTimes+x, OnTime(x)
        READ OffTimes+x, OffTime(x)
        #IFDEF USE_RANDOM_SEQUENCE
            IF x < 5 THEN
                READ RandPeriods+(x<<1), WORD RandPeriod(x)
            ENDIF
        #ENDIF
    NEXT X
    
    ;-- setup CCP1 and Start Timer1 --
    CCPR1   = CCPR1val          ; set compare value
    CCP1CON = %00001011         ; compare mode, special event 
    Timer1  = 0                 ; clear Timer1
    T1CON.0 = 1                 ; start Timer1
    
    ;----[Main Program Loop]----------------------------------------
    Main: 
        ' Check if flash mode has changed
        IF FlashMode <> Old_FlashMode Then
            Old_FlashMode = FlashMode
            GOSUB SetNavLtsFlashRates
        EndIF 
    
        x = (x + 1) // LEDcount
        PORTB.1(x) = !!(LoopLED(x) < OnTime(x))
        LoopLED(x) = (LoopLED(x) + 1) // (OnTime(x) + OffTime(x))
        #IFDEF USE_RANDOM_SEQUENCE
            IF x < 5 THEN
                RandPeriod(x) = RandPeriod(x) - 1
                IF RandPeriod(x) = 0 THEN
                    READ RandPeriods+(x<<1), WORD RandPeriod(x)
                    RANDOM RND
                    OnTime(x) = (MAX_ON - MIN_ON)* RND.HighByte / 255 + MIN_ON 
                    OffTime(x)= (MAX_OFF - MIN_OFF)* RND.LowByte / 255 + MIN_OFF
                ENDIF
            ENDIF
        #ENDIF
        IF x != (LEDcount - 1) THEN Main
    
    Waiting: IF !CCPIF THEN Waiting
        CCPIF = 0
    GOTO Main

  2. #2


    Did you find this post helpful? Yes | No

    Default Re: Single PIC to Blink 5 LEDs Independently?

    (Reviving an old thread)

    I now need to simplify this a bit and put this on a 8-pin PIC. I need 5 blinkies so I'd like to use a 12F629 (knowing GP3/MCLR is input only, I'll use GP0-GP5 and if nothing happens on GP3 then that's fine).

    The code won't compile because of this part:

    Code:
    ;-- setup CCP1 and Start Timer1 --
    CCPR1   = CCPR1val          ; set compare value
    CCP1CON = %00001011         ; compare mode, special event 
    Timer1  = 0                 ; clear Timer1
    T1CON.0 = 1                 ; start Timer1
    These registers exist on a 16F690 (and others) but not 12F629. I had earlier tried a 12F683 (my go-to PIC) but the .inc file for it already has a define for CCPR1 so I'd have to comment out that line in the .inc file but that makes me a wee bit uncomfortable.

    Is there a way to make this work on a 12F690 or 12F683? If not, is there another 8-pin PIC I could use? I need to be able to find SMT packages as the space where the PCB is going is extremely tight (I've never soldered SMTs before so that should be fun).

    Here's the whole code:

    Code:
    '****************************************************************
    '*  Name    : Nacelle_Blinking_Lights_5_12F629_4Mhz_Int.pbp     *
    '*  Author  : Ross A. Waddell                                   *
    '****************************************************************
    
    
    ' For production version of this code, update config fuses to 
    ' enable code protect on
    
    ' Basic blinky code provided by Darrel Taylor
    ' See forum posting here:
    ' http://www.picbasic.co.uk/forum/showthread.php?t=17299&p=116934#post116934
    
    ' ***************************************************************
    ' Pin Connections
    ' ***************************************************************
    
    ' GP5    -> pin 2             -> R4 -> BL4
    ' GP4    -> pin 3             -> R5 -> BL5
    ' GP3    -> pin 4             -> MCLR (input only, 'dummy' BL6)
    ' GP2    -> pin 5             -> R1 -> BL1
    ' GP1    -> pin 6             -> R2 -> BL2
    ' GP0    -> pin 7             -> R3 -> BL3
    
    ' ***************************************************************
    ' Initialization
    ' ***************************************************************
    
    DEFINE OSC 4             ' Set oscillator 4Mhz
    
    DEFINE BLINKYFREQ 100    ' 10mS periods
    
    CMCON    = %0000111	     'Turn off comparators
    TRISIO   = %00000000	 'Make all GPIO pins output
    
    ' ***************************************************************
    ' Device Fuses
    ' ***************************************************************
    ' PIC chip data sheets can be found here: C:\Program Files\Microchip\MPASM Suite
          
    #CONFIG
           __config _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_ON & _MCLRE_OFF & _BODEN_ON & _CP_OFF & _CPD_OFF
    #ENDCONFIG
    
    LEDcount    CON 6            ; Number of blinking LEDs
                                 ; (includes 'fummy' LED on GP3/MCLR pin, 
                                 ; rates defined separately below)
                                             
    '                 BL3,BL2,BL1,BL6,BL5,BL4
    '                 --- --- --- --- --- ---
    ' default "on" periods for each output
    OnTimes     DATA  50 ,82 ,50 ,33 ,133, 2
    ' default "off" periods for each output
    OffTimes    DATA 150 ,45 ,50 ,33 ,22 ,48
    
    ;----------------------------------------------------------------
    #DEFINE USE_RANDOM_SEQUENCE           ; comment for contiuous Sequence
    #IFDEF USE_RANDOM_SEQUENCE            
        RND     VAR WORD : RND = 13864
        MIN_ON  CON 33                    ; Minimum random ON time
        MAX_ON  CON 100                   ; Maximum random ON time
        MIN_OFF CON 33                    ; Minimum random OFF time
        MAX_OFF CON 100                   ; Maximum random OFF time
        RandPeriod VAR WORD[LEDcount]
        RandPeriods DATA WORD 1000, WORD 1250, WORD 1500, WORD 500, WORD 2000, WORD 2000
    #ENDIF
    
    CCPR1val      CON EXT      : @CCPR1val = (OSC*1000000/4)/ BLINKYFREQ
    CCPR1         VAR WORD EXT : @CCPR1 = CCPR1L
    Timer1        VAR WORD EXT : @Timer1 = TMR1L
    CCPIF         VAR PIR1.2
    
    LoopLED       VAR BYTE[LEDcount]
    OnTime        VAR BYTE[LEDcount]
    OffTime       VAR BYTE[LEDcount]
    x             VAR BYTE
    
    ' ***************************************************************
    
    ;----[Initialize on/off periods & random sequencing (if enabled)]---------------
    FOR x = 0 to (LEDcount)           ; Load the periods from EEPROM
        READ OnTimes+x, OnTime(x)
        READ OffTimes+x, OffTime(x)
        #IFDEF USE_RANDOM_SEQUENCE
            IF x < 5 THEN
                READ RandPeriods+(x<<1), WORD RandPeriod(x)
            ENDIF
        #ENDIF
    NEXT X
    
    ;-- setup CCP1 and Start Timer1 --
    CCPR1   = CCPR1val          ; set compare value
    CCP1CON = %00001011         ; compare mode, special event 
    Timer1  = 0                 ; clear Timer1
    T1CON.0 = 1                 ; start Timer1
    
    Main: 
        x = (x + 1) // LEDcount
        GPIO.0(x) = !!(LoopLED(x) < OnTime(x))
        LoopLED(x) = (LoopLED(x) + 1) // (OnTime(x) + OffTime(x))
        #IFDEF USE_RANDOM_SEQUENCE
            RandPeriod(x) = RandPeriod(x) - 1
            IF RandPeriod(x) = 0 THEN
                READ RandPeriods+(x<<1), WORD RandPeriod(x)
                RANDOM RND
                OnTime(x) = (MAX_ON - MIN_ON)* RND.HighByte / 255 + MIN_ON 
                OffTime(x)= (MAX_OFF - MIN_OFF)* RND.LowByte / 255 + MIN_OFF
            ENDIF
        #ENDIF
        IF x != (LEDcount - 1) THEN Main
    
    Waiting: IF !CCPIF THEN Waiting
        CCPIF = 0
    GOTO Main

  3. #3
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default Re: Single PIC to Blink 5 LEDs Independently?

    The 12F629 doesn't have a CCP module.
    You'll be better off with the 12F683.

    Don't comment the CCPR1 line in the .inc file. Comment the one in the blinky code.
    Code:
    CCPR1         VAR WORD EXT : @CCPR1 = CCPR1L
    DT

  4. #4
    Join Date
    Apr 2011
    Location
    Welches, Oregon
    Posts
    198


    Did you find this post helpful? Yes | No

    Default Re: Single PIC to Blink 5 LEDs Independently?

    You might also, if I follow your intent, use an AND gate to combine PWM and I/O into one PWM modulated blinking output.

  5. #5


    Did you find this post helpful? Yes | No

    Default Re: Single PIC to Blink 5 LEDs Independently?

    Quote Originally Posted by Amoque View Post
    You might also, if I follow your intent, use an AND gate to combine PWM and I/O into one PWM modulated blinking output.
    Presumably I'd need 5 AND gates for the 5 outputs, right? and then connect the other input of each AND gate to a PWM from another chip?

  6. #6


    Did you find this post helpful? Yes | No

    Default Re: Single PIC to Blink 5 LEDs Independently?

    Quote Originally Posted by Darrel Taylor View Post
    The 12F629 doesn't have a CCP module.
    You'll be better off with the 12F683.

    Don't comment the CCPR1 line in the .inc file. Comment the one in the blinky code.
    Code:
    CCPR1         VAR WORD EXT : @CCPR1 = CCPR1L
    Thanks Darrel!

  7. #7


    Did you find this post helpful? Yes | No

    Default Re: Single PIC to Blink 5 LEDs Independently?

    Quote Originally Posted by Darrel Taylor View Post
    The 12F629 doesn't have a CCP module.
    You'll be better off with the 12F683.

    Don't comment the CCPR1 line in the .inc file. Comment the one in the blinky code.
    Code:
    CCPR1         VAR WORD EXT : @CCPR1 = CCPR1L
    I converted the code to use a 12F683 PIC but the LEDs come on and then stay on (although not all 5; some stay off). Is it because of the input-only GP4/MCLR?

    Code:
    ' Basic blinky code provided by Darrel Taylor
    ' See forum posting here:
    ' http://www.picbasic.co.uk/forum/showthread.php?t=17299&p=116934#post116934
    
    ' ***************************************************************
    ' Pin Connections
    ' ***************************************************************
    
    ' GP5    -> pin 2             -> R4 -> BL4
    ' GP4    -> pin 3             -> R5 -> BL5
    ' GP3    -> pin 4             -> MCLR (input only, 'dummy' BL6)
    ' GP2    -> pin 5             -> R1 -> BL1
    ' GP1    -> pin 6             -> R2 -> BL2
    ' GP0    -> pin 7             -> R3 -> BL3
    
    ' Initialization
    ' ***************************************************************
    
    DEFINE OSC 8             ' Set oscillator 8Mhz
    
    DEFINE BLINKYFREQ 100    ' 10mS periods
    
    OSCCON   = %0111
    CMCON0   = %0000111	     ' Turn off comparators
    ANSEL.0  = 0             ' Digital only
    ANSEL.1  = 0             ' Digital only
    ANSEL.2  = 0             ' Digital only
    ANSEL.3  = 0             ' Digital only
    ADCON0.0 = 0             ' ADC is disabled
    TRISIO   = %00000000	 ' Make all GPIO pins output
    
    ' ***************************************************************
    ' Device Fuses
    ' ***************************************************************
    ' PIC chip data sheets can be found here: C:\Program Files\Microchip\MPASM Suite
          
    #CONFIG
           __config _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_ON & _MCLRE_OFF & _BOD_ON & _CP_OFF & _CPD_OFF
    #ENDCONFIG
    
    LEDcount    CON 6            ; Number of blinking LEDs
                                 ; (includes 'fummy' LED on GP3/MCLR pin, 
                                 ; rates defined separately below)
                                             
    '                 BL3,BL2,BL1,BL6,BL5,BL4
    '                 --- --- --- --- --- ---
    ' default "on" periods for each output
    OnTimes     DATA  50 ,82 ,50 ,33 ,133, 2
    ' default "off" periods for each output
    OffTimes    DATA 150 ,45 ,50 ,33 ,22 ,48
    
    ;----------------------------------------------------------------
    #DEFINE USE_RANDOM_SEQUENCE           ; comment for contiuous Sequence
    #IFDEF USE_RANDOM_SEQUENCE            
        RND     VAR WORD : RND = 13864
        MIN_ON  CON 33                    ; Minimum random ON time
        MAX_ON  CON 100                   ; Maximum random ON time
        MIN_OFF CON 33                    ; Minimum random OFF time
        MAX_OFF CON 100                   ; Maximum random OFF time
        RandPeriod VAR WORD[LEDcount]
        RandPeriods DATA WORD 1000, WORD 1250, WORD 1500, WORD 500, WORD 2000, WORD 2000
    #ENDIF
    
    CCPR1val      CON EXT      : @CCPR1val = (OSC*1000000/4)/ BLINKYFREQ
    ;CCPR1         VAR WORD EXT : @CCPR1 = CCPR1L
    Timer1        VAR WORD EXT : @Timer1 = TMR1L
    CCPIF         VAR PIR1.2
    
    LoopLED       VAR BYTE[LEDcount]
    OnTime        VAR BYTE[LEDcount]
    OffTime       VAR BYTE[LEDcount]
    x             VAR BYTE
    
    ' ***************************************************************
    
    ;----[Initialize on/off periods & random sequencing (if enabled)]---------------
    FOR x = 0 to (LEDcount)           ; Load the periods from EEPROM
        READ OnTimes+x, OnTime(x)
        READ OffTimes+x, OffTime(x)
        #IFDEF USE_RANDOM_SEQUENCE
            READ RandPeriods+(x<<1), WORD RandPeriod(x)
        #ENDIF
    NEXT X
    
    ;-- setup CCP1 and Start Timer1 --
    CCPR1   = CCPR1val          ; set compare value
    CCP1CON = %00001011         ; compare mode, special event 
    Timer1  = 0                 ; clear Timer1
    T1CON.0 = 1                 ; start Timer1
    
    Main: 
        x = (x + 1) // LEDcount
        GPIO.0(x) = !!(LoopLED(x) < OnTime(x))
        LoopLED(x) = (LoopLED(x) + 1) // (OnTime(x) + OffTime(x))
        #IFDEF USE_RANDOM_SEQUENCE
            RandPeriod(x) = RandPeriod(x) - 1
            IF RandPeriod(x) = 0 THEN
                READ RandPeriods+(x<<1), WORD RandPeriod(x)
                RANDOM RND
                OnTime(x) = (MAX_ON - MIN_ON)* RND.HighByte / 255 + MIN_ON 
                OffTime(x)= (MAX_OFF - MIN_OFF)* RND.LowByte / 255 + MIN_OFF
            ENDIF
        #ENDIF
        IF x != (LEDcount - 1) THEN Main
    
    Waiting: IF !CCPIF THEN Waiting
        CCPIF = 0
    GOTO Main

  8. #8
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default Re: Single PIC to Blink 5 LEDs Independently?

    On the 12F683, the CCP1IF bit is PIR1.5.
    Why they have to change things all the time ... I don't know.
    Code:
    CCPIF         VAR PIR1.5
    Both the OSCCON and CMCON0 values don't have enough digits.
    added: CMCON0 won't matter cause it's still 7, but to keep things consistent ...
    Code:
    OSCCON   = %01110000        ' 8Mhz
    CMCON0   = %00000111	    ' Turn off comparators
    Not sure how this got changed, it should be ...
    Code:
    FOR x = 0 to (LEDcount - 1)           ; Load the periods from EEPROM

    Last edited by Darrel Taylor; - 27th March 2014 at 04:47.
    DT

  9. #9
    Join Date
    Apr 2011
    Location
    Welches, Oregon
    Posts
    198


    Did you find this post helpful? Yes | No

    Default Re: Single PIC to Blink 5 LEDs Independently?

    Again, not wholly sure what your trying to do... I have trouble following someone else's code (even my own sometimes), but a PWM output tied to one input of each (yes, 5) "AND" gate, the other input to each I/O. Toggling I/O then controls modulated output. I am thinking of IR communications where a serial data stream and 38K PWM are "ANDed" for IR transmission.

  10. #10


    Did you find this post helpful? Yes | No

    Default Re: Single PIC to Blink 5 LEDs Independently?

    Quote Originally Posted by Amoque View Post
    Again, not wholly sure what your trying to do... I have trouble following someone else's code (even my own sometimes), but a PWM output tied to one input of each (yes, 5) "AND" gate, the other input to each I/O. Toggling I/O then controls modulated output. I am thinking of IR communications where a serial data stream and 38K PWM are "ANDed" for IR transmission.
    I was initially intending to make the brightness of the LEDs controllable via PWM, but now the chips are moving off the main board to another PCB that will be buried deep within my model where they will only have +5V & GND wires. These boards are too small to add any other components (even SMTs) so I'll just have to breadboard them with various series-limiting resistors to get the right brightness.

    Your suggestion is something I will investigate for the rest of my circuit where I do intend to provide brightness control on the main board. Right now, I'm daisy-chaining 2 NPNs which someone on SparkFun suggested but I haven't breadboarded it yet to see if it works.

  11. #11


    Did you find this post helpful? Yes | No

    Default Re: Single PIC to Blink 5 LEDs Independently?

    Quote Originally Posted by Darrel Taylor View Post
    On the 12F683, the CCP1IF bit is PIR1.5.
    Why they have to change things all the time ... I don't know.
    Code:
    CCPIF         VAR PIR1.5
    Both the OSCCON and CMCON0 values don't have enough digits.
    added: CMCON0 won't matter cause it's still 7, but to keep things consistent ...
    Code:
    OSCCON   = %01110000        ' 8Mhz
    CMCON0   = %00000111	    ' Turn off comparators
    Not sure how this got changed, it should be ...
    Code:
    FOR x = 0 to (LEDcount - 1)           ; Load the periods from EEPROM

    Thank you SOOOO much, Darrel!

    BTW, what software is that you are using to test out the code?

Similar Threads

  1. Single button function
    By DynamoBen in forum mel PIC BASIC Pro
    Replies: 40
    Last Post: - 4th April 2020, 18:33
  2. How to blink 8 LEDs at different rates- concurrently?
    By rmteo in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 26th April 2010, 23:47
  3. single sided PCB
    By schu4647 in forum General
    Replies: 1
    Last Post: - 10th December 2008, 18:22
  4. Can't Blink 2 LEDs
    By dfort in forum mel PIC BASIC
    Replies: 2
    Last Post: - 5th March 2008, 22:36
  5. Tx and Rx of Single Pin PIC's
    By Dwayne in forum Code Examples
    Replies: 0
    Last Post: - 26th May 2004, 14:55

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts