PDA

View Full Version : simple xmas light project not behaving....



comwarrior
- 28th October 2010, 01:19
ok, simple little xmas lights project and i been at it for 2 weeks and it's not doing what it's supposed to...

it's a simple 3 led software simplified PWM.... system....

the first light 'sequence' is to fade from red into green then into blue then back to red... and the cycle repeats...

I'm using 18F4550's with intosc... PBP 2.60A
it brings up the red, then the green and the fades out the green and goes back to red... that's when i can get it not to power both green and red solidly at the same time...
it's powered from a single lipol cell...

Can someone take a look at my code and tell me if i've gone completely mad?:confused:
Thanks

Be aware, on this version i set the chip to use an external 20MHz resonator and the BOR is set wrong (corrected at programming)



@ __CONFIG _CONFIG1L, _PLLDIV_5_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_1_1L
@ __CONFIG _CONFIG1H, _FOSC_HS_1H & _FCMEN_ON_1H & _IESO_OFF_1H
@ __CONFIG _CONFIG2L, _PWRT_ON_2L & _BOR_ON_2L & _BORV_1_2L & _VREGEN_OFF_2L
@ __CONFIG _CONFIG2H, _WDT_ON_2H & _WDTPS_32768_2H
@ __CONFIG _CONFIG3H, _MCLRE_OFF_3H & _PBADEN_OFF_3H
@ __CONFIG _CONFIG4L, _STVREN_ON_4L & _LVP_ON_4L & _ICPRT_OFF_4L & _XINST_ON_4L & _DEBUG_OFF_4L

Define OSC 20 ' Set clock speed

TRISB = %00000000
PORTB = %00000000

RED VAR PORTB.7
GREEN VAR PORTB.6
BLUE VAR PORTB.5
REDSCALE VAR WORD
GREENSCALE VAR WORD
BLUESCALE VAR WORD
Aloop VAR byte
Bloop VAR BYTE
DUTY var BYTE
clear
REDSCALE = 1
BLUESCALE = 1
GREENSCALE = 1
DUTY = 1

gosub powerupred
MAIN:
gosub powerupgreen
gosub powerdownred
gosub powerupblue
gosub powerdowngreen
gosub powerupred
gosub powerdownblue
GOTO MAIN

POWERUPRED:
for Aloop = 1 to DUTY
FOR Bloop = 1 to REDSCALE
HIGH RED
pause Aloop
LOW RED
pause (duty-Aloop)
NEXT Bloop
NEXT Aloop
HIGH RED
return

POWERDOWNRED:
for Aloop = 1 to duty
FOR Bloop = 1 to REDSCALE
HIGH RED
pause (duty-Aloop)
LOW RED
pause Aloop
NEXT Bloop
NEXT Aloop
return

POWERUPGREEN:
for Aloop = 1 to duty
FOR Bloop = 1 to GREENSCALE
HIGH GREEN
pause Aloop
LOW GREEN
pause (duty-Aloop)
NEXT Bloop
NEXT Aloop
HIGH GREEN
return

POWERDOWNGREEN:
for Aloop = 1 to duty
FOR Bloop = 1 to GREENSCALE
HIGH GREEN
pause (duty-Aloop)
LOW GREEN
pause Aloop
NEXT Bloop
NEXT Aloop
return

POWERUPBLUE:
for Aloop = 1 to duty
FOR Bloop = 1 to BLUESCALE
HIGH BLUE
pause Aloop
LOW BLUE
pause (duty-Aloop)
NEXT Bloop
NEXT Aloop
HIGH BLUE
return

POWERDOWNBLUE:
for Aloop = 1 to duty
FOR Bloop = 1 to BLUESCALE
HIGH BLUE
pause (duty-Aloop)
LOW BLUE
pause Aloop
NEXT Bloop
NEXT Aloop
return

Max Power
- 28th October 2010, 21:03
look into Darrel Taylor's 3 channel software PWM code - it's interrupt based. I'm not sure if it's ready to run on a 18F4550. Your main loop would look something like this:

FOR x = 0 to 255 STEP 5
red = x 'REd fade on,
PAUSE delay
NEXT x

FOR x = 0 to 255 STEP 5
green = x 'green fade on,
PAUSE delay
NEXT x

ect...

Jerson
- 29th October 2010, 01:56
The code you showed has some problems for sure.

The variables Duty and Scale are set to 1 at start. Then you do this

POWERUPRED:
for Aloop = 1 to DUTY
FOR Bloop = 1 to REDSCALE
HIGH RED
pause Aloop
LOW RED
pause (duty-Aloop)
NEXT Bloop
NEXT Aloop
HIGH RED
return

Rewriting with your declared values it looks like this

POWERUPRED:
for Aloop = 1 to 1
FOR Bloop = 1 to 1
HIGH RED
pause Aloop
LOW RED
pause (1-Aloop)
NEXT Bloop
NEXT Aloop
HIGH RED
return


Perhaps you see the problem now.

Regards

comwarrior
- 30th October 2010, 00:19
Jerson;
Ok thats weird...
The scale var is their if i want to slow down the change... so setting this to 1 is normal
The duty var is actually set to 100 but for some reason it's shown as 1...
Where did my zero's go?

Max;
I'm aware of DT's 3 channel PWM, however, the 18F4550 is only the development hardware i use for testing everything...
I will be buying some lower grade pic's to do the job... i want approximately 5-10 of these running different patterns around the house both inside and out...