DIRA, DIRB

Register: Direction Register for 32-bit Ports A and B.

((PUBPRI))
  DIRA [Pin(s)]


((PUBPRI))
  DIRB [Pin(s)] (Reserved for future use)


Returns: Current value of direction bits for I/O Pin(s) in Ports A or B, if used as a source variable.

  • Pin(s) is an optional expression, or a range-expression, that specifies the I/O pin, or pins, to access in Port A (0-31) or Port B (32-63). If given as a single expression, only the pin specified is accessed. If given as a range-expression (two expressions in a range format: x..y) the contiguous pins from the start to end expressions are accessed.

Explanation

DIRA and DIRB are one of six registers (DIRA, DIRB, INA, INB, OUTA and OUTB) that directly affect the I/O pins. The DIRA register holds the direction states for each of the 32 I/O pins in Port A; bits 0 through 31 correspond to P0 through P31. The DIRB register holds the direction states for each of the 32 I/O pins in Port B; bits 0 through 31 correspond to P32 through P63.

NOTE: DIRB is reserved for future use; the Propeller P8X32A does not include Port B I/O pins so only DIRA is discussed below.

DIRA is used to both set and get the current direction states of one or more I/O pins in Port A. A low (0) bit sets the corresponding I/O pin to an input direction. A high (1) bit sets the corresponding I/O pin to an output direction. All the DIRA register's bits default to zero (0) upon cog startup; all I/O pins are specified as inputs by that cog until the code instructs otherwise.

Each cog has access to all I/O pins at any given time. Essentially, all I/O pins are directly connected to each cog so that there is no hub-related mutually exclusive access involved. Each cog maintains its own DIRA register that gives it the ability to set any I/O pin's direction. Each cog's DIRA register is OR'd with that of the other cogs' DIRA registers and the resulting 32-bit value becomes the I/O directions of Port A pins P0 through P31. The result is that each I/O pin's direction state is the "wired-OR" of the entire cog collective. See I/O Pins for more information.

This configuration can easily be described in the following simple rules:

  1. A pin is an input only of no active cog sets it to an output.
  2. A pin is an output if any active cog sets it to an output.

If a cog is disabled, its direction register is treated as if were cleared to 0, causing it to exert no influence on I/O pin directions and states.

Note that because of the "wired-OR" nature of the I/O pins, no electrical contention between cogs is possible, yet they can all still access I/O pins simultaneously. It is up to the application developer to ensure that no two cogs cause logical contention on the same I/O pin during run time.

Using DIRA

Set or clear bits in DIRA to affect the direction of I/O pins as desired. For example:

DIRA := %00000000_00000000_10110000_11110011 

The above code sets the entire DIRA register (all 32 bits at once) to a value that makes I/O pins 15, 13, 12, 7, 6, 5, 4, 1 and 0 to outputs and the rest to inputs.

Using the post-clear (~) and post-set (~~) unary operators, the cog can set all I/O pins to inputs, or outputs, respectively; it's not usually desirable to set all I/O pins to outputs, however. For example:

DIRA~ 'Clear DIRA register (all I/Os are inputs) 

—and—

DIRA~~ 'Set DIRA register (all I/Os are outputs) 

The first example above clears the entire DIRA register (all 32 bits at once) to zero; all I/Os P0 through P31 to inputs. The second example above sets the entire DIRA register (all 32 bits at once) to ones; all I/Os P0 through P31 to outputs.

To affect only one I/O pin (one bit), include the optional Pin(s) field. This treats the DIRA register as an array of 32 bits.

DIRA[5]~~ 'Set DIRA bit 5 (P5 to output) 

This sets P5 to an output. All other bits of DIRA (and thus all other corresponding I/O pins) remain in their previous state.

The DIRA register supports a special form of expression, called a range-expression, which allows you to affect a group of I/O pins at once, without affecting others outside the specified range. To affect multiple, contiguous I/O pins at once, use a range expression (like x..y) in the Pin(s) field.

DIRA[5..3]~~ 'Set DIRA bits 5 through 3 (P5-P3 to output) 

This sets P5, P4 and P3 to outputs; all other bits of DIRA remain in their previous state. Here's another example:

DIRA[5..3] := %110 'Set P5 and P4 to output, P3 to input 

The above example sets DIRA bits 5, 4 and 3 equal to 1, 1, and 0, respectively, leaving all other bits in their previous state. Consequently, P5 and P4 are now outputs and P3 is an input.

IMPORTANT: The order of the values in a range-expression affects how it is used. For example, the following swaps the order of the range-expression of the previous example.

DIRA[3..5] := %110 'Set P3 and P4 to output, P5 to input 

Here, DIRA bits 3, 4 and 5 are set equal to 1, 1, and 0, respectively, making P3 and P4 outputs and P5 an input.

This is a powerful feature of range-expressions, but if care is not taken, it can also cause strange, unintentional results.

Normally DIRA is only written to but it can also be read from to retrieve the current I/O pin directions. The following assumes Temp is a variable created elsewhere:

Temp := DIRA[7..4] 'Get direction of P7 through P4 

The above sets Temp equal to DIRA bits 7, 6, 5, and 4; i.e., the lower 4 bits of Temp are now equal to DIRA7:4 and the other bits of Temp are cleared to zero. 

Unless otherwise noted, content on this site is licensed under the
Creative Commons Attribution-ShareAlike 4.0 International License.