PDA

View Full Version : shifting problem



helmut
- 29th August 2007, 20:25
hi everyone here, i'm newbie in pbp, i wrote a program so that when i shift 1 bit to left in portb, it will increase the pwm, while shift 1 bit to the right in portb will decrease the pwm. when i run my project, it seem doesnt run well, somemtimes it work, sometimes it's not. can you please let me know what problems i have? thank for your help.
my code shown below:


DEFINE OSC 4
DEFINE CCP1_REG PORTC
DEFINE CCP1_BIT 2
DEFINE CCP2_REG PORTC
DEFINE CCP2_BIT 1
trisb = %11111111
trisc = %11110000
CCP1CON = %00001100
CCP2CON = %00001100
T2CON = %00000100
L VAR BYTE
M VAR BYTE
a var byte
b var byte
c var byte
d var byte
e var byte
f var byte
g var byte
h var byte
m = 80
l = 0
a = %00000001
b = %00000010
c = %00000100
d = %00001000
e = %00010000
f = %00100000
g = %01000000
h = %10000000

main:
hpwm 1,0,1000
hpwm 2,0,1000
if portc.7 == 1 then 'selection button press, go to led
pause 850
goto LED
endif
goto main
end

'---------------------------------------------------------------------------

LED:
low portc.5
high portc.4
gosub LEDCOMPARE
if portc.7 == 1 then
pause 850
goto motor
else
goto LED
endif

'--------------------------------------------------------------------------

MOTOR:
low portc.4
high portc.5
GOSUB MOTORCOMPARE
if portc.7 == 1 then
pause 850
goto led
else
goto motor
endif

'-----------------------------------------------------------------------------
LEDCOMPARE:

if (portb == a) then
pause 500
if (portb == b) then
gosub ledincrease
endif
else
if (portb == h) then
gosub leddecrease
endif
endif

if (portb == b) then
pause 500
if (portb == c) then
gosub ledincrease
endif
else
if (portb == a) then
gosub leddecrease
endif
endif

if (portb == c) then
pause 500
if (portb == d) then
gosub ledincrease
endif
else
if (portb == b) then
gosub leddecrease
endif
endif

if (portb == d) then
pause 500
if (portb == e) then
gosub ledincrease
endif
else
if (portb == c) then
gosub leddecrease
endif
endif

if (portb == e) then
pause 500
if (portb == f) then
gosub ledincrease
endif
else
if (portb == d) then
gosub leddecrease
endif
endif

if (portb == f) then
pause 500
if (portb == g) then
gosub ledincrease
endif
else
if (portb == e) then
gosub leddecrease
endif
endif

if (portb == g) then
pause 500
if (portb == h) then
gosub ledincrease
endif
else
if (portb == f) then
gosub leddecrease
endif
endif

if (portb == h) then
pause 500
if (portb == a) then
gosub ledincrease
endif
else
if (portb == g) then
gosub leddecrease
endif
endif
return

'--------------------------------------------------------------------------

MOTORCOMPARE:
if (portb == a) then
pause 500
if (portb == b) then
gosub MOTORincrease
endif
else
if (portb == h) then
gosub MOTORdecrease
endif
endif

if (portb == b) then
pause 500
if (portb == c) then
gosub MOTORincrease
endif
else
if (portb == a) then
gosub MOTORdecrease
endif
endif

if (portb == c) then
pause 500
if (portb == d) then
gosub MOTORincrease
endif
else
if (portb == b) then
gosub MOTORdecrease
endif
endif

if (portb == d) then
pause 500
if (portb == e) then
gosub MOTORincrease
endif
else
if (portb == c) then
gosub MOTORdecrease
endif
endif

if (portb == e) then
pause 500
if (portb == f) then
gosub MOTORincrease
endif
else
if (portb == d) then
gosub MOTORdecrease
endif
endif

if (portb == f) then
pause 500
if (portb == g) then
gosub MOTORincrease
endif
else
if (portb == e) then
gosub MOTORdecrease
endif
endif

if (portb == g) then
pause 500
if (portb == h) then
gosub MOTORincrease
endif
else
if (portb == f) then
gosub MOTORdecrease
endif
endif

if (portb == h) then
pause 500
if (portb == a) then
gosub MOTORincrease
endif
else
if (portb == g) then
gosub MOTORdecrease
endif
endif
return

'-----------------------------------------------------------------------------

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

leddecrease:
high portc.4
l = l - 16
if (l <= 0) then
hpwm 1,0,1000 '
else
if (l > 0) then
hpwm 1,l,1000
endif
endif
return

'--------------------------------------------------------------------------

motorincrease:
high portc.5
if (m >= 245) then
hpwm 2,255,1000
ELSE
IF (m < 245) THEN
m = m + 11
HPWM 2,m,1000
endif
ENDIF
return

motordecrease:
high portc.5
if (m <= 80) then
hpwm 2,0,1000
ELSE
if (m > 80) then
m = m - 11
hpwm 2,m,1000
endif
ENDIF
return

paul borgmeier
- 31st August 2007, 06:11
ledincrease:
high portc.4
l = l + 16
if (l >= 240 ) then
hpwm 1,255,1000
else
IF (L < 240) THEN
HPWM 1,L,1000
endif
endif
return


Let's just look at this subroutine .. what happens if L = 226 coming into this routine. L is changed to 240, HPWM is set to 255, and then you exit. What happens if you return to this subroutine again. L is increased from 240 to 0 (240+16 = 256 = 1 0000 0000, but as a byte = 0000 0000). HPWM is set to 0. :(

See my reply in your earlier related thread for the answer (the answer: do not update L if L is equal to 240)
http://www.picbasic.co.uk/forum/showthread.php?t=6989
You also should check the decrease subroutine - I did not check it this time.