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?

    I will indeed try that out with a 1fF1825. Could you please clarify for me where I set the individual on/off values for each of the 8 LEDs? In my existing implementation (5 separate 12F683's) I have code like this:

    Code:
    ' Changing the value for "Cycle" will change the time it takes
    ' to fade from 100% to 0% (or 0% to 100% if duty is increasing)
    ' Lower number = faster fade in/out
    Cycle = 1
    
    ' Higher step value produces faster fade, but too high a value
    ' will introduce flicker
    STEP_CNTR = 2
    
    Main:
        ' Fade in
        For Duty = 0 TO 255 step STEP_CNTR
            PWM LED_0, Duty, Cycle
        Next
    
        ' Stay on LGHTS_ON_MS
        HIGH LED_0
        Pause LGHTS_ON_MS
    
        ' Fade out
        For Duty = 255 TO 0 STEP -STEP_CNTR
            PWM LED_0, Duty, Cycle
        Next
    	
        ' Stay off for LGHTS_OFF_MS
        Pause LGHTS_OFF_MS
    
        ' Fade in
        For Duty = 0 TO 255 step STEP_CNTR
            PWM LED_0, Duty, Cycle
        Next
    
        ' Stay on LGHTS_ON_MS
        HIGH LED_0
        Pause (LGHTS_ON_MS - 200)
    
        ' Fade out
        For Duty = 255 TO 0 STEP -STEP_CNTR
            PWM LED_0, Duty, Cycle
        Next
    	
        ' Stay off for LGHTS_OFF_MS
        Pause (LGHTS_OFF_MS + 100)
    
        GOTO Main
    (In actuality, I extend the fade in/stay on/fade out/stay off with various timings to randomize the effect. Old 1960's Christmas tree lights did not have constant blink rates).

  2. #2


    Did you find this post helpful? Yes | No

    Default Re: Single PIC to Blink 5 LEDs Independently?

    I've tried adapting Darrel's example from this thread, but no luck with a PIC16F1825 - PORTC.0-3 do not blink (PORTC.0-2 are off; PORTC.3 is on) but PORTC.4 blinks properly. What am I doing wrong here? I've had to adapt Darrel's code for the PIC16F1825 and reduced the number of blinkies to 5 from 8.

    Code:
    '****************************************************************
    '*  Name    : UNTITLED.BAS                                      *
    '*  Author  : Ross Waddell                                      *
    '*  Notice  : Copyright (c) 2012 [select VIEW...EDITOR OPTIONS] *
    '*          : All Rights Reserved                               *
    '*  Date    : 10/27/2012                                        *
    '*  Version : 1.0                                               *
    '*  Notes   : PIC16F1825                                                 *
    '*          :                                                   *
    '****************************************************************
    
    DEFINE OSC 4
    DEFINE BLINKYFREQ 100  ; 10mS periods
    
    ' ***************************************************************
    ' Device Fuses
    ' ***************************************************************
    ' PIC chip data sheets can be found here: C:\Program Files\Microchip\MPASM Suite
    #CONFIG
       __config _CONFIG1, _FOSC_INTOSC & _WDTE_ON & _PWRTE_ON & _MCLRE_OFF & _CP_OFF & _CPD_OFF
       __config _CONFIG2, _PLLEN_OFF & _STVREN_ON & _BORV_LO & _LVP_OFF
    #ENDCONFIG
    
    OSCCON   = %01101000        ' 4MHz internal osc
    
    
    DATA 50,22,38,75,17;,40,62,13  ; default periods for each Output
    ;----------------------------------------------------------------
    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[5]
    LoopCON   VAR BYTE[5]
    x         VAR BYTE
    
    ;----[Initialize]------------------------------------------------
    FOR x = 0 to 4         ; load the periods from EEPROM
        READ x, LoopCON(x)
    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
    
    ANSELC = 0
    TRISC = 0              ; PORTC all OUTPUT
    
    ;----[Main Program Loop]----------------------------------------
    Main: 
        ;x = (x + 1) // 8
        x = (x+1)&4
        PORTC.0(x) = !(LoopLED(x) < LoopCON(x))
        LoopLED(x) = (LoopLED(x) + 1) // (LoopCON(x) << 1)
        IF x != 4 THEN Main
    
    Waiting: IF !CCPIF THEN Waiting
        CCPIF = 0
    GOTO Main
    Last edited by RossWaddell; - 28th October 2012 at 19:35. Reason: correction

  3. #3


    Did you find this post helpful? Yes | No

    Default Re: Single PIC to Blink 5 LEDs Independently?

    Ok, so I answered my own question.

    This works:
    Code:
    x = (x + 1) // 5
    This doesn't:
    Code:
    x = (x+1)&4
    The question now becomes: is there a way to set the on/off periods individually for each of the 5 LEDs? The code above has the LEDs blink with the same on time as off; I need to be able to make some of the 'off' times longer than the 'on' and vice versa.
    Last edited by RossWaddell; - 28th October 2012 at 19:43.

  4. #4
    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 question now becomes: is there a way to set the on/off periods individually for each of the 5 LEDs?
    Yes, but you have to create the individual ON/OFF periods.
    That code only has 1 period, for a 50% dutycycle.

    So many things you can do.
    So little time to explain.
    Twinkle Twinkle little tree ...
    Code:
    DEFINE OSC 4
    DEFINE BLINKYFREQ 100  ; 10mS periods
    
    ' ***************************************************************
    ' Device Fuses
    ' ***************************************************************
    #CONFIG
       __config _CONFIG1, _FOSC_INTOSC & _WDTE_ON & _PWRTE_ON & _MCLRE_OFF & _CP_OFF & _CPD_OFF
       __config _CONFIG2, _PLLEN_OFF & _STVREN_ON & _BORV_LO & _LVP_OFF
    #ENDCONFIG
    
    OSCCON   = %01101000        ' 4MHz internal osc
    
    ;----------------------------------------------------------------
    LEDcount    CON 5                  ; Number of LEDs on the PORT
    OnTimes     DATA  50,22,38,75, 5   ; default periods for each Output
    OffTimes    DATA 150,45,38,95,34
    
    ;----------------------------------------------------------------
    #DEFINE USE_RANDOM_SEQUENCE     ; comment for contiuous Sequence
    #IFDEF USE_RANDOM_SEQUENCE
        RND     VAR WORD : RND = 13864
        MIN_ON  CON 10                  ; Minimum random ON time
        MAX_ON  CON 100                 ; Maximum random ON time
        MIN_OFF CON 10                  ; 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 1750, 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]------------------------------------------------
    FOR x = 0 to LEDcount - 1         ; 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
    
    ANSELC = 0
    TRISC = 0              ; PORTC all OUTPUT
    
    ;----[Main Program Loop]----------------------------------------
    Main: 
        x = (x + 1) // LEDcount
        PORTC.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
    DT

  5. #5


    Did you find this post helpful? Yes | No

    Default Re: Single PIC to Blink 5 LEDs Independently?

    My god ... it's ... it's beautiful Just like a 1960's string of Christmas tree lights, the exact effect I'm after. I can't thank you enough, Darrel!

    One last query (I hope) - since I only need 5 output pins I would like to convert this to a 12F683. When I do, I get the 'ol "Redefinition of VAR" error pointing to this line:

    Code:
    CCPR1      VAR WORD EXT : @CCPR1 = CCPR1L
    The whole code is here:

    Code:
    '****************************************************************
    '*  Name    : UNTITLED.BAS                                      *
    '*  Author  : Ross Waddell                                      *
    '*  Notice  : Copyright (c) 2012 [select VIEW...EDITOR OPTIONS] *
    '*          : All Rights Reserved                               *
    '*  Date    : 10/27/2012                                        *
    '*  Version : 1.0                                               *
    '*  Notes   : PIC12F683                                                 *
    '*          :                                                   *
    '****************************************************************
    
    DEFINE OSC 4
    DEFINE BLINKYFREQ 100  ; 10mS periods
    
    ' ***************************************************************
    ' 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
    
    OSCCON   = %01100000        ' 4MHz internal osc
    
    ;----------------------------------------------------------------
    LEDcount    CON 5                  ; Number of LEDs on the PORT
    OnTimes     DATA  50,22,38,75,17   ; default periods for each Output
    OffTimes    DATA 150,45,38,95,117
    
    ;----------------------------------------------------------------
    #DEFINE USE_RANDOM_SEQUENCE     ; comment for contiuous Sequence
    #IFDEF USE_RANDOM_SEQUENCE
        RND     VAR WORD : RND = 13864
        MIN_ON  CON 15                  ; Minimum random ON time
        MAX_ON  CON 115                 ; Maximum random ON time
        MIN_OFF CON 15                  ; Minimum random OFF time
        MAX_OFF CON 200                 ; Maximum random OFF time
        RandPeriod VAR WORD[LEDcount]
        RandPeriods DATA WORD 1000, WORD 1250, WORD 1500, WORD 1750, 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.5
    
    LoopLED    VAR BYTE[LEDcount]
    OnTime     VAR BYTE[LEDcount]
    OffTime    VAR BYTE[LEDcount]
    x          VAR BYTE
    
    ;----[Initialize]------------------------------------------------
    FOR x = 0 to (LEDcount - 1)         ; 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
    
    ANSEL  = 0
    TRISIO = 0             ; PORTC all OUTPUT
    
    ;----[Main Program Loop]----------------------------------------
    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
    Since there are no include files, my Google search didn't point to anything other than that. Is this too much to expect from a 12F?
    Last edited by RossWaddell; - 29th October 2012 at 01:04.

  6. #6
    Join Date
    May 2009
    Posts
    40


    Did you find this post helpful? Yes | No

    Default Re: Single PIC to Blink 5 LEDs Independently?

    Looking at the compiler include file for a 12f683 the CCPR1 VAR WORD EXT has already been included. I think you should be able to comment this line out.
    Can you please tell me what the following line of code does:
    GPIO.0(x) = !!(LoopLED(x) < OnTime(x))
    Cannot find any reference to !!

  7. #7
    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?

    Quote Originally Posted by grahamg View Post
    Can you please tell me what the following line of code does:
    GPIO.0(x) = !!(LoopLED(x) < OnTime(x))
    It is equivelent to ...
    Code:
    IF LoopLED(x) < OnTime(x) THEN
        GPIO.0(x) = 1
    ELSE
        GPIO.0(x) = 0
    ENDIF
    What you would like to do is a direct assignment of a True/False comparison to a variable.
    Code:
    GPIO.0(x) = LoopLED(x) < OnTime(x)
    But LoopLED(x) < OnTime(x) is a "Logical" expression that can't be assigned to a BIT variable.

    The bitwise NOT operator (!) can convert the logical expression to a bitwise expression that can be assigned to a BIT variable.

    A single ! will invert the result, so a second ! is used to invert it back.
    Somtimes you want the result inverted, and you can use a single !.
    Other times you might invert the logic of the comparison, use a single !, which gives you a non-inverted result.
    DT

  8. #8
    Join Date
    May 2006
    Location
    Del Rio, TX, USA
    Posts
    343


    Did you find this post helpful? Yes | No

    Default Re: Single PIC to Blink 5 LEDs Independently?

    Quote Originally Posted by RossWaddell View Post
    My god ... it's ... it's beautiful
    Yes, the code is beautiful (can speak for the lights, but I'm sure their nice too).

    Have you seen one of Darrel's breadbord layouts? Those are equally as beautiful.

Similar Threads

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