Log in

View Full Version : Help with PWM and duty and integers



JDM160
- 17th March 2005, 01:44
Hi all,

I'm having a hard time making a program run correctly for me. I'm trying to achieve the age old RBG LED fade sequence on my own (I have used several existing programs already).

I'm having a problem with my code where the LEDs are not fading - actually they are off most of the time and simply "pop" on for a split second in sequence.

I believe the problem is that I'm send non-integer values to the "duty" variable in PWM - or am I? I'm so confused as to what 10/3 looks like.. is it 3? Is it 3.33333 is it 333? Argh.

Also, is it necessary for the "duty" variable in PWM to be a integer?

Please don't suggest HPWM, I am trying to sharpen my programming skills with this excercize.

My Pic is 16F684 with PicBasicPro

here is the code I'm using:

-------------------------------------------------------------
DEFINE OSC 8
OSCCON=%01110000 '8 Mhz

CMCON0 = 7
TRISA = %00001000 'set all ports to output

'define ports
green VAR TRISA.0
red VAR TRISA.1
blue VAR TRISA.2

rbgArray var byte[3] ' holds the rgb values in this case the led is rbg
x var byte ' allows PWM to repeat
y var byte ' sets the rgb values
z var byte ' controls speeding up or slowing down state
rainbowVal var word ' the current value to produce rainbow
maxcolors var word ' the number of colors in the rainbow
thirdmax var word
dbthirdmax var word
maxtime var byte ' controlls the minimum speed of the color shifting
time var byte ' number of times to repeat PWM

x = 0
y = 0
z = 1
time = 1
maxtime = 2 ' set the max slow
maxcolors = 300 ' set the rainbow size !must be divisable by 3

thirdmax = maxcolors / 3 ' to simplify equations below
dbthirdmax = 2 * thirdmax ' to simplify equations below

main:
for rainbowval = 1 to maxcolors
gosub rbgrainbow
next
for rainbowval = maxcolors to 1 step - 1
gosub rbgrainbow
next

'this section changes the speed of color change

if (time < maxtime) and (z = 1) then
time = time + 1 'slowing down
else
time = time - 1 'speeding up
endif

if time = maxTime then
z = 2 'when max "slow" is reached, prog switches to speeding up
endif
if time = 1 then
z = 1 'when max "fast" is reached, prog switches to slowing down
endif

goto main


rbgrainbow:

if (rainbowval <= thirdmax) then
rbgarray[0] = ((rainbowval / thirdmax) * 255) 'red ramping up
rbgarray[1] = 0 'green off
rbgarray[2] = ((thirdmax - rainbowval) / thirdmax) * 255 'blue ramping down
endif

if (rainbowval > thirdmax) and (rainbowval <= dbthirdmax) then
rbgarray[0] = ((dbthirdmax - rainbowval) / thirdmax ) * 255 'red ramping down
rbgarray[1] = ((rainbowval - thirdmax) / thirdmax)* 255 'green ramping up
rbgarray[2] = 0 'blue off
endif

if (rainbowval > dbthirdmax) and (rainbowval <= maxcolors) then
rbgarray[0] = 0 'red off
rbgarray[1] = ((maxcolors - rainbowval) / thirdmax) * 255 'green ramping down
rbgarray[2] = ((rainbowval - dbthirdmax) / thirdmax) * 255 'blue ramping up
endif

gosub ledpwm

return

ledPWM:

for x = 1 to time

For y = 0 to 2
Select Case y
Case 0
pwm red, rbgarray[y], 1
case 1
pwm green, rbgarray[y], 1
case 2
pwm blue, rbgarray[y], 1
end select
next

next

return
--------------------------------------------------------

can anyone tell me how to make this work! Thanks

a.majid
- 23rd March 2005, 05:50
Hi JDM160

'define ports
green VAR TRISA.0
red VAR TRISA.1
blue VAR TRISA.2

Above is your intial code.
TRISA is data direction contrl register for PORTA.
You should use PORTA.0,PORTA.1 and PORTA.2.
TRISA can not be use as output port.

a.majid

NavMicroSystems
- 23rd March 2005, 11:38
JDM,

How about having a look at the PBP Manual ?

JDM160
- 24th March 2005, 07:03
Hi JDM160

'define ports
green VAR TRISA.0
red VAR TRISA.1
blue VAR TRISA.2

Above is your intial code.
TRISA is data direction contrl register for PORTA.
You should use PORTA.0,PORTA.1 and PORTA.2.
TRISA can not be use as output port.

a.majid

Really? I've gotten a similar code to work using the above port codes.. that's strange.