Problem with bit shifting LEDs
~Coffee~
Posts: 2
so I have 16 LEDs lined up on pins 0 through 15 and two buttons on pin 22 and 23
the program taht I wrote out would bit shift and move the lit LED over one way or another depending on which button I pushed... I ran into a few problems so I improvised and added two extra methods to define and everything worked smoothly except that when I started the program and moved it to the right first, it would start on pin 0 instead of pin 15 like it should have. Ive tried many different things to get this working correctly but the when I added another BIT to one of the defenitions, it fixed the problem, but then when I would go LEFT as soon as it got to pin 15, it wouldnt switch over to pin 0 again.....
here is that code
_xinfreq = 5_000_000·····················
· _clkmode = xtal1 + pll16x
PUB Def
··· outa[noparse][[/noparse]0..15] := %10000000_00000000_0··====·I added this·to fix the problem but then got another problem
··· Index
PUB Def2
··· outa[noparse][[/noparse]15..0] := %10000000_00000000
··· Index
PUB Index
··· dira[noparse][[/noparse]22] := 0
··· dira[noparse][[/noparse]23] := 0
··· ina[noparse][[/noparse]22] := 0
··· ina[noparse][[/noparse]23] := 0
· repeat
··· if ina[noparse][[/noparse]22] == 1
····· Main
··· elseif ina[noparse][[/noparse]23] == 1
····· Oppo
·
PUB Main
dira[noparse][[/noparse]0..15] := %11111111_11111111
··· repeat
····
····· repeat while ina[noparse][[/noparse]22] == 1
······· waitcnt(clkfreq/10 + cnt)
······· outa[noparse][[/noparse]0..15] >>= 1
······· If outa[noparse][[/noparse]0..15] == %00000000_00000000
········· Def
······· elseifnot outa[noparse][[/noparse]15..0] == %00000000_00000000
········· Index
PUB Oppo
dira[noparse][[/noparse]15..0] := %11111111_11111111······
······
· repeat
··
··· repeat while ina[noparse][[/noparse]23] == 1
····· waitcnt(clkfreq/10 + cnt)
····· outa[noparse][[/noparse]15..0] >>= 1
····· If outa[noparse][[/noparse]15..0] == %00000000_00000000
······· Def2
····· elseifnot outa[noparse][[/noparse]0..15] == %00000000_00000000
········· Index
the program taht I wrote out would bit shift and move the lit LED over one way or another depending on which button I pushed... I ran into a few problems so I improvised and added two extra methods to define and everything worked smoothly except that when I started the program and moved it to the right first, it would start on pin 0 instead of pin 15 like it should have. Ive tried many different things to get this working correctly but the when I added another BIT to one of the defenitions, it fixed the problem, but then when I would go LEFT as soon as it got to pin 15, it wouldnt switch over to pin 0 again.....
here is that code
_xinfreq = 5_000_000·····················
· _clkmode = xtal1 + pll16x
PUB Def
··· outa[noparse][[/noparse]0..15] := %10000000_00000000_0··====·I added this·to fix the problem but then got another problem
··· Index
PUB Def2
··· outa[noparse][[/noparse]15..0] := %10000000_00000000
··· Index
PUB Index
··· dira[noparse][[/noparse]22] := 0
··· dira[noparse][[/noparse]23] := 0
··· ina[noparse][[/noparse]22] := 0
··· ina[noparse][[/noparse]23] := 0
· repeat
··· if ina[noparse][[/noparse]22] == 1
····· Main
··· elseif ina[noparse][[/noparse]23] == 1
····· Oppo
·
PUB Main
dira[noparse][[/noparse]0..15] := %11111111_11111111
··· repeat
····
····· repeat while ina[noparse][[/noparse]22] == 1
······· waitcnt(clkfreq/10 + cnt)
······· outa[noparse][[/noparse]0..15] >>= 1
······· If outa[noparse][[/noparse]0..15] == %00000000_00000000
········· Def
······· elseifnot outa[noparse][[/noparse]15..0] == %00000000_00000000
········· Index
PUB Oppo
dira[noparse][[/noparse]15..0] := %11111111_11111111······
······
· repeat
··
··· repeat while ina[noparse][[/noparse]23] == 1
····· waitcnt(clkfreq/10 + cnt)
····· outa[noparse][[/noparse]15..0] >>= 1
····· If outa[noparse][[/noparse]15..0] == %00000000_00000000
······· Def2
····· elseifnot outa[noparse][[/noparse]0..15] == %00000000_00000000
········· Index
Comments
(1) You use RECURSION when you shouldn't! I don't know where this funny habbit comes from.. In fact I have seen two beginners now doing this.
Look: When you are in the endles loop of MAIN or OPPO you stack INDEX upon it. Think of a different way to RETURN from those routines!
(2) Be always consistent, whether to use OUTA[noparse][[/noparse]0..15] or OUTA[noparse][[/noparse]15..0]; those are different and you will get (and you have gotten ) absolutely confused when using both in the same program.
(3) Don't check again for the complement in an ELSEIF; this is bad practice, as it will confuse you as well. It is absolutly unnecessary - use just ELSE.
(4) Don't set INA!
(5) You don't need to set DIRAs to zero; but this does no harm.
After considering all this, your program will most likely run
Much luck!