3.5 Performing Digital IO Operations

Description

The driver supports four types of direct digital I/O operations: input bit, input byte, output bit, output byte. Digital I/O is fairly straightforward - to perform digital input, you provide a pointer to the storage variable and indicate the port number and bit number if relevant. To perform digital output, you provide the output value and the output port and bit number, if relevant.

The four Universal Driver functions described here are dscDIOInputByte, dscDIOInputBit, dscDIOOutputByte, dscDIOOutputBit.

Step-By-Step Instructions

If you are performing digital input, create and initialize a pointer to hold the returned value of type byte reference.

Some boards have digital I/O ports with fixed directions, while others have ports with programmable directions. Make sure the port is set to the required direction, input or output. Use function dscDIOSetConfig to set the direction if necessary.

Call the selected digital I/O function. Pass it a byte port value, and either a pointer to or a constant byte digital value. If you are performing bit operations, then you will also need to pass in a byte value telling the driver which particular bit (0-7) of the DIO port you wish to operate on.

Example of Usage for Digital I/O Operations

... 

byte port, bit; 
byte digital_value; 
byte config;
byte output_byte;
DSCUD_Class DSCUD_class = new DSCUD_Library.DSCUD_Class();

config=0;
if (DSCUD_Library.DSCUD_Class.dscDIOSetConfig(DSCUD_class.dscb, ref config) != DSCUD_class.DE_NONE)

// the value ranges from 0 to 255 
/* 1. input bit - read bit 3 of port 0 (port 0 is in input mode) */ 

port = 0; bit = 3; 

if ((result = DSCUD_Library.DSCUD_Class.dscDIOInputBit(DSCUD_class.dscb, port, bit, ref digital_value)) != DSCUD_class.DE_NONE) 
    return result;

/* 2. input byte - read all 8 bits of port 0 (port 0 is in input mode) */

port = 0; 

if (result = (DSCUD_Library.DSCUD_Class.dscDIOInputByte(DSCUD_class.dscb, port, ref input_byte) != DSCUD_class.DE_NONE))
    return result; 

/* 3. output bit - 3 examples (assumes port 2 is in output mode) */ 

port = 2; bit = 7; digital_val = 1;

/* 3a. dscDIOOutputBit(), set bit 7 of port 2 to 1, leave other bits alone */ 
if ( result=(DSCUD_Library.DSCUD_Class.dscDIOOutputBit(dscb,port,bit,digital_val) != DSCUD_class.DE_NONE) )
    return result;

/* 4. output byte - set port 2 to "00000101" (port 2 is in output mode) */ 
port = 2;
output_byte =5;

if ( result=(DSCUD_Library.DSCUD_Class.dscDIOOutputByte( DSCUD_class.dscb,port,output_byte) != DSCUD_class.DE_NONE) )
    return result;

...

Last updated