PDA

View Full Version : 4 x PWM outputs



Scampy
- 6th February 2012, 12:31
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,

HenrikOlsson
- 6th February 2012, 14:37
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 (http://darreltaylor.com/DT_INTS-14/SPWM.html) for example.

Scampy
- 6th February 2012, 21:04
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 !

Scampy
- 6th February 2012, 23:08
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

HenrikOlsson
- 7th February 2012, 06:40
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)



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.