Variable PWM on 2 Channels using software.


Closed Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2005
    Location
    Essex, UK
    Posts
    154

    Default Variable PWM on 2 Channels using software.

    I basically have two working routines and am having serious difficulty on the third.

    One is a variable PWM on a single coloured LED, which when you push a button, the LED gets brighter and then dimmer. Just like a standard house light dimmer switch.

    The Second is a RGB sequence using PWM and three LEDs.

    The third i would like to vary the brightness of TWO LEDs at the same time. Same as the first routine just with TWO LEDs.

    Now as the PIC16F628A can only handle PWM on one channel at a time, i have tried modifying the RGB sequence to perform this task to no avail. I am thinking i need to do it along the lines of an Array, but to be honest am getting it round my neck.

    I have posted the two routines below, along with my attempt which kind of works, but not properly. The LED Dims on one LED but not the other and you can't see the level of dim until you release the switch.

    There is a lot more to this full code, but hopefully this will be enqough to explain what i am trying to achieve. Can some offer help please? I have been working on this for weeks and its driving me mad !!

    [Fading Single LED]
    VariableSolidColour:
    PWM onled,bright,10
    If FuncSwitch=0 Then
    While FuncSwitch = 0
    If Flag = 0 Then
    If Bright <255 Then Bright = Bright + 1
    If bright = 255 Then Gosub flash
    EndIf
    If Flag = 1 Then
    If Bright >0 Then Bright = Bright - 1
    EndIf
    PWM onled,Bright,10
    WEnd
    Flag = Flag + 1
    EndIf
    Return
    [/Fading Single LED]

    [RGB Sequence]
    RGBSequence:
    rainbowval=0
    While (rainbowval !=255) and (getout=0)
    for RGBDelay = 1 to 10
    gosub RGBRainbow
    next RGBDelay
    rainbowval=rainbowval+1
    Wend
    While rainbowval and (getout=0)
    for RGBDelay = 1 to 10
    gosub RGBRainbow
    next RGBDelay
    rainbowval=rainbowval-1
    wend
    Return


    RGBRainbow:
    rbg=0
    While (rbg<=2) and (getout=0)
    section = ((rainbowval + ((rbg * 2)*delta))/ delta) // 6 ' this gives
    ' what section
    ' it is in

    Select case section
    case 0
    rbgarray[rbg] = (rainbowval // delta)*6 ' this is how far it
    ' has gone in its
    ' section
    case 1
    rbgarray[rbg] = 255
    case 2
    rbgarray[rbg] = 255
    case 3
    rbgarray[rbg] = 255 - ((rainbowval //delta)*6)
    case 4
    rbgarray[rbg] = 0
    case 5
    rbgarray[rbg] = 0
    case 6 'this one is for pics bad math
    rbgarray[rbg] = 0
    end select
    rbg=rbg+1
    wend
    Gosub ledpwm
    Return


    ledPWM:
    y=0
    While (y<=2) and (getout=0)
    Select Case y
    Case 0
    PWM red, rbgarray[y], 1
    Gosub ResetLEDs
    Case 1
    PWM green, rbgarray[y], 1
    Gosub ResetLEDs
    case 2
    PWM blue, rbgarray[y], 1
    Gosub ResetLEDs
    End Select
    y=y+1
    wend
    Return
    [/RGB Sequence]

    [My Attempt]
    VariableDualColour:
    ' PWM onled,Brightx,1
    ' PWM secondled,brightx,1
    Gosub pwmdual
    If FuncSwitch=0 Then
    While FuncSwitch = 0
    If Flag = 0 Then
    If Bright <255 Then Bright = Bright + 1
    If bright = 255 Then Gosub flash
    EndIf
    If Flag = 1 Then
    If Bright >0 Then Bright = Bright - 1
    EndIf
    gosub pwmdual
    ' PWM onled,Brightx,1
    ' PWM secondled,brightx,1
    WEnd
    Flag = Flag + 1
    EndIf
    Return

    PWMDual:
    For z = 0 to 1
    Select Case z
    Case 0
    pwm Onled, Bright, 10
    case 1
    pwm secondled, bright, 10
    end select
    next
    Return
    [/My Attempt]

    Many thanks for any help,

    Steve

  2. #2
    Join Date
    Feb 2005
    Location
    Essex, UK
    Posts
    154


    Did you find this post helpful? Yes | No

    Default

    The Code didn't format properly, hopefully this will make it easier to read !!

    Fading LED
    Code:
    VariableSolidColour:
        PWM onled,bright,10
            If FuncSwitch=0 Then 
                While FuncSwitch = 0
                If Flag = 0 Then
                If Bright <255 Then Bright = Bright + 1
                If bright = 255 Then Gosub flash
            EndIf 
                If Flag = 1 Then
                If Bright >0 Then Bright = Bright - 1
            EndIf 
        PWM onled,Bright,10 
            WEnd
                Flag = Flag + 1
            EndIf
    Return
    RGB Sequence
    Code:
    RGBSequence:
        rainbowval=0
        While (rainbowval !=255) and (getout=0)
            for RGBDelay = 1 to 10
            gosub RGBRainbow
            next RGBDelay
            rainbowval=rainbowval+1
        Wend
        While rainbowval and (getout=0)
            for RGBDelay = 1 to 10
            gosub RGBRainbow
            next RGBDelay
            rainbowval=rainbowval-1
        wend
    Return
    
    
    RGBRainbow:
        rbg=0
        While (rbg<=2) and (getout=0)
            section = ((rainbowval + ((rbg * 2)*delta))/ delta) // 6 ' this gives
                                                                     ' what section
                                                                     ' it is in
    
            Select case section
                case 0
                     rbgarray[rbg] = (rainbowval // delta)*6 ' this is how far it
                                                             ' has gone in its
                                                             ' section
                case 1
                     rbgarray[rbg] = 255
                case 2
                     rbgarray[rbg] = 255
                case 3
                     rbgarray[rbg] = 255 - ((rainbowval //delta)*6)
                case 4
                     rbgarray[rbg] = 0
                case 5
                     rbgarray[rbg] = 0
                case 6 'this one is for pics bad math
                     rbgarray[rbg] = 0
            end select
        rbg=rbg+1
        wend
        Gosub ledpwm
    Return
    
    
    ledPWM:
        y=0
        While (y<=2) and (getout=0)
            Select Case y
                Case 0
                     PWM red, rbgarray[y], 1
                     Gosub ResetLEDs
                Case 1
                     PWM green, rbgarray[y], 1
                     Gosub ResetLEDs
                case 2
                     PWM blue, rbgarray[y], 1
                     Gosub ResetLEDs
            End Select
        y=y+1
        wend
    Return
    My Attempt
    Code:
    VariableDualColour:
    '    PWM onled,Brightx,1
    '    PWM secondled,brightx,1
        Gosub pwmdual
           If FuncSwitch=0 Then 
                While FuncSwitch = 0
                If Flag = 0 Then
                If Bright <255 Then Bright = Bright + 1
                If bright = 255 Then Gosub flash
            EndIf 
                If Flag = 1 Then
                If Bright >0 Then Bright = Bright - 1
            EndIf 
        gosub pwmdual
    '    PWM onled,Brightx,1
    '    PWM secondled,brightx,1
            WEnd
                Flag = Flag + 1
            EndIf
    Return
    
    PWMDual:
        For z = 0 to 1
            Select Case z
        Case 0
            pwm Onled, Bright, 10
        case 1
            pwm secondled, bright, 10
        end select
        next 
    Return
    Many thanks for any help,

    Steve

  3. #3
    cosmomen's Avatar
    cosmomen Guest


    Did you find this post helpful? Yes | No

    Default

    Hi,

    I have the same problem . any progress regarding this dual LED fading issue?
    Thank you!

  4. #4
    Join Date
    Aug 2006
    Posts
    14


    Did you find this post helpful? Yes | No

    Default

    If that's all your PIC is doing, and you don't mind doing it the easy way, then take a look at this.

    http://www.pbpgroup.com/modules/wfse...p?articleid=12

    That'll do exactly what you need, all in software. (You just won't have many cycles left over for other things).

Similar Threads

  1. Variable PWM PIC18F2431
    By trr1985 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 12th June 2009, 06:03
  2. PWM 'channels ...what's that all about then!
    By HankMcSpank in forum General
    Replies: 5
    Last Post: - 23rd April 2009, 16:49
  3. Help with sound command in 2 programs
    By hyperboarder in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 5th July 2007, 20:36
  4. Microcontroller with 2 way paging application problem
    By oneohthree in forum mel PIC BASIC Pro
    Replies: 30
    Last Post: - 20th April 2007, 17:27
  5. pwm to pin using variable
    By wsmrite in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 7th April 2006, 17:08

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