M2 pin naming / direction

mjy58

Member
Having just acquired an 18M2 (from an 18X) , I am desperately trying to get my head around the port.pin and pinport.pin notation. I have taken Technical's quote in #10 of this thread as my crutch (its on the wall!)
Then I also have to understand port direction!! I cannot find an example of the dirb.0 command in the manual. Can I assume
Code:
DIRB.0=1
is the same as this?
Code:
DIRSB=%00000001
What are the circumstances / preferences for using each?

Then I tried to employ this understanding to a DS18B20 with the READTEMP12 command whose syntax is described as
READTEMP12 pin,wordvariable
- Pin is the input pin
But of course the DS1B20 requires a conversation with the PICAXE, so "pin" must be an input AND output pin.

So does the READTEMP12 (and other similar commands) override any previously defined pin input and pin ouput statements?
 

nick12ab

Senior Member
Then I also have to understand port direction!! I cannot find an example of the dirb.0 command in the manual. Can I assume
Code:
DIRB.0=1
is the same as this?
Code:
DIRSB=000001
What are the circumstances / preferences for using each?
According to PICAXE Manual 2,
The variable dirsX is broken down into individual bit variables for setting inputs/
outputs directly e.g. dirsB = dirB.7 : dirB.6 : dirB.5 : dirB.4 : dirB.3 : dirB.2 : dirB.1 : dirB.0
So just like with the pinB.x variables, a dirB.x variable only affects the one pin being referenced. Therefore your two pieces of code are not the same as dirsB will affect the data direction of all pins on port B whereas dirB.0 will only affect the data direction of pinB.0.
 

westaust55

Moderator
You may also find it worthwhile to have a look at the pin out diagrams in PICAXE manual 1 pages 9-11 – specifically for the 18M2 , see page 10.

You can see which pins have input only (IN), which have output only (OUT) and which are bidirectional (IN/OUT).
 

mjy58

Member
According to PICAXE Manual 2,
The variable dirsX is broken down into individual bit variables for setting inputs/
outputs directly e.g. dirsB = dirB.7 : dirB.6 : dirB.5 : dirB.4 : dirB.3 : dirB.2 : dirB.1 : dirB.0
So just like with the pinB.x variables, a dirB.x variable only affects the one pin being referenced. Therefore your two pieces of code are not the same as dirsB will affect the data direction of all pins on port B whereas dirB.0 will only affect the data direction of pinB.0.
Yes, I understand one is a byte command and the other a bit command. The manual does not state the syntax for the bit command (not that I can find anyway) so I was trying to confirm if I can infer the syntax from the byte command.

So does the following set pin B.0 as output?
Code:
DIRB.0=1
and this set pin B.0 as input?
Code:
DIRB.0=0
westaust55

You may also find it worthwhile to have a look at the pin out diagrams in PICAXE manual 1 pages 9-11 – specifically for the 18M2 , see page 10.
It was the manuals that were causing the confusion. Page 185 of manual 2:
Syntax:
READTEMP12 pin,wordvariable
- Pin is the input pin.
- Variable receives the raw 12 bit data read.
This command cannot be used on the following pins due to silicon restrictions:
08, 08M, 08M2 3 = fixed input
14M, 14M2 C.3 = fixed input
18M2 C.4, C.5 = fixed input
The fixed input state of C.4 and C.5 on the 18M2 confirmed from page 10 of manual 1.

So the syntax of the READTEMP12 command states 'pin' as the 'input' pin, but later on the same page states it cannot be used with an input pin!

This did not particularly matter before bidirectional pins, but now should be updated to
Syntax:
READTEMP12 pin,wordvariable
- Pin is the input/output pin.
- Variable receives the raw 12 bit data read.
I realise once you know these basic facts, it is straightforward. But while trying to get your head round all this in the beginning, these inconsistencies just add to the frustration.
 

mjy58

Member
Pin direction with READTEMP12

Thanks for the confirmation.

I am still confused why/if to use these commands.

For example both the following work

Code:
dirb.0 = 1
readtemp12 b.0,w1
Code:
dirb.0 = 0
readtemp12 b.0,w1
So the READTEMP12 command does not care what state the data pin is in.

What state does it leave the pin after it has executed?
 

nick12ab

Senior Member
What state does it leave the pin after it has executed?
Just check with your multimeter - measure between the pin and 5V and the pin and 0V and if you get 0V between both of these then the pin is an input, and if you get 5V between any of those then the PICAXE is pulling it high or low. I don't have a temperature sensor to check with.
 

mleeee

New Member
Hope this helps ...

Yo
I use an 18M2 for stuff and find it a great device.
At the top of my code I put a couple of compiler directives and then a couple of port direction commands just after Main: entry point.
You might use:

#picaxe 18M2 ;Tell the compiler I'm using an 18M2
#No_data ;tell the compiler not to bother downloading DATA. I have none

Main:
Let DirsB=%11111110​
;Set bit 0 on Port B as input, 1-7 as outputs
Let DirsC=%11001111 ;Set bits 0-3,6-7 on port C to outputs, 4-5 as inputs
If you have recently bought your 18M2, you might have the new 18M2+. Check the top of the chip marking.
If you do, it has an internal temperature sensor, and a new instruction in the compiler reads it -

Readinternaltemp voltage,offset,variable ;read how hot the Picaxe is

but this is a very rough temperature indicator. Have a look at Manual 2, Page 175 for a description.

Hope this helps

Matt Lee
 
Last edited:

westaust55

Moderator
Note that the ReadTemp commands are specific cases of 1-Wire network commands and are bi-directional.
Therefore the DS18B20 must always be connected to a PicAxe pin which is capable of being both an input and an output.
 
Last edited:

mjy58

Member
Thanks Matt, thats looks a good way of making sure you get the pins set up at the start and documenting it at the same time.

Westaust55, I had sort of got to that conclusion. Wouldn't it be a good idea to state it in the manual?
 

westaust55

Moderator
What state does it leave the pin after it has executed?
Without checking, I suggest that a simple test may not be conclusive.

The 1-Wire network protocol has the IO pin for each device on the network as an open collector pin – hence the need for the pull-up resistor. As the PICAXE chips is the master and the last action is typically reading back data, I suggest/guess that normally the pin could be left in an input state until the next command is to be sent to an external 1-Wire device.
There are some 1-Wire devices (simple switches) that accept a command but do not respond with return data (excluding when scanning for the serial number). In these latter cases, the PICAXE IO pin may be left in an output state.

Rev Ed are the best ones to verify exactly how the PICAXE behaves when a pin is used for a 1-Wire network.
The question might be: “Why does it matter?” If a pin has been set up for 1-Wire with a pull-up resistor, you would not normally use that pin for other purposes as well.
 

Technical

Technical Support
Staff member
As WA55 says, if using a i/o pin for 1 wire you wouldn't normally use it for anything else.

During 1 wire transmissions it will alternate between output and input, so nothing else should be connected as that could stop the 1-wire working.
The default pin state when not busy is input.
 

mjy58

Member
Good point - if set up for READTEMP(12) use, then it will be dedicated to that purpose so its state is not relevant.

So, if using a pin for READTEMP(12), is it best not to specifically define its direction earlier in the program?
 

hippy

Technical Support
Staff member
So, if using a pin for READTEMP(12), is it best not to specifically define its direction earlier in the program?
That's probably the best approach; leave the direction alone so it remains a default input, let READTEMP sort things out.

If it's necessary or otherwise convenient to set the direction ( as part of a dirsB= for example ), set that pin for input.
 
Top