I have done this before. You just need to put the 2 PWM commands side by side in a for next and do it for one cycle. Adjusing the 3 that I have there will keep the same duty cycle for longer or shorter. Your eye can't tell the difference. This circuit would brighten with the push of one button and dim with the push of another. It would time out after the counter got to 1200 and they would both go out. Not energy efficient but this was one of the first programs I wrote for my thesis. It was on a 16f84a
DUTY VAR BYTE
sleeptime VAR WORD
i VAR BYTE
Input PORTB.0 'Decrease light and time button
Input PORTB.1 'Increase light and time button
DUTY = 127 'Set LED to half brightness
sleeptime = 0 'Set sleeptime to 0
START:
For i = 1 to 3 'Go through each duty cycle 3 times or 30ms in time
PWM PORTB.4,DUTY,1 'Output to LED
PWM PORTB.5,DUTY,1 'Output to LED
Next i
IF (PORTB.1 = 1) AND (DUTY > 0) Then 'Check to see if light decrease is pushed
Duty = Duty - 1 'Make LED dimmer by 1 count
EndIF
IF (PORTB.2 = 1) AND (DUTY < 255) Then 'Check to see if light increase is pushed
DUTY = DUTY + 1 'Make LED brighter by 1 count
EndIF
GoSub testsleep 'Check to see if sleep creteria is met; no inputs for delay time
GoTo START 'Return to start
testsleep:
IF (PORTB.1 = 1) AND (sleeptime = 1200) Then
sleeptime = 0 'Reset sleeptime if an input is high and sleeptime is already at max
duty = 127 'On input set the LED's to half brightness
EndIF
IF (PORTB.2 = 1) AND (sleeptime = 1200) Then
sleeptime = 0 'Reset sleeptime if an input is high and sleeptime is already at max
duty = 127 'On input set the LED's to half brightness
EndIF
IF (PORTB.1 = 0) AND (PORTB.2 = 0) AND (sleeptime < 1200) Then
sleeptime = sleeptime + 1 'Increment sleeptime if no inputs
EndIF
IF sleeptime = 1200 Then 'If sleeptime gets to 1200 turn off LED's
Duty = 0
EndIF
Return
Bookmarks