Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I read and write pins on a port?

Ports

How do I read and write pins on a port?

by  Unknown member  Posted    (Edited  )
Here's what I figured out about reading and writing port pins . . . .

You can write a pattern of bits to the port once you have a mechanism for doing so, like a dll which will do so.

Two useful sites for porty stuff are:

http://www.lvr.com
http://www.boondog.com

In essence, you can set a pin by writing a '1' to it, ie by setting the correct bit. But the problem comes when you don't want to disturb the other bits: so you can't just write (say) 00000001 to set the right most bit, because that will clear the other 7 bits. What happens if the leftmost bit was set already and was supposed to stay set: well you've just cleared it and so done some damage to the plant.

These are the rules I determined empirically, and they work although maybe there are perhaps other (better?) ways. These rules rely on being able to read the current status of the port, so that we don't disturb the bits which don't concern us at the moment:

1) To SET a bit, ie to force it HIGH; here you OR the current status with a new byte which has your target bit set. (Don't worry- examples below!)

2) To CLEAR a bit, ie force it LOW; you AND the current status with the 'inverse' of a byte with your bit set.

3) To TOGGLE a bit, ie go LOW if high, HIGH if LOW; XOR the current status with a byte with your bit set.

Here's an example or three....

Let's say the current status of the port is 00110101 and it's the 3rd bit from the right we're interested in changing, so it's HIGH to start with. I'll mark it 'x' below.

Case 1, we want to HIGH our bit:

Current byte: 00110101
x x marks the spot!
Our byte: 00000100 set our bit
OR them: 00110101 our bit goes HIGH- it was already, others stay as they were


Case 2, we want to LOW our bit

Current byte: 00110101
x x marks the spot
Our byte: 11111011 set all but our bit
AND them: 00110001 our bit goes LOW, others stay the same

Case 3: we want to TOGGLE our bit

Current byte: 00110101
x x still marks the spot
Our byte: 00000100 set our bit
XOR them: 00110001 our byte TOGGLES, other stay the same




Let's check this with our bit LOW to start, ie 00110001

Case 1, we want to HIGH our bit

Current byte: 00110001
x yep, x marks the spot
Our byte: 00000100 set our bit
OR them: 00110101 our bit is HIGH, others unchanged

Case 2: we want to LOW our bit

Current byte: 00110001
x
Our byte: 11111011 set all but our bit
AND them: 00110001 our bit goes LOW-it was already, others unchanged

Case 3: we want to TOGGLE our bit

Current byte: 00110001
x
Our byte: 00000100 set our bit
XOR them: 00110101 our bit TOGGLES, others unchanged.


SO you need to read the port using the mechanism of the dll you use, the do the OR, AND or XOR what you get with a byte (1,2,4.... 128) appropriate for your bit and then write the result to the port. The result is, that you SET, CLEAR or TOGGLE your byte, while maintaining the values of the other 7 bits.

AND LASTLY, if you just want to READ a bit for whatever reason, ie TEST it, I found 2 ways:

Method 1: Read the port and OR it with a byte containing all but the target bit set; you get 255 if it's set. So say you want to check the bit 'x' above:

Current byte: 00110101
x we can see it's set

Test byte: 11111011 our bit is clear
OR them 11111111 255 means it was SET

Current byte: 00110001
x we can see it's clear
Test byte 11111011 our bit clear
OR them 11111011 not 255, so it was CLEAR

Method 2: Read the port and AND it with a byte which has only our bit SET. The result is 0 if our bit was CLEAR, or number if it was SET

Current byte: 00110101
x we know it's set
Test byte: 00000100 only our bit is SET
AND them: 00000100 a number (=the test byte) so it was SET

Current byte: 00110001
x we know it's clear
Test byte: 00000100 only our bit SET
AND them: 00000000 0 means it was CLEAR.










Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top