how do i invert a single bit??? NOT? INV? other?

OLDmarty

Senior Member
Hi All,

I'm trying to invert the logic of 1 pin so it's always the opposite logic of another pinb. (Basically like an inverter of an existing output).

I'm using a 28x2 and tried using 'not' and 'inv' commands with no success.

Do those commands NOT work on a variable name? or don't operate on 1 single bit?

For example if i use:

symbol dog = b.7
symbol cat = b.6

Later in the code structure i have 'dog pulsing away a various times, so i thought i'd add an inverted output called 'cat' in this example.

"let cat = not dog" doesn't seem to compile...

then i changed my code to be:

"let cat = not b.7" but still no good...

Are the NOT and INV commands strictly for an entire byte-wide variable only?

So, after all the above, i resorted to:

high dog
low cat

and later in the code

low dog
high cat

I was hoping to create an easier inversion of 1 bit, instead of tracking where in the code all the high/lows need to occur.


thanx in advance...
 

inglewoodpete

Senior Member
dirsB= %01000000
.....
symbol dog = pinb.7
symbol cat = pinb.6

cat = not dog
Not quite - you are addressing as if there are two input pins.

Try this:
Code:
[color=Purple]dirsB[/color][color=DarkCyan]= [/color][color=Navy]%01000000[/color]
[color=Green]'.....[/color]
[color=Blue]Symbol [/color][color=Purple]Dog [/color][color=DarkCyan]= [/color][color=Purple]pinb.7[/color]
[color=Blue]Symbol [/color][color=Purple]Cat [/color][color=DarkCyan]= [/color][color=Purple]outpinb.6[/color]

[color=Blue]Do
   [/color][color=Purple]Cat [/color][color=DarkCyan]= not [/color][color=Purple]Dog[/color]
[color=Blue]Loop[/color]
 

hippy

Technical Support
Staff member
Not quite - you are addressing as if there are two input pins.
In most case a "pinX.Y" on the left of an assignment or as a result variable elsewhere will be interpreted by the compiler as an "outpinX.Y", but it is good practice to distinguish input "pin" from output "outpin", and that can be required in some cases.
 

PieM

Senior Member
In most case a "pinX.Y" on the left of an assignment or as a result variable elsewhere will be interpreted by the compiler as an "outpinX.Y", but it is good practice to distinguish input "pin" from output "outpin", and that can be required in some cases.
With dirsB= %01000000, pinb.6 is defined as an output.
pinb.6 is the state 1/0 of this pin, input or output, true ?
 

hippy

Technical Support
Staff member
With dirsB= %01000000, pinb.6 is defined as an output.
Correct.

pinb.6 is the state 1/0 of this pin, input or output, true ?
Depends on which side of an assignment it is on or where it occurs within a command ...

"LET pinB.6 = ..." will set the output state ( if an output )

"LET ... = pinB.6" or "IF pinB.6 ... THEN" will attempt to read the pin as an input even if output, but can be unreliable when it is an output pin.
 

OLDmarty

Senior Member
Thanx everyone for your input (or output lol)

I'll go recheck my exact code/syntax when i'm back in the workshop with free time, and alter it accordingly.....
 

OLDmarty

Senior Member
Hmmmm,

If i use anything other than b.7 or b.6 to configure my symbols, it won't compile. pinb.6 fails, so does outpinb.6

if i use this:
symbol dog = pinb.7
symbol cat = pinb.6

and then later in my code states this:
high cat
low dog

I then get this error when running the syntax checker (or atempting to program the chip)
"Error: This command requires the pin number in format PORT.PIN!"


When i revert back to:
symbol dog = b.7
symbol cat = b.6

my dirsb = %11111111 (all outputs)

my code then allows cat & dog to be processed, syntax check passes and it programs the chip.
 

westaust55

Moderator
When controlling a pin then use the Port.Pin nomenclature so it is
High B.7 or Low B.6 etc
And symbol statements are as SYMBOL elephant = B.7

When you want to test a pin status then you must add the "pin" prefix
So you may have
IF pinB.6 = 1 THEN ....
And the symbol statement would be in the form
SYMBOL Mouse = pinB.6
 

OLDmarty

Senior Member
I'm no better off, i simply invert a symbol based on the logic of the other symbol it's monitoring.

"cat = not dog" seems so obvious, where i want b.6(Cat) to be an inverted value of b.7(dog)

In another section of code i'm happily using a pulsing output called 'beat' (symbol beat=b.0) and i can easily state "low beat", or "high beat" and that 1 pin toggles as expected....i'never had to state "high b.0" to make it pulse, so now i'm confused why my cat/dog invertor won't work.
I'm not really testing bits, i'm just going with the logic that what logic level 'dog' is, then make 'cat' the opposite.

For now i'm just using :
high dog
low cat

which works fine, but negates the use of even trying to implement NOT or INV commands.

another bug perhaps?
 

inglewoodpete

Senior Member
I think there is some sort of user error occurring.

I have just fired up one of my AXE401 "shield" boards, which has a 28X2 on board. The board has a yellow LED connected to C.3, so I've changed to specification of "Cat" to outpinC.3

I loaded the following code with PE Version 6.0.8.3 and the LED flashes as expected at bootup, so the addressing mode works (For-Next structure). Once in the main loop, the LED is ON when input Dog (pinB.7) is jumpered to 0v and OFF when input Dog (pinB.7) is jumpered to +5v.

I think you must have a fault somewhere else that you are missing.
Code:
[color=Navy]#PICAXE [/color][color=Black]28X2[/color]
[color=Green]'[/color]
[color=Blue]Symbol [/color][color=Purple]Dog [/color][color=DarkCyan]= [/color][color=Purple]pinb.7[/color]
[color=Blue]Symbol [/color][color=Purple]Cat [/color][color=DarkCyan]= [/color][color=Purple]outpinc.3[/color]
[color=Green]'[/color]
[color=Purple]dirsB[/color][color=DarkCyan]= [/color][color=Navy]%00000000[/color]
[color=Purple]dirsC[/color][color=DarkCyan]= [/color][color=Navy]%00001000[/color]
[color=Blue]For [/color][color=Purple]b0 [/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=Blue]To [/color][color=Navy]30
   [/color][color=Purple]Cat [/color][color=DarkCyan]= [/color][color=Purple]bit0
   [/color][color=Blue]Pause [/color][color=Navy]100[/color]
[color=Blue]Next [/color][color=Purple]b0[/color]
[color=Green]'[/color]
[color=Blue]Do
   [/color][color=Purple]Cat [/color][color=DarkCyan]= not [/color][color=Purple]Dog[/color]
[color=Blue]Loop[/color]
 
Last edited:

Technical

Technical Support
Staff member
symbol cat = pinb.6
high cat
The compiler does not allow this as it generally does not do what people often expect it to do.
If input pin b.6 = 1 it makes output 1 high, if pinb.6 = 0 it makes output 0 high.
Not what you wanted.

There is a difference between these three commands
Code:
high b.6            ; make b.6 pin output, set high
high pinb.6         ; read state of input b.6 and make b.0 or b.1 output and set high
outpinb.6 = 1       ; set b.6 high
 

hippy

Technical Support
Staff member
and then later in my code states this:
high cat
low dog
I think you may need to clarify exactly what you are attempting to do.

Are you (1) trying to get an output pin to track the inverse of an input pin, or (2) trying to have two output pins which are the inverse of each other ?
 

hippy

Technical Support
Staff member
This one is specially confusing ! Should not be allowed...
The compilers detect those and produce errors when used because of that potential confusion.

If one does ever want to achieve "HIGH pinB.6" it can be done using -

b0 = pinB.6 : High b0
 

OLDmarty

Senior Member
I think you may need to clarify exactly what you are attempting to do.

Are you (1) trying to get an output pin to track the inverse of an input pin, or (2) trying to have two output pins which are the inverse of each other ?
Yes, (2) trying to have two output pins which are the inverse of each other, based on 1 pin (dog) being like the master, and cat always being the inverse of dog.

If the dog pin is generating a complex series of pulses/patterns, then all i wanted was for the cat pin to be an inverted version of the dog signal....

If cat is 1, then dog is 0, and vica-verca.

dog is on pin port b.7, while cat is on port b.6....


I thought this would be really easy to accomplish, by simply setting one output to always be the inverse of the other, but i'd like to be controlling names (symbols) instead of referring to b.6 or b.7 in my code.

My example of cat/dog was to simply what i intended to do, otherwise it may as well be called temperature, or motor drive or doorlock etc.
 

OLDmarty

Senior Member
The compiler does not allow this as it generally does not do what people often expect it to do.
If input pin b.6 = 1 it makes output 1 high, if pinb.6 = 0 it makes output 0 high.
Not what you wanted.

There is a difference between these three commands
Code:
high pinb.6         ; read state of input b.6 and make b.0 or b.1 output and set high
All the above makes no sense, how/why is controlling bit port pin b.6 affecting b.0 and b.1????? is there a misquote you took from another example?
 

lbenson

Senior Member
All the above makes no sense, how/why is controlling bit port pin b.6 affecting b.0 and b.1????? is there a misquote you took from another example?
pinb.6 has a value of either 0 or 1; if it is 0, then you are effectively issuing the command, HIGH 0; if it is 1, you are issuing the command HIGH 1.

Are you sure you are properly distinguishing between setting a pin (in which case you use the syntax, for instance, "HIGH B.0"), and reading the value of a pin, in which case you would say, for instance, BIT0 = pinB.0?

When you post that something doesn't work, please post the exact code which isn't behaving as you expect.
 

OLDmarty

Senior Member
pinb.6 has a value of either 0 or 1; if it is 0, then you are effectively issuing the command, HIGH 0; if it is 1, you are issuing the command HIGH 1.

Are you sure you are properly distinguishing between setting a pin (in which case you use the syntax, for instance, "HIGH B.0"), and reading the value of a pin, in which case you would say, for instance, BIT0 = pinB.0?

When you post that something doesn't work, please post the exact code which isn't behaving as you expect.
yes, i know what a 0 and 1 are ;-)

My code and examples are in my initial posting....now you're introducing a concept somewhat out of context stating HIGH 0 (which contradicts the logic states)

HIGH = 1 or ON, LOW = 0 or OFF.


The initial point of my post is that using symbols doesn't appear to compile, i cannot state "dog = not cat" in my code without compile errors, but i can resort to just stating
HIGH dog
LOW cat

which is the simple way around the issue, but when cleaning up some code, i thought using symbols "dog = not cat" would still turn invert the bits on Port B.6 & B.7 that i was using in my example.
 

westaust55

Moderator
HIGH 0
Is a valid command - in particular for older part but still works for the M2 and X2 parts.

Do not confuse states (high vs low) with the IO pin designations.

Output pins have predefined values as references.
B.0 = 0
B.1 = 1
:
B.7 = 7
C.0 = 8
C.1 = 9
:
C.7 = 15

Thus HIGH B.0 is the same as HIGH 0.
Care is needed as the older parts had fixed inputs so INPUT 0 was valid but now we must used INPUT C.0 (=8) to access the correct pin.
 

inglewoodpete

Senior Member
OLDmarty, Please read my post #11 and tell me what part of that code does not work for you. It contains the code Cat = not Dog
and it works!
 

hippy

Technical Support
Staff member
The initial point of my post is that using symbols doesn't appear to compile, i cannot state "dog = not cat" in my code without compile errors
The root problem is not having 'dog' and 'cat' defined as they need to be for what you wanted to achieve.

And that is not helped by 'dog' and 'cat' not being very useful names, not indicating whether they would be input pins or output pins ( though 'dog' would have to be an output ).

I would also suspect some confusion on when and where "X.Y" and "pinX.Y" names can or should appear. Also the intricacies of "pinX.Y" and "outpinX.Y".

Probably the best way to start would be to write the code without SYMBOL commands and then replace those later. For example -

Code:
#Picaxe 28X2
dirsB = %11000000
Do
  For b0 = 0 To 1
     outpinB.6 =     b0
     outpinB.7 = not b0
     Pause 1000
  Next
Loop
From that it is easier to see what the two SYMBOL statements would be -

Code:
#Picaxe 28X2
Symbol cat_outputOnB6 = outpinB.6
Symbol dog_outputOnB7 = outpinB.7
dirsB = %11000000
Do
  For b0 = 0 To 1
     cat_outputOnB6 =     b0
     dog_outputOnB7 = not b0
     Pause 1000
  Next
Loop
Then there is a trick whereby the output state of a pin can be read back and used, which gives the 'dog = not cat' functionality ...

Code:
#Picaxe 28X2
Symbol cat_outputOnB6 = outpinB.6
Symbol dog_outputOnB7 = outpinB.7
dirsB = %11000000
Do
  For b0 = 0 To 1
     cat_outputOnB6 =     b0
     dog_outputOnB7 = not cat_outputOnB6
     Pause 1000
  Next
Loop
Which, when put back to the 'dog' and 'cat' example as it was would be -

Code:
#Picaxe 28X2
Symbol cat = outpinB.6
Symbol dog = outpinB.7
dirsB = %11000000
Do
  For b0 = 0 To 1
     cat =     b0
     [b]dog = not cat[/b]
     Pause 1000
  Next
Loop
The key thing is how the SYMBOLs are defined; for this case they should both be "outpin".
 

Technical

Technical Support
Staff member
The fundamental issue here is a misunderstanding between constants (b.6, b.7) and variables (pinB.6, pinB.7, outpinB.6, outpinB.7).
They are very different - and hence are also colour coded differently in the editor.


A statement such a


symbol dog = b.6
symbol cat = b.7


b.6 = not b.7
dog = not cat
constant = not constant


makes no sense at all , and so is not allowed by the compiler.


However the command 'high' is fine with a constant


symbol dog = b.6
symbol cat = b.7
high dog
low cat


However a variable can contain the number 0 or 1, so


symbol dog = outpinb.6
symbol cat = outpinb.7


outpinb.6 = not outpinb.7
dog = not cat
variable = not variable


is absolutely fine.

Now consider the command 'high' with a variable

high pinb.6


remember pinb.6 can only contain 0 or 1 as it is a bit variable

so you are in effect saying

high 0 or high 1

The compiler knows this is probably nonsense - and hence disallows this.




There are two different variables for each pin:


pinb.6 contains a 0 or 1, and is the state of an input pin. It should only be read and is only valid when the pin is configured as an input.
outpinb.7 contains a 0 or 1, and is the state of an output pin. It can be read or written and is only valid when the pin is configured as an output.

In assembler code terms pinB.x reads the PORTB register and outpinB.x reads/writes the LATB register. They are two completely separate registers at the silicon level.
 

PieM

Senior Member
technical said:
In assembler code terms pinB.x reads the PORTB register and outpinB.x reads/writes the LATB register. They are two completely separate registers at the silicon level.
Yes but :

dirsB = %1 ; pinb.0 = 1 work !
 

hippy

Technical Support
Staff member
dirsB = %1 : pinB.0 = 1

Works because the compiler treats a "pinX.Y" on the left of the assignment equivalent to an "outpinX.Y". In assembler terms they both write to LATB registers.
 

lbenson

Senior Member
As IP stated, "Cat = not Dog" does work if Cat and Dog are properly defined.

As suggested before, when you say that some code doesn't work, please post the smallest snippet you can that shows the code not working in the way you expect. That will make it much easier for forum posters to help you to a solution.
 
Top