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?

    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

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

  3. #3


    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?

  4. #4


    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!

  5. #5


    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

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

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

  8. #8


    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.

  9. #9
    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
    BTW, what software is that you are using to test out the code?
    It's Proteus Design Suite from Labcenter Electronics.

    Quote Originally Posted by RossWaddell View Post
    I was initially intending to make the brightness of the LEDs controllable via PWM, ... so I'll just have to breadboard them with various series-limiting resistors to get the right brightness.
    That can be done in software using MIBAM.
    The blinky's timer will have to be changed, but it's doable.
    DT

  10. #10


    Did you find this post helpful? Yes | No

    Default Re: Single PIC to Blink 5 LEDs Independently?

    Spent a very happy afternoon reading up on MIBAM - can't wait to try it out (is there a later version of the .pbp code or is the v1.0 you posted on the melbas page the one I should use?).

    In preparation for that, I converted the now-workiing code to use Timer2. I think that since Timer2 is 8-bit (versus 16-bit for Timer1) that I don't need to worry about using the Least Significant register as you did with Timer1 but can just use TMR2:

    Code:
    CCPR1val      CON EXT      : @CCPR1val = (OSC*1000000/4)/ BLINKYFREQ
    ;CCPR1         VAR WORD EXT : @CCPR1 = CCPR1L
    Timer2        VAR WORD EXT : @Timer2 = TMR2
    CCPIF         VAR PIR1.5
    And then this to turn it on:

    Code:
    ;-- setup CCP1 and Start Timer2 --
    CCPR1   = CCPR1val          ; set compare value
    CCP1CON = %00001011         ; compare mode, special event 
    Timer2  = 0                 ; clear Timer2
    T2CON.0 = 1                 ; start Timer2
    Is that it?

  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