40x2 serial out a.4

techElder

Well-known member
I can't figure out why I can't get A.4 to work the way it should. I must be missing something.

There's a difference between OUTPINSA & PINSA of 16 which is A.4.

There's a difference between PINSA & %11111011 of 16 which is A.4.

I don't get it.

Here's the output from hSerial (NOT sertxd). I've pulled the programming cable, and it doesn't make any difference. I've also changed the 40X2.

EDIT: I keep forgetting to say that this is all done on the AXE091 development board.

pinsA vs. outpinsA: 255 vs. 239
pinsA AND BIT2=0 (should be %11111011 or 251: 235
pinsA vs. outpinsA: 255 vs. 239
pinsA AND BIT2=0 (should be %11111011 or 251: 235
pinsA vs. outpinsA: 255 vs. 239
pinsA AND BIT2=0 (should be %11111011 or 251: 235
...

Code:
[color=Navy]#picaxe [/color][color=Black]40X2      [/color][color=Green]; Set the compiler mode, also the equivalent of #define xxx.[/color]
[color=Navy]#no_table         [/color][color=Green]; Do not download table or EEPROM data (X1 and X2 parts only).[/color]

[color=Navy]#define [/color][color=Black]SERIALOUTPUT [/color][color=Blue]hserout [/color][color=Navy]0[/color][color=Black], [/color]

[color=Blue]Hsersetup B38400_32[/color][color=Black], [/color][color=Navy]%111[/color]

[color=Purple]dirsa [/color][color=DarkCyan]= [/color][color=Navy]%11111111[/color]

[color=Blue]do
      [/color][color=Purple]pinsA [/color][color=DarkCyan]= [/color][color=Navy]%11111111
      [/color][color=Blue]pause [/color][color=Navy]1000
      [/color][color=Black]SERIALOUTPUT [/color][color=Blue]([/color][color=Red]"pinsA vs. outpinsA: "[/color][color=Black], #[/color][color=Purple]outpinsA[/color][color=Black], [/color][color=Red]" vs. "[/color][color=Black], #[/color][color=Purple]pinsA[/color][color=Black], [/color][color=Blue]cr[/color][color=Black],[/color][color=Blue]lf)
      [/color][color=Purple]pinsA [/color][color=DarkCyan]= [/color][color=Purple]pinsA [/color][color=DarkCyan]& [/color][color=Navy]%11111011
      [/color][color=Blue]pause [/color][color=Navy]1000
      [/color][color=Black]SERIALOUTPUT [/color][color=Blue]([/color][color=Red]"pinsA AND BIT2=0 (should be %11111011 or 251: "[/color][color=Black], #[/color][color=Purple]pinsA[/color][color=Black], [/color][color=Blue]cr[/color][color=Black],[/color][color=Blue]lf)
loop[/color]
 
Last edited:

hippy

Technical Support
Staff member
There's a difference between OUTPINSA & PINSA of 16 which is A.4.
There's a difference between PINSA & %11111011 of 16 which is A.4.
pinsA vs. outpinsA: 255 vs. 239
pinsA AND BIT2=0 (should be %11111011 or 251: 235
It's not clear what you are actually saying and your example code is rather obscure but I suspect you are saying that when A.4 is set high the pinsA variable does not show A.4 as high but outpinsA does.

It is not advised to use pinsX variables to read the state of output pins and that is why 'outpinsX' variables are provided.

Thus your "pinsA = pinsA & %11111011" would be correctly expressed as -

pinsA = outpinsA & %11111011
 

techElder

Well-known member
I corrected the code to use OUTPINSA on the right side of the equation and it seems to work, but only if I report OUTPINSA. PINSA always seems to be affected (in reporting) by A.4. In other words, PINSA is not always equal to OUTPINSA, but OUTPINSA seems to be accurate.

The manual is somewhat agnostic about this difference even though the suggestion is to use OUTPINSA on the right side. (I can't find where I read that now, but Manual 2, page 21 describes using PINSA on either side of the equation.)

I haven't found anything that goes into the depth of explanation that you are mentioning with "It is not advised to use pinsX variables to read the state of output pins and that is why 'outpinsX' variables are provided."

This has driven me crazy trying to track down in hardware.

Code:
[color=Navy]#picaxe [/color][color=Black]40X2      [/color][color=Green]; Set the compiler mode, also the equivalent of #define xxx.[/color]
[color=Navy]#no_table         [/color][color=Green]; Do not download table or EEPROM data (X1 and X2 parts only).[/color]

[color=Navy]#define [/color][color=Black]SERIALOUTPUT [/color][color=Blue]hserout [/color][color=Navy]0[/color][color=Black], [/color]

[color=Blue]Hsersetup B38400_32[/color][color=Black], [/color][color=Navy]%111[/color]

[color=Purple]dirsa [/color][color=DarkCyan]= [/color][color=Navy]%11111111[/color]

[color=Blue]do
      [/color][color=Purple]pinsA [/color][color=DarkCyan]= [/color][color=Navy]%11111111
      [/color][color=Blue]pause [/color][color=Navy]1000
      [/color][color=Black]SERIALOUTPUT [/color][color=Blue]([/color][color=Red]"pinsA vs. outpinsA: "[/color][color=Black], #[/color][color=Purple]pinsA[/color][color=Black], [/color][color=Red]" vs. "[/color][color=Black], #[/color][color=Purple]outpinsA[/color][color=Black], [/color][color=Blue]cr[/color][color=Black],[/color][color=Blue]lf)
      [/color][color=Purple]pinsA [/color][color=DarkCyan]= [/color][color=Purple]outpinsA [/color][color=DarkCyan]& [/color][color=Navy]%11111011
      [/color][color=Blue]pause [/color][color=Navy]1000
      [/color][color=Black]SERIALOUTPUT [/color][color=Blue]([/color][color=Red]"pinsA AND BIT2=0 (should be %11111011 or 251: "[/color][color=Black], #[/color][color=Purple]outpinsA[/color][color=Black], [/color][color=Blue]cr[/color][color=Black],[/color][color=Blue]lf)
loop[/color]
pinsA vs. outpinsA: 239 vs. 255
pinsA AND BIT2=0 (should be %11111011 or 251: 251
pinsA vs. outpinsA: 239 vs. 255
pinsA AND BIT2=0 (should be %11111011 or 251: 251
pinsA vs. outpinsA: 239 vs. 255
pinsA AND BIT2=0 (should be %11111011 or 251: 251
...
 

techElder

Well-known member
I guess I'm doing something a little offbeat, but toggling a port bit doesn't seem to be a strange operation to me. I just want to be able to know that it has changed.

So I can change the pins bit, but I have to monitor the outpins bit to check for change?

EDIT:

It also means I can't do things like:
Code:
symbol ADDR_IO = pinsA
ADDR_IO = %11111111
ADDR_IO = ADDR_IO & %11111011
; do something with the port
ADDR_IO = ADDR_IO | %00000100
However, I could add to that with:

Code:
symbol ADDR_IO = pinsA
symbol ADDR_IO_OUT = outpinsA
ADDR_IO = %11111111
ADDR_IO = ADDR_IO_OUT & %11111011
; do something with the port
ADDR_IO = ADDR_IO_OUT | %00000100
That's workable.
 
Last edited:

hippy

Technical Support
Staff member
Hippy, is this analogous to "write to LAT, read from PORT"?
Correct.

In other words, PINSA is not always equal to OUTPINSA, but OUTPINSA seems to be accurate.
That is correct and ...

I guess I'm doing something a little offbeat, but toggling a port bit doesn't seem to be a strange operation to me. I just want to be able to know that it has changed.
The only reliable way to tell is by physically examining the hardware state of the output pin perhaps by reading it in to another input pin.

Reading "pinsX" will reliably indicate what an input level is but not necessarily the level for output pins and "outpinsX" only indicates what the outputs were set as.
 

techElder

Well-known member
Is pinsA a variable or not?

Wait just a minute ... I got sidetracked with the "outpinsA" stuff from the main problem that I was having.

If "pinsA" is a variable, then I should be able to set it to a value. e.g. "pinsA = %11111100"

Then why can't I change a single bit in that variable? e.g. "pinsA = pinsA & %11111011"

So, "pinsA" isn't really a variable in the sense that "B1" is a variable?

EDIT:

... and another thing. This behavior doesn't occur on pinsB.


It is not advised to use pinsX variables to read the state of output pins and that is why 'outpinsX' variables are provided.

Thus your "pinsA = pinsA & %11111011" would be correctly expressed as -

pinsA = outpinsA & %11111011
 
Last edited:

hippy

Technical Support
Staff member
If "pinsA" is a variable, then I should be able to set it to a value. e.g. "pinsA = %11111100"
Yes, and you can.

Then why can't I change a single bit in that variable? e.g. "pinsA = pinsA & %11111011"
Because what pinsA returns on the right does not accurately reflect the output status as it is at the time.

Set 'pinsA=%11111111' and all A.x pins physically go high. Read 'pinsA' and it shows those highs except for A.4 which appears low even though set; %11101111 rather than the %11111111 you would like to see.

If you had 'pinsA=%11111111' then 'pinsA=pinsA' that would clear A.4 because you would read %11101111 then write that to the output.

Similarly 'pinsA=%11111111' then 'pinsA=pinsA & %11111011' would read %11101111 for pinsA, then AND it giving, %11101011, and set the outputs accordingly.

You have cleared A.2, but have also inadvertently affected A.4

So, "pinsA" isn't really a variable in the sense that "B1" is a variable?
It is a variable, it's just not containing the values you might like it to.

In the sense of 'b1' being an internal box which one can drop numbers in and get them back later; no it's not the same. Values dropped in 'pinsA' go to the output pins, but when you read 'pinsA' it doesn't get that value back, instead it reads the input pins.

This behavior doesn't occur on pinsB.
Just because you might be able to read some output states by reading the pins as if they were inputs for some pins does not mean it will work for all pins.
 

westaust55

Moderator
@TCH,
The words here might also give reference:
http://www.picaxe.com/BASIC-Commands/Digital-InputOutput/readoutputs/
same information in PICAXE Manual2 page 181:
Information:
The current state of the output pins can be read into a variable using the readoutputs command.
Note that this is not the same as ‘let var = pins’, as this let command reads the status of the input (not output) pins.

This command is not normally used with M2, X1 or X2 parts as the outputs can e read directly with ‘let var = outpinsX’
Albeit READOUTPUTS is now somewhat redundant.


at PICAXE maunual Page 21 and other pages for other chips the typical wording is:
When used on the left of an assignment ‘pins’ applies to the ‘output’ pins e.g. let pinsB = %11000000
will switch outputs 7,6 high and the others low.
When used on the right of an assignment ‘pins’ applies to the input pins e.g. let b1 = pinsB
will load b1 with the current state of the input pin on portB.
 
Last edited:

techElder

Well-known member
It is almost like someone designed the port commands to intentionally cause confusion. However, I understand that more commands take up more room etc. etc. etc.

I knew I was going to have trouble at this stage in my project, if for no other reason than I'm using all four ports of the 40X2. I thought that I had accounted for the pins that have fixed functions, but A.4 has thrown me for a loop for a couple of days.

Thanks to you folks for your explanations.
 
Top