08m2 to 18m2 problem rewriting code simple problem

binary1248

Senior Member
I am converting some simple ode from 08m2 written years ago to 18m2 chip.
My 1st if statement fails on 18m2 but works on 08m2.
What gives, I will try to attach code.
Some other problems exists but I need to get by this if statement error.

#picaxe 18M2 ;define chip type
;
symbol redled=b.7 ;pin 13 on chip Output pins for led lights
symbol yelled=b.6 ;pin 12 on chip
symbol grnled=b.5 ;pin 11 on chip

symbol inblck=b.0 ;input pin 6 on chip 1st block sense
symbol outblck=b.1 ;input pin 7 on chip 2nd block sense

Start:
input b.0,b.1 ;make ports b.0 & b.1 inputs
output b.5,b.6,b.7
;little startup test seq lamps
gosub testblnk

Main:
; Find 1st block entry switch triped
;note for testing reversed polarty allowing bb test buttons
red: if pin inblck= 1 then ;physical pin 6
high redled ;turn on red
low yelled ;turnoff yellow
low grnled ;turn off grn
endif

if outblck =1 then
goto yellow ;process yellow with exit timer
else goto red

endif
 
Last edited:

PhilHornby

Senior Member
Code:
symbol inblck=b.0 ;input pin 6 on chip 1st block sense
symbol outblck=b.1 ;input pin 7 on chip 2nd block sense
Code:
red:    if pin inblck= 1 then  ;physical pin 6
but may well have meant :-
Rich (BB code):
symbol inblck=pinb.0 ;input pin 6 on chip 1st block sense
symbol outblck=pinb.1 ;input pin 7 on chip 2nd block sense
and
Rich (BB code):
red: if  inblck = 1 then ;physical pin 6
 

binary1248

Senior Member
Thanks, that fixed the problem. Don't understand why inputs have to be pinb.0 yet outputs are simply b.6. Maybe I will try as pinb.6 for consistency later, but it is working.
Many thanks.
 

binary1248

Senior Member
Changed symbol outputs to pinb.6, pinb.7, pinb.8 so everything is hunky dorory now.
symbol redled=pinb.7 ;pin 13 on chip
symbol yelled=pinb.6
symbol grnled=pinb.5
Many thanks as I am now rolling on this simple block signal conrol fro HO train
 

lbenson

Senior Member
pinb.7 specifies an input pin, and returns the value of that pin, which will be 0 or 1. So HIGH redled will set either pin 0 high or pin 1 high--not what you intend. B.7 designate the pin number.

To see the difference, try SERTXD(#pinB.7," ",B.7,13,10) in the simulator. Also in the simulator, look at the difference between HIGH B.7 and HIGH pinB.7. This is a matter which has confused many, myself included, but consider that it is necessary to distinguish between a pin and the electrical value (high or low, 1 or 0) which is present on that pin.
 

hippy

Technical Support
Staff member
Don't understand why inputs have to be pinb.0 yet outputs are simply b.6.
Pins are all named "X.Y", so "B.0" and "B.6" do refer to those pins on the chip.

All "X.Y" names represent a 'magic number' the compiler uses to understand which pin is being referred to. That number isn't very meaningful or memorable to most people so the "X.Y" form is used which is.

Most commands reference the pin name, expect the "X.Y" format, and this includes HIGH, LOW, INPUT and OUTPUT so -

HIGH B.6 will set the B.6 pin to output and high
LOW B.6 will set the B.6 pin output and low
OUTPUT B.6 will set the B.6 to be an output
INPUT B.0 will set the B.0 pin to be an input

When it comes to reading an input pin, most often in an IF statement, 'IF B.0 = 1' is asking 'does "B.0" have the magic value "1" ?'. It doesn't, never will and, regardless, the PICAXE Basic language does not allow 'IF value = value' so a syntax error is produced.

The "X.Y" represents the pin name. To read the signal level coming in on that pin when an input one has to use "pinX.Y". So "IF pinB.0 == 1" is correct, is asking 'is the input signal to pin B.0 a high (1) value ?", which is what's wanted in the code.

This does mean there can appear to be anomalies when "X.Y" is used in some places, "pinX.Y" in others but it is logical and consistent, such as -

INPUT B.0
IF pinB.0 = 1 THEN

The first makes the named pin an input pin. The second reads the input signal value presented to that pin.

An issue arises when one comes to use SYMBOL to define a name to represent a pin, such as "SYMBOL inblk = B.0". You can replace "B.0" in the code but cannot replace "pinB.0" with "inblk because "B.0" and "pinB.0" aren't the same thing. So you need two definitions to cover both. This isn't usually an issue for output pins because only the pin names are ever used.

So the solution here would be something like -
Code:
Symbol inblk  = B.0 : Symbol inblk_level = pinB.0
Symbol outblk = B.1
Symbol redled = B.7

Input  inblk
Output outblk

Main:
    If inblk_level = 1 Then
       High redled
    Else
       Low  redled
   End If
   Goto Main
 
Top