mainlogo.jpg (16286 bytes)

AN508

Product: Domino 1 and 2

Masking Port 1 bits on the Domino

Date: 3/18/99

Introduction: Using Port 1 on the Domino modules can be a little tricky if you're not familiar with the technique of masking.
 

Background: Masking allows you to read or write one or more bits from a group. When writing to Port 1, we generally don't want to disturb bits 5-7 since they are used for the ADC and I2C bus. When reading the port, masking allows us to clearly see and test one or more bits and not be concerned with the others.

Write Mask

To set or clear individual bits on port 1 from BASIC, masking must be used. .AND. is used to clear a bit and .OR. is used to set a bit. Basically it works as follows:

PORT1 = PORT1 .AND. 0FEh

This will clear bit 0 of port 1. FEh = 1111 1110 binary. Since the least significant bit is 0, 0 AND anything equals 0. All the other bits will remain as they were since 1 AND 0 equals 0 and 1 AND 1 equals 1.

PORT1= PORT1 .OR. 01h

This will set bit 0 of port 1. 01h = 0000 0001 binary. Since the least significant bit is 1, 1 OR anything equals 1. All the other bits will remain as they were since 0 OR 0 equals 0 and 0 OR 1 equals 1.

Read Mask

Using a mask when reading bits allows you to easily see and test one or more bits at a time. For example, say you have a switch attached to bit 2 of Port 1 and you want to test whether it is on or off. If you read the port you might see something like:

Port1 = 0A3h

For this demonstration we'll assume that if the bit is high (or 1) the switch is on. Now, if you know all the other bits on the port are always in the same state then you could just say:

IF PORT1=0A3h THEN PRINT "Switch is off" ELSE PRINT "Switch is on"

In reality you're probably using the other bits as a mix of inputs and outputs and you're not going to know their states. To make the program clearer, especially if you have to go back and debug or make changes, use a mask. To see if the bit is on or off, mask the bits that are of no concern. For example:

SW=PORT1 .AND. 04h : REM AND the value with 0000 0100
IF SW = 4 THEN PRINT "Switch is on"
IF SW= 0 THEN PRINT "Switch is off"


Using this method it doesn't matter what state any of the other bits are in, we just mask them out.