Digital I/O C Demo
C language demonstration program with examples of how to use the Digital IO Wildcard library of driver functions
This section presents the C version of the demonstration program source code.
// ********************************************************************* // FILE NAME: digitalio.c // --------------------------------------------------------------------- // AUTHOR: BEN MORSE // DATE: 5/8/2009 // VERSION: 1.0 // --------------------------------------------------------------------- // This is the driver code for the Digital I/O Wildcard. // This code: // -Sets all lines as outputs. // -Sets outputs from 0v to logic high in groups of four. // --------------------------------------------------------------------- // Important user words: // Init_IO_Direction: Turn the specified block of IO lines to inputs or outputs // Control_DIO: Change the specified line high or low // Read_Nibble: Returns the state of the specified line // all_outputs: Sets all configurable IO lines to outputs // Run_Demo: Top level word which runs the demo // WDIM_MODULE_NUM this constant MUST match hardware jumper settings! // --------------------------------------------------------------------- // // Disclaimer: THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT // ANY WARRANTIES OR REPRESENTATIONS EXPRESS OR IMPLIED, // INCLUDING, BUT NOT LIMITED TO, ANY IMPLIED WARRANTIES // OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. // // ********************************************************************* #include <mosaic\allqed.h> // Include QED header files #ifndef __GNUC__ #include "../../libraries/include/io_access.h" #include "../../libraries/include/io_access.c" #endif // NOTE: YOU MUST MAKE SURE THAT THIS CONSTANT CORRESPONDS TO YOUR MODULE SELECT JUMPERS! // For example, to access the Wildcard at address 4: // remove both module select jumper caps and mount the card on Wildcard Module Bus 1 #define WDIM_MODULE_NUM 4 // Note: if you are using a QScreen or Handheld, accessing a wildcard at module address 0 // is not allowed; this module address is reserved for the GUI on the QScreen and Handheld. #define DIRECTION_OFFSET 05 #define NIBBLE_0_DIRECTION 1 // Lines 0-3 #define NIBBLE_1_DIRECTION 2 // Lines 4-7 #define NIBBLE_2_DIRECTION 4 // Lines 8-11 #define NIBBLE_3_DIRECTION 8 // Lines 12-15 #define NIBBLE_0_ADDR 00 // Lines 0-3. #define NIBBLE_1_ADDR 01 // Lines 4-7. #define NIBBLE_2_ADDR 02 // Lines 8-11. #define NIBBLE_3_ADDR 03 // Lines 12-15. #define NIBBLE_4_ADDR 04 // Lines 16-19. Inputs only. #define OUTPUT 1 #define INPUT 0 #define OUTPUT_HIGH 1 #define OUTPUT_LOW 0 #define LINE_0 1 #define LINE_1 2 #define LINE_2 4 #define LINE_3 8 #define LINE_4 1 #define LINE_5 2 #define LINE_6 4 #define LINE_7 8 #define LINE_8 1 #define LINE_9 2 #define LINE_10 4 #define LINE_11 8 #define LINE_12 1 #define LINE_13 2 #define LINE_14 4 #define LINE_15 8 _Q void Init_IO_Direction ( int module, uchar nibble, int direction ) // Valid module numbers are 0-7. Valid nibbles are NIBBLE_0 to NIBBLE_3 // Valid directions are INPUT or OUTPUT. // ------------------------------------------------------------------------ // The module number depends on the module select jumpers. See Table 1 for // the jumper settings and associated addresses. // ------------------------------------------------------------------------ // No error checking is done on the input parameters! // ------------------------------------------------------------------------ // This routine initializes the direction of a nibble of I/O lines on the // Digital I/O Wildcard. { if( direction ) { IOSetBits( nibble, DIRECTION_OFFSET, module ); } else { IOClearBits( nibble, DIRECTION_OFFSET, module ); } } _Q void Control_DIO ( int module, uchar nibble_addr, uchar line, int state ) // Sets I/O line of specified nibble to the appropriate state (high or low). // Valid module numbers are 0-7. // Valid nibble addresses are NIBBLE_0_ADDR to NIBBLE_3_ADDR. // Valid lines are LINE_0 to LINE_15 // Valid states are OUTPUT_HIGH or OUTPUT_LOW { if( state ) { IOSetBits( line, nibble_addr, module ); } else { IOClearBits( line, nibble_addr, module ); } } _Q uchar Read_Nibble ( int module, uchar nibble_addr ) // Reads the current state of the Digital I/O nibble. // Valid module numbers are 0-7. // Valid nibble addresses are NIBBLE_0_ADDR to NIBBLE_4_ADDR. // Returns an unsigned character whose least significant nibble represents // the four I/O lines. For example, if nibble 1 is read and a 1 is returned // (0001 in binary), then line 4 is high and lines 5-7 are low. If 12 is // returned (1100 in binary) after reading nibble 3, then lines 12 and 13 are // low and lines 14 and 15 are high. The four most significant bits of the // returned byte do not matter. { return IOFetchChar( nibble_addr, module ); } _Q void Logic_Low ( void ) // This word sets all outputs to logic low, or 0 volts { Control_DIO( WDIM_MODULE_NUM, NIBBLE_0_ADDR, LINE_0, OUTPUT_LOW ); Control_DIO( WDIM_MODULE_NUM, NIBBLE_0_ADDR, LINE_1, OUTPUT_LOW ); Control_DIO( WDIM_MODULE_NUM, NIBBLE_0_ADDR, LINE_2, OUTPUT_LOW ); Control_DIO( WDIM_MODULE_NUM, NIBBLE_0_ADDR, LINE_3, OUTPUT_LOW ); Control_DIO( WDIM_MODULE_NUM, NIBBLE_1_ADDR, LINE_4, OUTPUT_LOW ); Control_DIO( WDIM_MODULE_NUM, NIBBLE_1_ADDR, LINE_5, OUTPUT_LOW ); Control_DIO( WDIM_MODULE_NUM, NIBBLE_1_ADDR, LINE_6, OUTPUT_LOW ); Control_DIO( WDIM_MODULE_NUM, NIBBLE_1_ADDR, LINE_7, OUTPUT_LOW ); Control_DIO( WDIM_MODULE_NUM, NIBBLE_2_ADDR, LINE_8, OUTPUT_LOW ); Control_DIO( WDIM_MODULE_NUM, NIBBLE_2_ADDR, LINE_9, OUTPUT_LOW ); Control_DIO( WDIM_MODULE_NUM, NIBBLE_2_ADDR, LINE_10, OUTPUT_LOW ); Control_DIO( WDIM_MODULE_NUM, NIBBLE_2_ADDR, LINE_11, OUTPUT_LOW ); Control_DIO( WDIM_MODULE_NUM, NIBBLE_3_ADDR, LINE_12, OUTPUT_LOW ); Control_DIO( WDIM_MODULE_NUM, NIBBLE_3_ADDR, LINE_13, OUTPUT_LOW ); Control_DIO( WDIM_MODULE_NUM, NIBBLE_3_ADDR, LINE_14, OUTPUT_LOW ); Control_DIO( WDIM_MODULE_NUM, NIBBLE_3_ADDR, LINE_15, OUTPUT_LOW ); } _Q void all_outputs ( void ) // This word sets all lines as outputs // Lines 0 to 15 (pins 9 to 24) can be either inputs or outputs // The io pins are configured in groups of 4 { Init_IO_Direction( WDIM_MODULE_NUM, NIBBLE_0_DIRECTION, OUTPUT ); Init_IO_Direction( WDIM_MODULE_NUM, NIBBLE_1_DIRECTION, OUTPUT ); Init_IO_Direction( WDIM_MODULE_NUM, NIBBLE_2_DIRECTION, OUTPUT ); Init_IO_Direction( WDIM_MODULE_NUM, NIBBLE_3_DIRECTION, OUTPUT ); } int main() { printf("\nWelcome to the Digital I/O Wildcard Demo\n"); printf("This wildcard will output a logic low output of 0 volts,\n"); printf("and a logic high of >3.5 volts typ. Placing a pull up jumper\n"); printf("on an IO line will pull it up to 5.0 volts.\n---------------\n\n"); printf("press enter to set pins 9-24 as outputs, at 0 volts.\n" ); Key(); all_outputs(); Logic_Low(); printf("press enter to set pins 21-24 to output high.\n"); Key(); Control_DIO( WDIM_MODULE_NUM, NIBBLE_0_ADDR, LINE_0, OUTPUT_HIGH ); Control_DIO( WDIM_MODULE_NUM, NIBBLE_0_ADDR, LINE_1, OUTPUT_HIGH ); Control_DIO( WDIM_MODULE_NUM, NIBBLE_0_ADDR, LINE_2, OUTPUT_HIGH ); Control_DIO( WDIM_MODULE_NUM, NIBBLE_0_ADDR, LINE_3, OUTPUT_HIGH ); printf("press enter to set pins 17-20 to output high.\n"); Key(); Control_DIO( WDIM_MODULE_NUM, NIBBLE_1_ADDR, LINE_4, OUTPUT_HIGH ); Control_DIO( WDIM_MODULE_NUM, NIBBLE_1_ADDR, LINE_5, OUTPUT_HIGH ); Control_DIO( WDIM_MODULE_NUM, NIBBLE_1_ADDR, LINE_6, OUTPUT_HIGH ); Control_DIO( WDIM_MODULE_NUM, NIBBLE_1_ADDR, LINE_7, OUTPUT_HIGH ); printf("press enter to set pins 13-16 to output high.\n"); Key(); Control_DIO( WDIM_MODULE_NUM, NIBBLE_2_ADDR, LINE_8, OUTPUT_HIGH ); Control_DIO( WDIM_MODULE_NUM, NIBBLE_2_ADDR, LINE_9, OUTPUT_HIGH ); Control_DIO( WDIM_MODULE_NUM, NIBBLE_2_ADDR, LINE_10, OUTPUT_HIGH ); Control_DIO( WDIM_MODULE_NUM, NIBBLE_2_ADDR, LINE_11, OUTPUT_HIGH ); printf("press enter to set pins 9-12 to output high.\n"); Key(); Control_DIO( WDIM_MODULE_NUM, NIBBLE_3_ADDR, LINE_12, OUTPUT_HIGH ); Control_DIO( WDIM_MODULE_NUM, NIBBLE_3_ADDR, LINE_13, OUTPUT_HIGH ); Control_DIO( WDIM_MODULE_NUM, NIBBLE_3_ADDR, LINE_14, OUTPUT_HIGH ); Control_DIO( WDIM_MODULE_NUM, NIBBLE_3_ADDR, LINE_15, OUTPUT_HIGH ); return 0; }
The Digital I/O Wildcard is ideal for instrumentation, automation, and control applications. It expands the digital IO capabilities of Mosaic embedded computer boards. This 2" x 2.5" card provides 16 channels configurable in groups of four as either inputs or outputs plus 4 additional digital input channels. Each output line is configurable for pull up, pull down, or tri-state operation and can directly drive logic devices, LEDs (light emitting diodes) and relays.
See also → Digital I/O Forth Demo
Digital I/O Wildcard Users Guide
This page is about: Up to 20 Digital Input-output Lines – C language demonstration program with examples of how to use the Digital IO Wildcard library of driver functions