40X1 in a0 syntax

hax

New Member
Quick question I hope.

I want to use the 40x1 analog inputs as digital inputs.

What is the appropriate syntax to do this?

I tried this:

if pina0 = 1 then dosomething


but it does not work.

I did search the manual before posting, so if it is in the manual and I am blind, can someone refer me to the page number?
 

womai

Senior Member
if porta pin0 = 1 then ...


Note that the porta keyword is valid for the whole line, i.e. you cannot mix porta and non-porta input pins in the same if statement.

Wolfgang
 

westaust55

Moderator
40X1 port A as digital IO

Womai may be right as I have not tried to do this.

But in reading the manual there is no reference to "porta".

It could be that you have to poke and peek the special fucntion registers to even set up a port A pin as a digital input. Something along these lines:

For each portA bit in question, Set the corresponding bit in the ANSEL register to a 0 to make the bit a digital IO (instead of Analogue)

Set the corresponding bit in the port A register TRISA) as a 1 to set the pin as an input.

then read the port A register with Peek 5, b0


So first, try what Womai has suggested . . .

EDIT:
In this thread, hippy has givien a clue:
http://www.picaxeforum.co.uk/showthread.php?t=10265&highlight=portA

I recall reading previously that portA pins (more than other inputs) need pull down reistors on unused inputs to ensure valid readings. I guess this also suggests portA can be read in BASIC.

Lets hope there is a way to do it thru BASIC as while the TRISA register (133) is accessible thru Poke and Peek commands, the ANSEL register (location 188) is outside the picaxe BASIC peek and poke range.

EDIT2:
tried hippys code from the above thread.
Syntax wise comes up okay but try to simulate or run and you get an error "unknown byte/bit variable"
The reason is that on portA bit/pin 4 (= physical pin 6) is used as the Serial Input line and not available under PICAXE BASIC for use as an input (analogue or digital). So skip the line I have highlighted in red.

Code:
b0 = 0
If PortA pin0 = 1 Then : bit0 = 1 : End If
If PortA pin1 = 1 Then : bit1 = 1 : End If
If PortA pin2 = 1 Then : bit2 = 1 : End If
If PortA pin3 = 1 Then : bit3 = 1 : End If
[COLOR="Red"]If PortA pin4 = 1 Then : bit4 = 1 : End If[/COLOR]   ; do not use this line
[code]
 
Last edited:

westaust55

Moderator
Thanks Technical.

However since port c is covered in both manual section 1 appendix F AND manual section 2 wrt programming seems an ommission that there is absolutely nothing in the Manual section 2 about port a, suggest the IF...THEN ability to test port a be added around manual section 2 page 71 - 77 in the next revision.
 
Top