PDA

View Full Version : Tidying Up PWM Routine



Tissy
- 20th February 2005, 16:25
I have a piece of code which does PWM on Consecutive outputs PORTC.0-4. However the code seems quite long (taking up unnecessary code space on the PIC). I'm sure it can be shortened, but a little unsure how as each of the variables (LED Colours) have been defined. I need to leave the other bits (getout) in which relate to an interupt routine. Can it be shortened ?

Many thanks,

Red VAR PORTC.0 ' All LEDs
Green VAR PORTC.1 ' Connected between
Blue VAR PORTC.2 ' RC pins and ground
UV VAR PORTC.3 ' via resistor
White VAR PORTC.4 '

Bright VAR byte
getout VAR byte

FadeAll:
'FadeRED
Low White
bright = 0
while (Bright !=255) and (GetOut=0)
PWM Red,Bright,3
bright = bright + 1
wend
while (bright !=0) and (getout = 0)
PWM Red,Bright,3
bright=bright - 1
wend
'FadeGreen
bright = 0
while (Bright !=255) and (GetOut=0)
PWM Green,Bright,3
bright = bright + 1
wend
while (bright !=0) and (getout = 0)
PWM Green,Bright,3
bright=bright - 1
wend
'FadeBlue
bright = 0
while (Bright !=255) and (GetOut=0)
PWM Blue,Bright,3
bright = bright + 1
wend
while (bright !=0) and (getout = 0)
PWM Blue,Bright,3
bright=bright - 1
wend
'FadeUV:
bright = 0
while (Bright !=255) and (GetOut=0)
PWM UV,Bright,3
bright = bright + 1
wend
while (bright !=0) and (getout = 0)
PWM UV,Bright,3
bright=bright - 1
wend
'FadeWhite:
bright = 0
while (Bright !=255) and (GetOut=0)
PWM White,Bright,3
bright = bright + 1
wend
while (bright !=0) and (getout = 0)
PWM White,Bright,3
bright=bright - 1
wend
return

mister_e
- 20th February 2005, 23:15
try this and post result


PwmOnLed var byte
Bright VAR byte
getout var bit

FadeAll:
For pwmonled = 8 to 12
bright = 0
while (Bright !=255) and (getout=0)
PWM pwmonled,Bright,3
bright = bright + 1
wend
while (bright !=0) and (getout=0)
PWM pwmonled,Bright,3
bright=bright - 1
wend
next
goto fadeall

Tissy
- 20th February 2005, 23:39
Yup, that works great and a lot shorter, you're the man !!

Now the question so i can learn how you did it.

How does the PWM command know to output on pins PORTC.0-4. I'm guessing its to do with the 8 - 12 you have placed in the for next loop. But if that were true, why is it not outputing to pins PORTB.0-4.

I base that on the following:

0-7 = PORTA.0 - 7
8-14 = PORTB.0 - 7
15-21 = PORTC.0 - 7

Thanks again.

Steve

mister_e
- 20th February 2005, 23:52
As i remind you have a 40Pins PIC ?!?

look into your PBP manual section 4.11 page 26

Tissy
- 21st February 2005, 00:26
It so simple when you read it !!!

Knowing that will hopefully make things a little easier when i come to change the program from using a PIC16C876 to a PIC16C628A.

Thanks again,

Steve