Colour changing LEDs


+ Reply to Thread
Page 1 of 3 123 LastLast
Results 1 to 15 of 32

Thread: Colour changing LEDs3639 days old

  1. #1
    chudson
    Guest

    0 Not allowed!

    Default Colour changing LEDs

    Hi Guys, I'm trying to get a PICF628 to colour change LEDs. I have 3 LEDs setup on a board with 27 Ohm resistors with MOSFETs driving ground and PIC controlling the MOSFETs on PORTA.6, PORTA.7 and PORTB.7.
    I am using the command PWM to try and achieve the colour changing effect but the code runs extremely slow. Heres the code:

    fade:
    For colour = 0 To 255 Step 1
    PWM PORTA.7, colour, 255
    PWM PORTA.6, colour, 255
    PWM PORTB.7, colour, 255
    DelayMS 10
    Next
    GoTo fade

    It effectively is running through each instruction at 1 second an instruction and executes then kills the instruction, e.g. Red comes on, red goes off, green comes on.....

    I am using the internal 8Mhz OSC.

    Any ideas?

  2. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  3. #2
    Maverick
    Guest

    0 Not allowed!

    Default Re: Colour changing LEDs

    What your trying to do wont work as the 16F628 only has one hardware PWM output and a soft PWM output is locked while it executes , so looking at your code you will get exactly what you describe.
    You would be better off using a hardware timer.

    Here is an example by Les that will fade 8 Leds all with independent duty cycles
    Code:
    ' Create eight PWM outputs, using interrupts
    ' Incremently increase brightness of eight LEDs attached to PORTD 
    
    '------------------------------------------------------------
    ' Compiler Declares :-
        Device = 16F877
    Reminders = Off
        Optimiser_Level = 1
        Dead_Code_Remove = On
        Xtal = 4
        Fsr_Context_Save = Off        ' Don't save the FSR register within the interrupt
    '------------------------------------------------------------
    ' Variables :-
    ' Make sure these variables are located low in RAM. i.e. first in the Dim list
    ' This will minimise RAM bank switching
    '
        Dim Temp As Byte
        Dim Accum0 As Byte           ' DutyCycle Accumumulators
        Dim Accum1 As Byte
        Dim Accum2 As Byte
        Dim Accum3 As Byte
        Dim Accum4 As Byte
        Dim Accum5 As Byte
        Dim Accum6 As Byte
        Dim Accum7 As Byte
        
        Dim DutyCycle0 As Byte        ' DutyCycle DUTY cycle registers
        Dim DutyCycle1 As Byte
        Dim DutyCycle2 As Byte
        Dim DutyCycle3 As Byte
        Dim DutyCycle4 As Byte
        Dim DutyCycle5 As Byte
        Dim DutyCycle6 As Byte
        Dim DutyCycle7 As Byte 
        
        
    '------------------------------------------------------------
    ' Symbol Alias's
        Symbol T0IE INTCON.5        ' Timer0 Overflow Interrupt Enable
        Symbol T0IF INTCON.2        ' Timer0 Overflow Interrupt Flag
        Symbol GIE  INTCON.7        ' Global Interrupt Enable
    
        Symbol PS0 OPTION_REG.0     ' Prescaler ratio bit-0
        Symbol PS1 OPTION_REG.1     ' Prescaler ratio bit-1
        Symbol PS2 OPTION_REG.2     ' Prescaler ratio bit-2
        Symbol PSA OPTION_REG.3     ' Prescaler Assignment (1=assigned to WDT, 0=assigned to oscillator)
        Symbol T0CS OPTION_REG.5    ' Timer0 Clock Source Select (0=Internal clock, 1=External PORTA.4)
    
    '------------------------------------------------------------
    
        On_Hardware_Interrupt GoTo PWM_interrupt ' Point interrupt to handler
        All_Digital = True                  ' Set PORTA and PORTD to all digital
        GoTo Main                           ' Jump over the interrupt handler
        
    '------------------------------------------------------------
    ' Interrupt Subroutine
    ' Update eight 8-bit PWM'S on PORTD
    PWM_interrupt:
        Context Save                        ' Save WREG, STATUS, and PCLATH
        Accum0 = Accum0 + DutyCycle0        ' Update DutyCycle0
        rol Temp    
        Accum1 = Accum1 + DutyCycle1        ' Update DutyCycle1
        rol Temp    
        Accum2 = Accum2 + DutyCycle2        ' Update DutyCycle2
        rol Temp    
        Accum3 = Accum3 + DutyCycle3        ' Update DutyCycle3
        rol Temp    
        Accum4 = Accum4 + DutyCycle4        ' Update DutyCycle4
        rol Temp    
        Accum5 = Accum5 + DutyCycle5        ' Update DutyCycle5
        rol Temp    
        Accum6 = Accum6 + DutyCycle6        ' Update DutyCycle6
        rol Temp    
        Accum7 = Accum7 + DutyCycle7        ' Update DutyCycle7
        rlf Temp,W
        movwf PORTD                         ' Output DutyCycle States
        T0IF = 0                            ' Clear the TMR0 overflow flag
        Context Restore                     ' Restore WREG, STATUS, and PCLATH. Then exit the interrupt
    '------------------------------------------------------------
    ' Subroutines can go here
    
    '------------------------------------------------------------
    ' Main Demonstration Loop
    Main:
        Temp = 0
        Accum0 = 0: Accum1 = 0: Accum2 = 0: Accum3 = 0
        Accum4 = 0: Accum5 = 0: Accum6 = 0: Accum7 = 0        
        DutyCycle0 = 0: DutyCycle1 = 0: DutyCycle2 = 0: DutyCycle3 = 0
        DutyCycle4 = 0: DutyCycle5 = 0: DutyCycle6 = 0: DutyCycle7 = 0
        
        Low PORTD                       ' Make PortD an output and extinguish the LEDs
        PSA = 0                         ' Assign the prescaler to external oscillator
        PS0 = 1                         ' Set the prescaler
        PS1 = 0                         '       to increment TMR0
        PS2 = 0                         '       every nth instruction cycle
        T0CS = 0                        ' Assign TMR0 clock to internal source
        TMR0 = 0                        ' Clear TMR0 initially
        T0IE = 1                        ' Enable TMR0 overflow interrupt
        GIE = 1                         ' Enable global interrupts    
        While 1 = 1
            For DutyCycle0 = 255 To 1 Step -1
                DutyCycle1 = ~DutyCycle0
                DelayMS 1
            Next
            For DutyCycle1 = 255 To 1 Step -1
                DutyCycle2 = ~DutyCycle1
                DelayMS 1
            Next
            For DutyCycle2 = 255 To 1 Step -1
                DutyCycle3 = ~DutyCycle2
                DelayMS 1
            Next
            For DutyCycle3 = 255 To 1 Step -1
                DutyCycle4 = ~DutyCycle3
                DelayMS 1
            Next
            For DutyCycle4 = 255 To 1 Step -1
                DutyCycle5 = ~DutyCycle4
                DelayMS 1
            Next
            For DutyCycle5 = 255 To 1 Step -1
                DutyCycle6 = ~DutyCycle5
                DelayMS 1
            Next
            For DutyCycle6 = 255 To 1 Step -1
                DutyCycle7 = ~DutyCycle6
                DelayMS 1
            Next
            For DutyCycle7 = 255 To 1 Step -1
                DutyCycle0 = ~DutyCycle7
                DelayMS 1
            Next
        Wend
    Simply modify it for 3 leds

  4. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  5. #3
    chudson
    Guest

    0 Not allowed!

    Default Re: Colour changing LEDs

    Thanks Maverick but it won't really work with what I am doing eventually. I have purchased a PIC16F737 today that has 3 PWM Modules. The only problem is the 3rd isnt working. The pin itself works high or low but won't PWM. Also the program freezes at a DelayMS. Any ideas? Heres the code:

    Device = 16F737
    @CONFIG_REQ
    @__CONFIG _CONFIG1, CP_OFF & CCP2_RC1 & DEBUG_OFF & VBOR_2_0 & BOREN_0 & MCLR_OFF & PWRTE_OFF & WDT_OFF & INTRC_IO
    @__CONFIG _CONFIG2, BORSEN_0 & IESO_OFF & FCMEN_OFF

    Xtal = 8


    'All_Digital = TRUE 'Equivilent to CMCON = 7 and ADCON1 = 7 on page 48 as per proton guide
    TRISC.2 = 0
    TRISC.1 = 0
    TRISB.5 = 0
    TRISC.5 = 0
    Declare CCP1_Pin PORTC.2
    Declare CCP2_Pin PORTC.1
    Declare CCP3_Pin PORTB.5
    Dim i As Byte

    main:

    HPWM 1, 100, 20000
    HPWM 3, 70, 20000
    HPWM 2, 255, 20000
    GoTo main

  6. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  7. #4
    Fanatical Contributor Tim's Avatar
    Join Date
    Jan 2003
    Posts
    7,709
    Thumbs Up
    Received: 49
    Given: 95
    Total Downloaded
    1.32 GB

    0 Not allowed!

    Default Re: Colour changing LEDs

    I doubt that Proton will work with the 3rd PWM channel as its not a standard item.

    There are many posts showing how to set up a PWM, its a just a couple of lines of code and the same to alter it.
    Tim

  8. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  9. #5
    chudson
    Guest

    0 Not allowed!

    Default Re: Colour changing LEDs

    Don't suppose you have a link to one? searching PWM gives no results. Thanks

  10. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  11. #6
    Fanatical Contributor fanie's Avatar
    Join Date
    Oct 2005
    Posts
    7,987
    Thumbs Up
    Received: 31
    Given: 15
    Total Downloaded
    434.52 MB

    0 Not allowed!

    Default Re: Colour changing LEDs

    The 16F737 has 3 PWM modules ? I know of no 16 or 18 bit pic with more than one PWM module. Only the 16 bit pics have more PWM modules as far as I know and they can be controlled independantly.

    You can only set up PWM's with the 8 bit pics as 1, 2 or 4 outputs. You can omit the 4th, but the timebase is the same for all. I have not succeeded in controlling more than 2 outputs independant from one onother.

    I'd go with Mav's manual generating PWM's.
    Fanie

  12. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  13. #7
    chudson
    Guest

    0 Not allowed!

    Default Re: Colour changing LEDs

    Hi Fanie, how do you setup with 4 PWMs and ommiting the 4th? I don't need the frequency to be different only the duty which I assume is not a problem.

  14. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  15. #8
    Fanatical Contributor fanie's Avatar
    Join Date
    Oct 2005
    Posts
    7,987
    Thumbs Up
    Received: 31
    Given: 15
    Total Downloaded
    434.52 MB

    0 Not allowed!

    Default Re: Colour changing LEDs

    how do you setup with 4 PWMs and ommiting the 4th
    That's the easy part. You let it switch in the air

    I'd be most interested if you get it to work.
    Fanie

  16. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  17. #9
    Prolific Poster pic-ignorant's Avatar
    Join Date
    Oct 2007
    Posts
    2,938
    Thumbs Up
    Received: 25
    Given: 31
    Total Downloaded
    926.61 MB

    0 Not allowed!

    Default Re: Colour changing LEDs

    A pointer to set up onboard hardare pwm manually...

    Device = 16F737
    Xtal
    8
    T2CON = %00000100 ' Timer2 ON and set up prescale
    PR2 = 25 ' Set PWM frequency
    CCPR1L = 13 ' Set PWM Duty-Cycle
    CCP1CON = %00001100 ' set CCP for PWM
    CCPR2L = 'repeat for other 2 ccp modules
    CCP2CON =
    CCPR3L =
    CCP3CON =

    Check the 16F737 datasheet for more detals

    The link is a useful tool for calculating the frequency/period/duty etc


    http://www.micro-examples.com/public...alculator.html


    Regards
    John

  18. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  19. #10
    chudson
    Guest

    0 Not allowed!

    Default Re: Colour changing LEDs

    lol I mean setting up the third in code as Proton doesn't recognise Declare CCP3_Pin or CCP4_Pin so how do you get 3rd and 4th channels?

  20. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  21. #11
    chudson
    Guest

    0 Not allowed!

    Default Re: Colour changing LEDs

    This code works fine

    Device = 16F737
    @CONFIG_REQ
    @__CONFIG _CONFIG1, CP_OFF & CCP2_RC1 & DEBUG_OFF & VBOR_2_0 & BOREN_0 & MCLR_OFF & PWRTE_OFF & WDT_OFF & INTRC_IO
    @__CONFIG _CONFIG2, BORSEN_0 & IESO_OFF & FCMEN_OFF
    OSCCON = %1011111

    Xtal = 8


    All_Digital = TRUE 'Equivilent to CMCON = 7 and ADCON1 = 7 on page 48 as per proton guide
    TRISC.2 = 0
    TRISC.1 = 0
    TRISB.5 = 0
    TRISC.5 = 0
    T2CON = %00000100
    Declare CCP1_Pin PORTC.2
    Declare CCP2_Pin PORTC.1
    Declare CCP3_Pin PORTB.5
    Declare CCP4_Pin PORTB.5
    CCP1CON = %00001100
    CCP2CON = %00001100
    CCP3CON = %00001100

    Dim duty As Byte
    Dim i As Byte
    DelayMS 100
    main:
    HPWM 2, 72, 20000
    CCPR3L = 224
    HPWM 1, 208, 20000
    'Turquise
    DelayMS 2500
    HPWM 2, 124, 20000
    CCPR3L = 252
    HPWM 1, 0, 20000
    'Lawn Green
    DelayMS 2500
    HPWM 2, 255, 20000
    CCPR3L = 255
    HPWM 1, 0, 20000
    'yellow
    DelayMS 2500
    HPWM 2, 255, 20000
    CCPR3L = 20
    HPWM 1, 147, 20000
    'deep pink
    DelayMS 2500
    HPWM 2, 255, 20000
    CCPR3L = 165
    HPWM 1, 0, 20000
    'orange
    DelayMS 2500

    GoTo main



    Thanks for your help guys

  22. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  23. #12
    Prolific Poster pic-ignorant's Avatar
    Join Date
    Oct 2007
    Posts
    2,938
    Thumbs Up
    Received: 25
    Given: 31
    Total Downloaded
    926.61 MB

    0 Not allowed!

    Default Re: Colour changing LEDs

    Glad you got it working.
    Are your 2500ms delays timing right? I havent got the datasheet open, but I think your Osccon setting doesn't match the 8mhz xtal setting.
    You could tidy up the code a bit by getting rid of the CCP pin declares, set PR2, and just use the CCPRxL statements in the code

  24. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  25. #13
    chudson
    Guest

    0 Not allowed!

    Default Re: Colour changing LEDs

    Quote Originally Posted by pic-ignorant View Post
    Glad you got it working.
    Are your 2500ms delays timing right? I havent got the datasheet open, but I think your Osccon setting doesn't match the 8mhz xtal setting.
    You could tidy up the code a bit by getting rid of the CCP pin declares, set PR2, and just use the CCPRxL statements in the code
    The delays are out quite a bit. There around 9 seconds long.

  26. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  27. #14
    Prolific Poster pic-ignorant's Avatar
    Join Date
    Oct 2007
    Posts
    2,938
    Thumbs Up
    Received: 25
    Given: 31
    Total Downloaded
    926.61 MB

    0 Not allowed!

    Default Re: Colour changing LEDs

    Try changing removing the Osccon declare, and replace it with: (Easier to read this way)

    OSCCON.4 = 1
    OSCCON.5 = 1
    OSCCON.6 = 1 'Set Internal Oscillator for 8Mhz.

    You Xtal declare is basing the timing on 8mhz, but your internal osc is running much slower. Look at the Osccon info in the datasheet Page 38

    Regards
    John

  28. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  29. #15
    chudson
    Guest

    0 Not allowed!

    Default Re: Colour changing LEDs

    I thought I had OSCCON setup like that but as OSCCON = %1011111. But switching to the way you said now means the timings running perfectly at 2500ms. Thanks for solving that

  30. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. 16 leds to drive, how?
    By Tim in forum The Lounge
    Replies: 21
    Last Post: 27th March 2019, 06:04
  2. Diffuser IR Leds (how)
    By Tim in forum The Lounge
    Replies: 19
    Last Post: 7th February 2019, 08:23
  3. changing speed on the fly
    By jyb in forum Proton Plus Compiler v3
    Replies: 8
    Last Post: 20th February 2014, 22:02
  4. X Power Leds
    By pic-ignorant in forum The Lounge
    Replies: 61
    Last Post: 7th April 2009, 20:40
  5. [SOLVED !] driving bi-colour leds
    By spenol in forum Proton Plus Compiler v3
    Replies: 10
    Last Post: 10th July 2007, 20:08

Tags for this Thread

Posting Permissions

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