Single PIC to Blink 5 LEDs Independently?


Closed Thread
Results 1 to 40 of 69

Hybrid View

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

  2. #2


    Did you find this post helpful? Yes | No

    Default Re: Single PIC to Blink 5 LEDs Independently?

    Darrel - do you also think I can simply comment out this line to compile for a 12F629?

    CCPR1 VAR WORD EXT : @CCPR1 = CCPR1L

  3. #3
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,648


    Did you find this post helpful? Yes | No

    Default Re: Single PIC to Blink 5 LEDs Independently?

    Hi,

    I think you also have to add some lines very specific for the used chip ...

    especially to deal with GPIO.3 ... which is input only pin ...

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  4. #4


    Did you find this post helpful? Yes | No

    Default Re: Single PIC to Blink 5 LEDs Independently?

    I overlooked that GP3 is input only, Alain. So the 12F629 has only 5 output pins. That would still work for the 5 blinky LEDs but I've been expanding Darrel's example to include 2 other non-random blinky LEDs in order to further reduce the # of components, so if I end up with 6-7 different blink rates I might as well stay with the 16F1825 (since there doesn't appear to be any PICs with 10-12 pins).

  5. #5
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    966


    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
    It is equivelent to ...
    Code:
    IF LoopLED(x) < OnTime(x) THEN
        GPIO.0(x) = 1
    ELSE
        GPIO.0(x) = 0
    ENDIF
    Like any invention, after the explanation, it seems obvious. However, this is a coding construct I loved learning.

  6. #6
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,115


    Did you find this post helpful? Yes | No

    Default Re: Single PIC to Blink 5 LEDs Independently?

    Cool! The man is an artist!

    Kudos to Darrel.

    Ioannis

  7. #7
    Join Date
    Aug 2005
    Location
    Michigan, USA
    Posts
    224


    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
    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.
    Could the comparison have been reversed, like this?

    Code:
    GPIO.0(x) = !(LoopLED(x) >= OnTime(x))
    Also, is the GPIO.0(x) a valid PBP construct? It's not very intuitive (to me). I would think that GPIO.0 is the GP0 pin and then the (x) looks really alien...
    Last edited by Mike, K8LH; - 30th October 2012 at 15:03.

  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?

    Thanks guys, but I learned the !! from Charles Leo (my boss).
    It's great to be in the offices of melabs.

    Mike,

    Yup, that's the correct way to "invert the logic of the comparison, use a single !, which gives you a non-inverted result.".

    And while it does look strange, the (x) in GPIO.0(x) is the offset from GPIO.0.
    If x = 0 then it uses GPIO.0
    If x = 2, it uses GPIO.2

    You can start from any bit.
    With GPIO.2(x)

    If x = 0 then it uses GPIO.2
    If x = 2, it uses GPIO.4

    This notation can only be used in direct assignments (using = sign).
    You cannot use it as PIN designators in PBP commands like SERIN/OUT, HIGH/LOW etc.
    DT

  9. #9


    Did you find this post helpful? Yes | No

    Default Re: Single PIC to Blink 5 LEDs Independently?

    No argument from me, SteveB - the elegance of the solution is indeed beautiful!

    Would I gain any better resolution/exactitude of timings by increasing the oscillator? The 16F1825 can go up to 32Mhz internal with the pullup enabled. I've added to Darrel's solution to include another blinky without randomization of the periods, so it needs to be on for 20ms and off for 480ms (i.e. it blinks twice a second).
    Last edited by RossWaddell; - 30th October 2012 at 18:32.

  10. #10
    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 RossWaddell View Post
    Would I gain any better resolution/exactitude of timings by increasing the oscillator?
    The timing is maintained by the CCP module and Timer1. Those periods will remain the same reqardless of the CPU's frequency.
    Increasing the frequency will only allow more instructions to be executed between periods.

    The way I left it, only 4.5 mS of the 10 mS periods are being used to run the 5 led's.
    You should be able to add 2 more without any problems.
    DT

  11. #11


    Did you find this post helpful? Yes | No

    Default Re: Single PIC to Blink 5 LEDs Independently?

    I switched to a PIC16F88 since I needed a chip which had 8 pins of the same port (i.e. PORTB0-7). I thought I had it working using RB0-6 (7 blinkies) but realized this chip has the external interrupt on RB0, which I will need for the button to toggle 'FlashMode'. So I moved everything over to PORTA and now the LED connected to RA7 kind of flickers and is no way near the 1.5 sec on/0.5 sec off flash rate set in code.

    Code:
    '****************************************************************
    '*  Name    : Formation_Engine_Running_Lights_16F88_4Mhz_Int.pbp*
    '*  Author  : Ross A. Waddell                                   *
    '*  Notice  : Copyright (c) 2012                                *
    '*          : All Rights Reserved                               *
    '*  Date    : 10/29/2012                                        *
    '*  Version : 1.0                                               *
    '*  Notes   : Strobe lights, nacelle lights (steady-on and      *
    '*            Christmas flashers) and running lights for the    *
    '*            TOS Enterprise                                    *
    '*          :                                                   *
    '****************************************************************
    
    ' ***************************************************************
    ' Pin Connections
    ' ***************************************************************
    
    ' RA0                        -> Flashing lights for nacelle engines 1  (x2)
    ' RA1                        -> Flashing lights for nacelle engines 2  (x2)
    ' RA2                        -> Flashing lights for nacelle engines 3  (x2)
    ' RA3                        -> Flashing lights for nacelle engines 4  (x2)
    ' RA4                        -> Flashing lights for nacelle engines 5  (x2)
    ' RA5                        -> Dummy output (MCLR is input only)
    ' RA6                        -> Secondary hull strobe/formation lights (x2)
    ' RA7                        -> Primary hull running lights            (x4)
    ' RB0                        -> Button for toggling running light flash mode
    ' RB1                        -> Steady-on lights for nacelle engines   (x10)
    
    ' Secondary Hull Strobes Lights
    ' =============================
    ' The two strobe locastions are:
    '   - Port/starboard rear secondary hull (just forward of the "1837" marking),
    '     horizontally inline with the shuttlebay flight deck
    
    ' The strobe light flashed at the rate of twice per second, 
    ' or every 12 film frames. Since it's a strobe, it stays on
    ' for much less time than a running light; approx. .020 sec
    
    ' Nacelle engine lights
    ' =====================
    ' (a) The colours used were: red, blue, green, amber and pink, all standard colours 
    '     they had for common Christmas lights of the time. 
    ' (b) They were all Christmas lights (C7, too big for a 1/350).
    ' (c) Amber was steady (5 in a star pattern), the other 5 blinked at various rates
    
    ' Running lights
    ' ==============
    ' The running light locations on the primary hull are (4):
    '   - Saucer Port [top] (red) - 1
    '   - Saucer Starboard [top] (green) - 1
    '   - Saucer Port/Starboard [bottom (white) - 2
    
    ' The TOS E running lights flash at a base timing of 1-1/2 sec (36 frames) on, 
    ' 1/2 sec (12 frames) off (FlashMode = 0)
    
    ' Alternate is 1/2 sec on, 1.5 sec off (reverse of above). (FlashMode = 1)
    
    ' In "The Corbomite Maneuver", the lights were on for 18 frames and off for 20
    ' (FlashMode = 2)
    
    ' >> These modes are available via a momentary button
    
    DEFINE OSC 4                 ; Set oscillator 4Mhz
    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_INTOSCIO & _WDTE_ON & _PWRTE_ON & _MCLRE_OFF & _CP_OFF & _CPD_OFF & _LVP_OFF
    #ENDCONFIG
    
    ' ***************************************************************
    ' Initialization
    ' ***************************************************************
    
    OSCCON   = %01100000         ; 4MHz internal osc
    
    ANSEL    = 0                 ; PIC16F88 only
    TRISA    = 0                 ; PORTA all output
    TRISB    = %00000001         ; Make PORTB pin 0 input for flash mode button
    
    LEDcount    CON 8                        ; Number of blinking LEDs on PORTA
    '                 BL1,BL2,BL3,BL4,BL5,MCLR,STB
    OnTimes     DATA  50 ,22 ,38 ,75 ,17 ,2   ,2  ; default "on" periods for each output
    OffTimes    DATA 150 ,45 ,38 ,95 ,117,2   ,48 ; default "off" periods for each output
    
                                             ; (Strobe flashes 2 times per second
                                             ; or every 500ms; subtract "on" time)
     
    ' Running lights will be handled slightly differently
    
    ' FlashMode Values
    ' ================
    ' 0 = 1-1/2 sec (36 frames) on,  1/2 sec (12 frames) off (Trek Ace, HobbyTalk)
    ' 1 = 1/2 sec (12 frames) on, 1-1/2 sec (36 frames) off (Master Replica model)
    ' 2 = 3/4 sec (18 frames) on, 5/6 sec (20 frames) off ("The Corbomite Maneuver")
    
    FlashMode_Default CON  0
    EE_FlashMode      DATA FlashMode_Default
    FlashMode         VAR  BYTE
    READ EE_FlashMode, FlashMode
                                             
    ;----------------------------------------------------------------
    #DEFINE USE_RANDOM_SEQUENCE           ; comment for contiuous Sequence
    #IFDEF USE_RANDOM_SEQUENCE            ; randomization used for BL1-5 only
        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-3]
        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
    
    LED_STDY_ON   VAR PORTB.1    ; Alias PORTB.1 as "LED_STDY_ON"
    
    ;Old_FlashMode           VAR BYTE
    PRI_HULL_LGHTS_ON_MS    VAR BYTE
    PRI_HULL_LGHTS_OFF_MS   VAR BYTE
    
    ;----[Initialize on/off periods & random sequencing (if enabled)]---------------
    ;     (only set up first 7 blinkies; the 8th [running lights] is separate)
    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
    
    GOSUB SetFlashRates    ; Set up flash rates based on saved "FlashMode" val
    
    ;-- 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
    
    HIGH LED_STDY_ON       ; Turn on steady-on LEDs
    
    ;----[Main Program Loop]----------------------------------------
    Main: 
        x = (x + 1) // LEDcount
        PORTA.0(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
    
    SetFlashRates:
        If FlashMode = 0 Then
            PRI_HULL_LGHTS_ON_MS = 150
            PRI_HULL_LGHTS_OFF_MS = 50    
        ElseIf FlashMode = 1 Then
            PRI_HULL_LGHTS_ON_MS = 50
            PRI_HULL_LGHTS_OFF_MS = 150      
        Else
            PRI_HULL_LGHTS_ON_MS = 75
            PRI_HULL_LGHTS_OFF_MS = 83        
        EndIf
    
        OnTime(8)  = PRI_HULL_LGHTS_ON_MS
        OffTime(8) = PRI_HULL_LGHTS_OFF_MS
    
        RETURN
    END
    What am I doing wrong? In case anyone is wondering, I only want to randomize RA0-4 flash rates. Also, RA5 (MCLR) is input only so I had to make that a dummy output pin.

    Is there something special about RA7 because of Timer1?

  12. #12
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,648


    Did you find this post helpful? Yes | No

    Default Re: Single PIC to Blink 5 LEDs Independently?

    Not really ...

    Code:
    OnTimes     DATA  50 ,22 ,38 ,75 ,17 ,2   ,2  ; default "on" periods for each output
    OffTimes    DATA 150 ,45 ,38 ,95 ,117,2   ,48 ; default "off" periods for each output
    nothing missing here ???

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

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