PDA

View Full Version : need help on hpwm



helmut
- 27th August 2007, 12:08
hi everyone here
i need some help on hpwm
my code is as below:

DEFINE OSC 4
DEFINE CCP1_REG PORTC
DEFINE CCP1_BIT 2
DEFINE CCP2_REG PORTC
DEFINE CCP2_BIT 1
trisb = %11111111
trisc = %00000000
CCP1CON = %00001100
CCP2CON = %00001100
T2CON = %00000100
i var byte
a var byte
L VAR BYTE
M VAR BYTE

main:
L = 0
m = 80
hpwm 1,0,1000
hpwm 2,0,1000
if portb.0 == 1 then
pause 1000
goto LED
endif
goto main
end

LED:
IF PORTB.1 == 1 THEN
pause 500
gosub ledincrease
endif

if portb.2 == 1 then
pause 500
gosub leddecrease
endif

if portb.0 == 1 then
pause 1000
goto motor
else
goto LED
endif


MOTOR:
if portb.1 == 1 then
pause 500
gosub motorincrease
endif
if portb.2 == 1 then
pause 500
gosub motordecrease
endif
if portb.0 == 1 then
pause 1000
goto led
else
goto motor
endif

ledincrease:
L = L + 16
IF (L < 255) THEN
HPWM 1,L,1000
else
if (l >= 255) then
L = 255
hpwm 1,L,1000
endif
endif
return

leddecrease:
L = L - 16
if (l > 0) then
hpwm 1,l,1000
else
if (l <= 0) then
L = 0
hpwm 1,L,1000
endif
endif
return

motorincrease:
m = m + 11
IF (m < 255) THEN
HPWM 2,m,1000
else
if (m >= 255) then
M = 255
hpwm 2,M,1000
endif
endif
return
motordecrease:
m = m - 11
if (m > 0) then
hpwm 2,m,1000
else
if (m <= 0) then
M = 0
hpwm 2,M,1000
endif
endif
return



this program is used to control intensity of 2 application - led or motor
when i high portb.1, the hpwm will increase
and when i high portb.2, the hpwm will decrease.
the problems i face is the hpwm will go from 100% to 0% when i further HIGH portb.1
i wish the hpwm will stop at 100% until i decrease it.
thank for your help~

paul borgmeier
- 28th August 2007, 06:54
L = L + 16
With this code, you are letting L overflow ... If L = 240 and you add 16, you get 0 (not 256). Bytes range from 0 – 255

If L = 0 to start

try something like this for the L increase routines:


IF L < 240 THEN
L = L + 16
ELSE
IF L = 240 THEN L = 255
ENDIF

try something like this for the decrease routines:



IF L > 0 THEN
IF L = 255 THEN
L = 240
ELSE
L = L – 16
ENDIF
ENDIF

You can work out something similar for the m variable. Reply back if you need more hints.

helmut
- 28th August 2007, 15:49
hah! i was tried today....
and i got it...
i wrote

if ( L >= 240) then
hpwm 1,255,1000
else
if (L<240) then
L=L+16
hpwm 1,L,1000

thank you paul....
but i stil have problems with my shifting input