Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
08/12/03 19:07
Read: times


 
#52499 - RE: update pins on a port on the byte level
Responding to: ???'s previous message
Hi Spencer,

/* PORT_WR() performs a buffered write of the "mask"ed bits of "val" to
 * "port" (P1, P2, etc.) through the port's output latch register.  This
 * technique is especially useful to deal with any read-modify-write
 * issues a port may have.
 */
#define PORT_WR(port, val, mask) 
        (port = (port##_OLR ^= (port##_OLR ^ (val)) & (mask)))
The object is to update the software output latch register (or "shadow" register) by "merging-in" the new bit values (val) for some bit positions (mask) and then write the updated register value to the port.

Let's break it down and analyze each subexpression. I'll refer to the subexpression results along the way as "SER".
    port##_OLR ^ (val)
For each of the 8 bits in OLR, if that bit value is not equal to the bit in "val" in that same bit position, set that respective bit in SER. SER now has a superset "list of bits" to change in OLR. But we don't want to change all the bits; just those bit positions flagged by "mask".
    SER & (mask)
ANDing SER with "mask" zeroes the bits in SER that we don't want changed. SER now has the exact "list of bits" to change in OLR. We don't care about the actual values in OLR, we just know that those bits are different than what they need to be.
    port##_OLR ^= SER
XORing OLR with SER "flips" the OLR bit values for those bits flagged by SER's "list of bits" to change. SER now has the value that we want OLR to be. The "assign" part of the XOR-assign operator stores SER in the OLR byte. This saves the new port value for the next PORT_WR() invocation.
    port = SER
And now this writes our desired final value to the physical port.

Regards,

--
Dan Henry


List of 11 messages in thread
TopicAuthorDate
update pins on a port on the byte level            01/01/70 00:00      
   RE: update pins on a port on the byte level            01/01/70 00:00      
   RE: update pins on a port on the byte level            01/01/70 00:00      
      RE: update pins on a port on the byte level            01/01/70 00:00      
      RE: update pins on a port on the byte level            01/01/70 00:00      
         RE: update pins on a port on the byte level            01/01/70 00:00      
            RE: update pins on a port on the byte level            01/01/70 00:00      
               RE: update pins on a port on the byte level            01/01/70 00:00      
   RE: update pins on a port on the byte level            01/01/70 00:00      
   RE: update pins on a port on the byte level            01/01/70 00:00      
   RE: update pins on a port on the byte level            01/01/70 00:00      

Back to Subject List