3.4 Performing Digital IO Operations

The driver supports four types of direct digital I/O operations: input bit, input byte, output bit, and 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 SAMD51 functions described here are DSCSAM_DIOInputBit, DSCSAM_DIOInputByte, DSCSAM_DIOOutputBit, DSCSAM_DIOOutputByte.‌

Step-By-Step Instructions

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

Make sure the port is set to the required direction, input or output. Use function DSCSAM_DIOConfig or DSCSAM_DIOConfigAll 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) or (0-4) of the DIO port you wish to operate on.‌

Example of Usage for Digital I/O Operations

... 

ERRPARAMS errparams;  // structure for returning error code and error string
BYTE port, bit; 
BYTE input_byte; 
BYTE digital_val; 
BYTE output_byte;

...

// the value ranges from 0 to 255 

/* 1. input bit - read bit 3 of port 0(port A) (port 0 is in input mode) */ 

port = 0; bit = 3; 

if( DSCSAM_DIOInputBit(port,bit,&input_byte) != DE_NONE )
{
			DSCGetLastError ( &errparams );
			printf ( "DSCSAM_DIOInputBit error: %s %s\n", DSCGetErrorString ( errparams.ErrCode ), errparams.errstring );
			return 0;
}
    
/* 2. input byte - read all 8 bits of port 0(port A) (port 0 is in input mode) */ 

port = 0; 

if( DSCSAM_DIOInputByte(port,&input_byte) != DE_NONE )
{
			DSCGetLastError ( &errparams );
			printf ( "DSCSAM_DIOInputByte error: %s %s\n", DSCGetErrorString ( errparams.ErrCode ), errparams.errstring );
			return 0;
}
			
/* 3. output bit (assumes port 0(port A) is in output mode) */ 

port = 0; bit = 7; 
digital_val = 1;

if(DSCSAM_DIOOutputBit(port,bit,digital_val) != DE_NONE)
{
			DSCGetLastError ( &errparams );
			printf ( "DSCSAM_DIOOutputBit error: %s %s\n", DSCGetErrorString ( errparams.ErrCode ), errparams.errstring );
			return 0;
}

/* 4. output byte - set port 0(port A) to "01010101" (port 0 is in output mode) */ 

port = 0; 
output_byte = 85;  //decimal of "01010101"

if( DSCSAM_DIOOutputByte(port,output_byte) != DE_NONE )
{
			DSCGetLastError ( &errparams );
			printf ( "DSCSAM_DIOOutputByte error: %s %s\n", DSCGetErrorString ( errparams.ErrCode ), errparams.errstring );
			return 0;
}
			

Last updated