Inverting port pin

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
be80be
Registered User
Registered User
Posts: 90
Joined: Mon Feb 23, 2009 2:15 am
Location: tn

Inverting port pin

Post by be80be » Tue Mar 20, 2012 1:00 pm

If I have data coming in PORTC.7 and I want to Invert it and send it out PORTC.0

Whats the best way if I just XOR it it errors

PORTC.0 XOR PORTC.7

Thanks

Jerry Messina
Swordfish Developer
Posts: 1473
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Post by Jerry Messina » Tue Mar 20, 2012 2:11 pm

I think you need a destination in there somewhere. To toggle a bit you XOR it with '1'

Here's a few choices...

Code: Select all

// 9 instructions
// note: this always sets PORTC.0 first no matter what, so it may glitch the output
PORTC.0 = PORTC.7 xor 1

// 7 instructions
PORTC.0 = not PORTC.7

// 5 instructions
if (PORTC.7 = 1) then
    PORTC.0 = 0
else
    PORTC.0 = 1
endif
The first one may cause problems in that it always sets the output pin (and then clears it if needed). Not a good choice.

The fastest method is the third

be80be
Registered User
Registered User
Posts: 90
Joined: Mon Feb 23, 2009 2:15 am
Location: tn

Post by be80be » Wed Mar 21, 2012 12:49 am

The input pin is high going low I want to invert it and set the output low going high

I used the third one you posted, But I was thinking I could XOR it seeing its high but that doesn't work so I guess I'll keep doing it with the IF then statement

Thanks Jerry

Post Reply