4 x PWM outputs


Closed Thread
Results 1 to 5 of 5

Thread: 4 x PWM outputs

  1. #1
    Join Date
    Oct 2009
    Posts
    583

    Default 4 x PWM outputs

    Hi Guys,

    Been a while since I've dabbled in pic programming, but now need some advice and suggestions for a project I'm toying with.

    I'm looking at developing a 4 channel dew heater controller for use with a telescope. Basic requirement is 4 separate PWM outputs, each controlled by a analogue pot. The frequency of the PWM would be a low 1Hz with the pot controlling the width of the pulse. Any suggestions of a suitable PIC,

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: 4 x PWM outputs

    Hi,
    At such low frequency you can easily get away with a software based PWM scheme which means basically "any" PIC with atleast 8 I/O out of which 4 are analog will do. If you search the forum for Darrels SPWM routine it might give you kickstart in the right direction. Have a look Here for example.

  3. #3
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: 4 x PWM outputs

    Henrik, thanks for the suggestions and link. It's been upwards of 18 months since I last programmed in PBP - rusty ain't the word !

  4. #4
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: 4 x PWM outputs

    Code:
    FOR LoopCount = 1 TO 4              ; Reapeat 4 times
            FOR DutyVar1 = 0 TO 150         ; Fade LED1 UP
                PAUSE 10
                RANDOM  RandVar             
                DutyVar3 = RandVar & $3F    ; Give LED3 a random dutycycle
            NEXT DutyVar1
             
            FOR DutyVar1 = 150 TO 0 STEP -1 ; Fade LED1 Down
                PAUSE 10
                RANDOM  RandVar
                DutyVar2 = RandVar & $3F    ; Give LED2 a random dutycycle
            NEXT DutyVar1
        NEXT LoopCount
    If I follow the example above all the processing (for want of a better word) is done in a loop, so once DutyVar1 has had its value written to the port, it then moves on to DutyVar3 and does the same and then to DutyVar2, then loops back round and does then runs through the loop again adding or subtracting (or inserting a random value), then loops back .... etc etc.

    Is there a way of having all three LEDs running in independent loops so that led1 doesn't have to wait for led3 to be actioned before having the next value written to it ? Or are we entering the realm of your PID routines that Darrel used to help me develop my 4ch reptile thermostat ??

    Cheers

    Malcolm

  5. #5
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: 4 x PWM outputs

    Hi Malcolm,
    Using Darrels SPWM routines is probably easier but basically you can do something like this. (This is untested, meant to show the theory only)

    Code:
    Duty1 VAR BYTE    ' 0-255 = 0-100%
    Duty2 VAR BYTE
    Duty3 VAR BYTE
    Period VAR BYTE
    
    LED1 VAR PortB.0
    LED2 VAR PortB.1
    LED3 VAR PortB.2
    
    Period = 0
    
    PWMStart:
       ' Turn on outputs only if duty is more than 0
       If Duty1 > 0 THEN
          LED1 = 1
       ENDIF
    
       IF Duty2 > 0 THEN
          LED2 = 1
       ENDIF
    
       IF Duty3 > 0 THEN
          LED3 = 1
       ENDIF
    
    PWMLoop:
       ' Increment period
       Period = Period + 1
       
       ' Time to turn off output 1?
       IF Period = Duty1 THEN
          LED1 = 0
       ENDIF
    
       ' Time to turn off output 2?
       IF Period = Duty2 THEN
          LED2 = 0
       ENDIF
    
       ' Time to turn off output 3?
       IF Period = Duty3 THEN
          LED3 = 0
       ENDIF
    
       ' Go and do other stuff needed
       GOSUB DoOtherStuff
    
       IF Period = 0 THEN
          GOTO PWMStart
       ELSE
          GOTO PWMLoop
       ENDIF
    
    END
    
    '--------------------------------------------------------------
    DoOtherStuff:
    ' Do the ADC stuff etc here. 
    
    PAUSEUS 4000    ' Adjust to get desired PWM frequency.
    '--------------------------------------------------------------
    /Henrik.

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