Pin Naming Conventions

I am currently writing a program and am planning on using a 18M2 chip. I am however getting confused with the pin naming for the chip as the pinout diagram, do i use the pin numbers or the B. and C. numbers, like below?

symbol Cap = C.7
symbol Pot1 = B.6
symbol Pot2 = pin1
symbol Pot1Value = b0
symbol Pot2Value = b1
symbol Servo1 = C.2

i have written some of the program that i am going to use but am getting an error message when i try to simulate this line ...

servo Cap, 0 'reset sevo to 0 degrees

The error message that i am presented with is:

Error: Only portB pins can be used with servo!

Im completely new to all of this and so am having a few problems, so any help would be appreciated. Thanks
 
Also i have just noticed that if i use the C. and B. names it will not let me use them in my code, it will just come up with a syntax error.....
 
Code:
Symbol Cap = C.7
symbol Pot1 = B.6
symbol PTM2 = pin1
symbol Pot1Value = b0
symbol Pot2Value = b1
symbol Servo1 = C.2

	
main:
		servo cap, 0							
		pause 500								
		serout 7,N2400,(254,128)					
		serout 7,N2400,("Welcome")					
		pause 3000								
		serout 7,N2400,(254,1)						
		pause 30								
		
PTM2Interupt:
		Do
		IF  pin1 = 1 then
		b2 = 255
		Servo wing,	b2
		EndIf	
		Loop while pin1 = 1
Right now there is not a lot to this program, but i have only just started it, the main problem im having is with the 'symbol' pins and why it will not let me call them pin numbers here but will only let me use pin numbers in the code??
 
I think i may have solved some of my problems by using the 'input' command but....

I am really stuck on the PTM2 code, i want the second line to say IF PTM2 = 1 then, but it will not let me use PTM2 or C.2 ahhh!!!!
 

Paix

Senior Member
I am currently writing a program and am planning on using a 18M2 chip. I am however getting confused with the pin naming for the chip as the pinout diagram, do i use the pin numbers or the B. and C. numbers, like below?

symbol Cap = C.7
symbol Pot1 = B.6
symbol Pot2 = pin1
symbol Pot1Value = b0
symbol Pot2Value = b1
symbol Servo1 = C.2
The B. and C. numbers as you use them are just fine.


i have written some of the program that i am going to use but am getting an error message when i try to simulate this line ...

servo Cap, 0 'reset sevo to 0 degrees

The error message that i am presented with is:

Error: Only portB pins can be used with servo!

Im completely new to all of this and so am having a few problems, so any help would be appreciated. Thanks
The Picaxe Manual 2 page 204 clarifies this for M2 parts (ie 18M2). So you will need to use port B for your servo and not port C as you appear to be using.

The Pinout assignments appear in Picaxe Manual 1 page 10.

I have attached the appropriate clips for your convenience.

You can expect to be asked to upload a circuit diagram and your code to assist those trying to help you, so be prepared. If you are uncertain how to do this please ask.
= = =
Hippy, any reason not to slightly increase the .png upload size allowance please?

Sorry about the lateness of my post. struggling with .png file size limits, resizing, too small and giving up to the dreaded .jpg full size options kept me back a tad . . .
 

Attachments

Last edited:

russbow

Senior Member
On the 18m2, I think only port b pins can be used with a servo.

I suggest you also add

#picaxe18m2

as the first line of your program
 
The B. and C. numbers as you use them are just fine.
The only problem im now having is that to get the code to work i have to call the PTM2 symbol pin 1, not C.2 as i want it am i missing something here or being really thick lol!!

With the servo that makes a lot more sense now, i will change that and add #picaxe18m2 onto the start of my program... Cheers guys
 

Technical

Technical Support
Staff member
When you use if...then you use a variable, the variable contains the state of the input pin. So in that case you use the pinX.X variable name

symbol PTM = pinC.1
if PTM = 1 then...

in all output commands you address the pin directly, not the input variable, so it is

symbol servo2 = B.2
servo servo2,150
 

garylcamp

New Member
Hi,
I just ran into the same problem. I boiled it down into a simple program :

; No Error in syntax using old pinx style
symbol LED = C.6 ;let an LED be here
symbol SW1 = pin4 ;Switch 1 for user

if SW1 = 1 then ;=Vcc, (button pressed)
b1 =1
else endif

; Error in syntax using C.x style
symbol SW2 = c.5 ;Switch 1 for user

if SW2 = 1 then ;=Vcc, (button pressed)
b1 =1
else endif

This looks like one of those "fact of Life" things we must live with. Very hard to find when the error is syntax error in "if then" when you change pin5 to c.5 in the symbol (like the new naming convention suggests). I do not see how to document this more clearly. Technical has it concisely but where can you put it for beginners to see and understand? It should be under Variables-General in Manual-2, I think.

There does not seem to be a section like the (BASIC) Command in definitions on the website that covers and elaborates on technical issues for the PicAxe. There are a ton of issues that need further explanation technically and very hard to search out answers on the forum. And not fair to the forum members to have to keep answering the same questions either. I wonder if REV Ed has thought of opening a WIKI and let the community answer these questions as time goes on and have a nice searchable encyclopedia? Could reduce work load some, over time.
 
Last edited:

lbenson

Senior Member
Note that in your working code, you specify "pin4". You need a similar designation with the new syntax: pinc.5. This passes the syntax check:

symbol SW2 = pinc.5 ;Switch 1 for user

if SW2 = 1 then ;=Vcc, (button pressed)
b1 =1
else endif
 

PeteShep

Member
Theres nothing to say you can't define a pin twice.


Heres that code again with a couple of additions

Code:
#picaxe 18m2 ;

; No Error in syntax using old pinx style
symbol LED = C.6 ;let an LED be here
symbol SW1 = pin4 ;Switch 1 for user

if SW1 = 1 then ;=Vcc, (button pressed)
b1 =1
else endif

;
symbol SW2 = c.5 ;Switch 1 for user
symbol pSW2 = pinc.5 ;<<<<<<<<<<<<<<<<<<<<<<<<<<<

if pSW2 = 1 then ;=Vcc, (button pressed) ; note pSW2
b1 =1
else endif

In this case, if the normal symbol fails, just pop a "p" in front of it.
 

lewisg

Senior Member
When you use if...then you use a variable, the variable contains the state of the input pin. So in that case you use the pinX.X variable name

symbol PTM = pinC.1
if PTM = 1 then...
Except it appears not to work for me...

This picaxe08M2 code:
Code:
symbol relay = pinC.1
symbol LED   = pinC.4
main:
    toggle relay
    if relay = 0 then
        low LED
    else
        high LED    
    end if    
    pause 5000
goto main
provokes this:
toggle relay
^
Error: This command requires the pin number in format PORT.PIN not PINx!
Changing to this:
Code:
symbol relay = C.1
symbol LED   = C.4
main:
    toggle relay
    if relay = 0 then
        low LED
    else
        high LED    
    end if    
    pause 5000
goto main
Results in this error:
if relay = 0 then
^
Error: Syntax error in this line!
But this works:
Code:
symbol relay = C.1
symbol LED   = C.4
main:
    toggle relay
    if pinC.1 = 0 then
        low LED
    else
        high LED    
    end if    
    pause 5000
goto main
Am I doing something wrong?

Symbols give a lot of flexibility but much of that is lost if you can't use them for all references.
 

lewisg

Senior Member
Theres nothing to say you can't define a pin twice.

...

In this case, if the normal symbol fails, just pop a "p" in front of it.
New to the forum, didn't notice the second page until AFTER I posted. This forum software is going to take some time to get used to...

Anyway this does work:
Code:
#picaxe08M2
symbol Relay  =    C.1
symbol pRelay = pinC.1
symbol LED    =    C.4
main:
	toggle Relay
	if pRelay = 0 then
		low LED
	else
		high LED	
	end if	
	pause 5000	
goto main
Seems kind of hack to me. Your solution does make the best of a less than desirable situation. Is there a reason this has to work this way? Is there somewhere this behavior is explained or is it something that might be fixed?

Does post #10 in this thread give the answer ?
Only in retrospect. Since post 10 referred to two different pins it wasn't obvious what was going on. PeteShep's post made the issue crystal clear.

My question is WHY? Is there a good reason this weirdness should not or cannot be fixed?
 
Last edited:

hippy

Ex-Staff (retired)
Very hard to find when the error is syntax error in "if then" when you change pin5 to c.5 in the symbol (like the new naming convention suggests).
That's a misunderstanding of what needs to change and how; while pin "5" needs to change to "C.5" the 'input pin level' variable needs to change from "pin5" to "pinC.5".

You changed "pin5" to "C.5" which is not the same as changing it to "pinC.5".

You are also potentially clouding the issue because, in subsequent examples, you are using the pin "C.5" as both an output ( commands require "C.5" ) and as an input tested in an IF statement ( command requires "pinC.5" ). This is an unusual or advanced use situation; most people would use a pin for either output or input not both.

There is really no weirdness, it's probably just not recognising the subtle but important difference between a pin identifier ( "C.5" ) and the 'input level of a pin' ( "pinC.5" ). It's a bit like having "Penguin" and "Elephant" as identifiers with "Feed Penguin" and "Eat Elephant" making linguistic sense but asking "Penguin?" or "Elephant?" does not.

The main problem here was that an error was made in converting a SYMBOL definition but that error does not manifest itself until the IF command which uses that definition is encountered. Locating the source of the error and the nature of the error - "the IF has a syntax error, the IF uses a SYMBOL definition, so how does that SYMBOL definition make the IF statement incorrect?" - really comes with practice and experience. The degree of separation does make the error of -

Symbol BTN = C.5
If BTN = 1 Then ...

is harder to identify than spotting that -

If C.5 = 1 Then ...

should be -

If pinC.5 = 1 Then ...
 

lewisg

Senior Member
There is really no weirdness, it's probably just not recognising the subtle but important difference between a pin identifier ( "C.5" ) and the 'input level of a pin' ( "pinC.5" ). It's a bit like having "Penguin" and "Elephant" as identifiers with "Feed Penguin" and "Eat Elephant" making linguistic sense but asking "Penguin?" or "Elephant?" does not.
Thanks for the clarification.

But it is still weird to me. What a simpleton like me likes to do is more along the lines of:
Code:
symbol Elephant = C.1

if Elephant = 0   'elephant is hungry
  Elephant = 1    'feed elephant
endif
Having to do it this way:
Code:
symbol Elephant  =    C.1
symbol pElephant = pinC.1

if pElephant = 0  'elephant is hungry
  Elephant = 1    'feed elephant
endif
Strikes me as ugly, unnecessary and reminiscent of other deprecated BASIC constructs like let and line numbers. The entire notion of symbols is to abstract the gritty details from the programmer. Querying the status of a pin isn't the same as an input command. Unless I'm mistaken the status of a pin is binary. I realize that these are very small devices we are dealing with but something like this, if technically possible, seems a good candidate for change.

This is BASIC not C. Please don't make our brains hurt...
 

hippy

Ex-Staff (retired)
Having to do it this way:
Code:
symbol Elephant  =    C.1
symbol pElephant = pinC.1

if pElephant = 0  'elephant is hungry
  Elephant = 1    'feed elephant
endif
Strikes me as ugly, unnecessary and reminiscent of other deprecated BASIC constructs like let and line numbers. The entire notion of symbols is to abstract the gritty details from the programmer.
Actually that isn't correct but you can use ...

Code:
symbol pElephant = pinC.1

if pElephant = 0  'elephant is hungry
  [b]p[/b]Elephant = 1   'feed elephant
endif
Except it would not make a lot of sense as pElephant should either refer to a pin used as input or output but not both. It is however entirely consistent and only uses one SYMBOL definition and we are discussing the abstraction rather than usefulness.

I suspect we're just heading into further confusion though so I'll try and think of a better way to explain things.
 

MartinM57

Moderator
Is there an elephant in the room? :)

You have a fair point, up to a certain degree IMHO. At the underlying hardware level (and it's the same in PICs, AVRs and I suspect other microcontrollers) the place that you write to set the pins and the place you go to read the input values of those pins are in different registers/mapped to different memory locations. And they need to be - just because you set an output pin high doesn't mean it will have a "logic 1" level on it - it could be dragged down to zero, it could be a disconnected open collector output etc.

However it's always a gotcha (in AVR-land, reading PORTB instead of PINB is a common schoolboy error)

I don't think you'll get your wish - it's one of things your likely to have to live with. Also, in your example, what would you want "Elephant = 2" to do? Or would you expect the "compiler" to pick that up? No doubt you would also want to set Elephant to a run time calculated expression - what would happen if the expression calculated to 5 under some obscure condition?
 

Buzby

Senior Member
This pin business still trips me up now and then, but as hippy says, it's
just not recognising the subtle but important difference between a pin identifier ( "C.5" ) and the 'input level of a pin' ( "pinC.5" ).
, and we just have to learn it.

It would be ( reasonably ) easy for Rev-Ed to change the compiler so that it could recognise something like 'valueC.5' and 'configC.5' as well as 'pinC.5' and 'C.5', but you could always set up Symbols to use these names anyway.
 
Last edited:

lewisg

Senior Member
More circus, with elephants...

Consider:
Code:
symbol Elephant  =    C.4
symbol pElephant = pinC.4
high Elephant
pause 2000

main:
  pElephant = 0
  pause 2000
  if pElephant = 0 then  
    pElephant = 1
  endif
  pause 2000
goto main
Run in the simulator and downloaded to a 08M2 pin C.4 toggles on and off as expected. However if you comment out high Elephant on the third line or invert the pElephant = X logic like this:

Code:
symbol Elephant  =    C.4
symbol pElephant = pinC.4
high Elephant
pause 2000

main:
  pElephant = 1
  pause 2000
  if pElephant = 1 then  
    pElephant = 0
  endif
  pause 2000
goto main
Pin C.4 does not toggle. Changing high Elephant to low Elephant on the third line does not change the results.

I got caught not testing in my earlier examples. No then in the if's! and I swapped=X for the high/low construct. Bad bad bad... However Hippy's suggestion created some clarity in my mind. I'm not headed at this from a calculation or I/O point of view. It is more about conservation of variables and code clarity. If I set an LED or relay on a pin it is nice to be able to perform later conditionals based on the state of the pin. I see the slippery slope and the results could be worse if carried to the extreme.

Sorry if I have wandered too far afield here. I just got back to PIXAXE two weeks ago and the pinX.X and X.X has caused me some confusion. My RTFM had not really cleared the issue up but I understand a lot more now. Thanks!
 

hippy

Ex-Staff (retired)
I'm not headed at this from a calculation or I/O point of view. It is more about conservation of variables and code clarity. If I set an LED or relay on a pin it is nice to be able to perform later conditionals based on the state of the pin.
You should really be using an "outpin" variable rather than reading an output pin as if it were an input pin. It may sometimes work but may not always return the correct result nor work under all conditions.

Simulation may also give different results to what a real chip does when relying on use of an output pin as an input.

In the second case, where commenting out the "High pElephant", you're not at any time making the C.4 pin an output so any "pinC.4=" assignments won't have any effect. You need to make the pin an output before use (e.g. using a High, Low, Toggle or Output command or by setting dirsC or dirsC.4)

Not withstanding the potential unreliability of the technique, both your code examples worked as expected on my 08M2. It's likely that either your code was not as you thought or you have run into this unreliability.
 
Last edited by a moderator:
Top