Why does this work?

geezer88

Senior Member
I'm playing with a Picaxe 18M2. I've got eight leds connected to the port B pins. They are arranged with current limiting resistors to 5+, then to the output, so when the output is low, the light comes on. There are no other connections to the 18M2 except needed for power and programming. The program below works as expected, but I'm not sure why:

Code:
symbol cntr = b0

main:

	outpinsb = 255			;raise all outputs to turn off leds

lupe:
 
for cntr = 0 to 7
	
	low cntr				;drop output to turn on single led
	pause 50
	high cntr
	
next cntr

for cntr = 7 to 0 step -1	;do it all backwards
	
	low cntr
	pause 50
	high cntr
	
next cntr

goto lupe					;repeat cycle
end
The manual says on a 18M2 that individual pins should be addressed as B.3 or whatever, but when I tried to make a variable in place of the 3, the compiler gives a syntax error. So, I tried the simple high 3 and that does work. How does the compiler know that it is port B that I'm referring to? I am quite confused by the individual pin outputting inspite of reading the manual several times.

thanks,
tom
 

BeanieBots

Moderator
How does the compiler know that it is port B that I'm referring to?
By luck more than anything else;)
The assignments "B.0 - B.7" are nothing more than the equivalent of Symbol deffinitions. Luck happens that "B.0" = 0, "B.1" = 1 etc, etc.
If you were using a different port, it would have got it wrong!

Try it out.
b0 = C.3
sertxd (#b0)
 

geezer88

Senior Member
So what would the proper way to address output pins for portC if I want to only change one pin, but I want to use a variable, not a constant?

thanks,
tom
 

BeanieBots

Moderator
Untested, but I'd try something like this:-

In keeping with your existing code.
Code:
.......
for cntr = C.0 to C.7
	
	low cntr				;drop output to turn on single led
	pause 50
	high cntr
	
next cntr
.....
 

geezer88

Senior Member
YeeHaw!

You nailed it. Thanks. I had tried something similar, but was trying to just append the 0 ....7 variable to 'c.' or 'b.', not the whole works. I would never have thought you could do this based on the docs.

thanks again,
tom
 

geezer88

Senior Member
It just occurred to me that the ability to increment a pin number might work across ports. At least in the simulator, it does. For example:

main:
for b0 = b.0 to c.3
high b0
pause 10
low b0
next b0

It steps right through the B port, then begins in on the C port. Neat.
tom
 

Technical

Technical Support
Staff member
As far as the compiler is concerned for M2 and X2 parts, B.0 is just the number 0, C.0 the number 8 etc.

so
high C.7 : high 15
low B.0 : low 0

for b0 = 0 to 15 : for b0 = B.0 to C.7

are all exacty the same.
However it is generally easier to write/understand when using port.pin
 
Last edited:

westaust55

Moderator
Hmm, while a bit off the original topic, in view of Technical's comment I tried some test in the PE simulator and also tried some selecting the 28X1

In the PE for the 28X1:
HIGH 8 is the same as HIGH 0 and turns on output 0 (port B pin 0),
HIGH 9 is the same as HIGH 1 and turns on output 1 (port B pin 1),
etc

Probably not an intended event but the PE does not capture the situation, maybe just AND's the pin number with 7 ?

For an X2 part the PE simulator works just as Technical states when a pin number higher than 7 is used.
 

hippy

Technical Support
Staff member
And while "For b0 = B.0 To C.7" will work, on those PICAXE which have A.x or D.x pins, "For b0 = A.0 To B.7" etc won't always behave as expected.

It's best to not cross port boundaries; use a separate FOR-NEXT for each port set. If there's common code then put that in a subroutine and call from within each FOR-NEXT, or simply duplicate it.

Perceived savings are often less than imagined and get into the habit of crossing port boundaries and it may come back to 'bite you', create a lot of head-scratching when that doesn't work.
 

geezer88

Senior Member
And while "For b0 = B.0 To C.7" will work, on those PICAXE which have A.x or D.x pins, "For b0 = A.0 To B.7" etc won't always behave as expected.

It's best to not cross port boundaries; use a separate FOR-NEXT for each port set. If there's common code then put that in a subroutine and call from within each FOR-NEXT, or simply duplicate it.
I'm really appreciating this discussion. It seems that no matter how good a computing product's documentation is, there are always a bunch of things that don't make it into the official doc. I've started my own "PicAxe Tricks" to help retain some of this stuff. It seems that my 62 year old brain needs help in the non volatile department. (maybe I'm up against the 100k cycle limit)

tom
 

John West

Senior Member
I'm with you there. My short term memory is shot. Long term memory fading. I make a list to remind myself of what to do, then misplace the list and can't quite recall where it might be. Such is life. We soldier on. Memory sticks are our friends. :)
 
Top