Digital I/O Forth Demo
Forth language demonstration program with examples of how to use the Digital IO Wildcard library of driver functions
This section presents the Forth version of the demonstration program source code.
The Forth demo is also located in your installation directory. After sending the demo to your board type:
Run_Demo
to start the demo.
\ ********************************************************************* \ FILE NAME: digitalio.4TH \ --------------------------------------------------------------------- \ 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. \ \ ********************************************************************* BASE @ HEX FIND WHICH.MAP \ WHICH.MAP does not exist in the V6.xx kernel. IFTRUE \ Do this only for page-swapping platforms, ie V4.xx EXECUTE 0= ( -- standard.map? ) \ run which.map IFTRUE 4 PAGE.TO.RAM \ if in standard.map, transfer to download map 5 PAGE.TO.RAM 6 PAGE.TO.RAM DOWNLOAD.MAP ENDIFTRUE \ nesting is allowed if ends are sequential ENDIFTRUE FIND USE.PAGE \ only in V4.xx kernels IFTRUE XDROP ( -- ) \ drop xcfa 4 USE.PAGE \ comment this out if memory map is already set up ENDIFTRUE \ the following line includes a do-nothing file if kernel V6.xx is used \ (ie, if IO.C@, IO.C!, etc. are already defined): #include "..\..\libraries\include\QED_QCard_IO.4th" F WIDTH ! \ Set width of names stored in dictionary ANEW DIO.CODE \ Forget marker for easy re-loading. \ 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 4 CONSTANT WDIM_MODULE_NUM \ 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. 05 CONSTANT DIRECTION_OFFSET 1 CONSTANT NIBBLE_0_DIRECTION \ Lines 0-3 2 CONSTANT NIBBLE_1_DIRECTION \ Lines 4-7 4 CONSTANT NIBBLE_2_DIRECTION \ Lines 8-11 8 CONSTANT NIBBLE_3_DIRECTION \ Lines 12-15 00 CONSTANT NIBBLE_0_ADDR \ Lines 0-3. 01 CONSTANT NIBBLE_1_ADDR \ Lines 4-7. 02 CONSTANT NIBBLE_2_ADDR \ Lines 8-11. 03 CONSTANT NIBBLE_3_ADDR \ Lines 12-15. 04 CONSTANT NIBBLE_4_ADDR \ Lines 16-19. Inputs only. 1 CONSTANT OUTPUT 0 CONSTANT INPUT 1 CONSTANT OUTPUT_HIGH 0 CONSTANT OUTPUT_LOW 1 CONSTANT LINE_0 2 CONSTANT LINE_1 4 CONSTANT LINE_2 8 CONSTANT LINE_3 1 CONSTANT LINE_4 2 CONSTANT LINE_5 4 CONSTANT LINE_6 8 CONSTANT LINE_7 1 CONSTANT LINE_8 2 CONSTANT LINE_9 4 CONSTANT LINE_10 8 CONSTANT LINE_11 1 CONSTANT LINE_12 2 CONSTANT LINE_13 4 CONSTANT LINE_14 8 CONSTANT LINE_15 : Init_IO_Direction ( byte1\u\byte2 -- ) \ byte1 = module number, byte2 = nibble, byte3 = 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. locals{ &direction &nibble &module } &direction IF &nibble DIRECTION_OFFSET &module IO.SET.BITS \ set nibble as output ELSE &nibble DIRECTION_OFFSET &module IO.CLEAR.BITS \ set nibble as input ENDIF ; : Control_DIO ( byte1\u\byte2\byte3 -- ) \ Sets I/O line of specified nibble to the appropriate state (high or low). \ byte1 = module number, u = nibble address, byte2 = line, byte3 = state. \ 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 locals{ &state &line &nibble_addr &module } &state IF \ set line high &line &nibble_addr &module IO.SET.BITS ELSE \ set line low &line &nibble_addr &module IO.CLEAR.BITS ENDIF ; : Read_Nibble ( byte1\u -- byte2 | byte1 = module number, u = 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. locals{ &nibble_addr &module } &nibble_addr &module IO.C@ ; : Logic_Low ( -- ) \ This word sets all outputs to logic low, or 0 volts 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 Control_DIO ; : all_outputs ( -- ) \ 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 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 Init_IO_Direction ; : Run_Demo ( -- ) CR ." Welcome to the Digital I/O Wildcard Demo" CR ." This wildcard will output a logic low output of 0 volts," CR ." and a logic high of >3.5 volts typ. Placing a pull up jumper" CR ." on an IO line will pull it up to 5.0 volts." CR ." ---------------" CR CR ." press enter to set pins 9-24 as outputs, at 0 volts." KEY DROP all_outputs Logic_Low CR ." press enter to set pins 21-24 to output high." KEY DROP 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 Control_DIO CR ." press enter to set pins 17-20 to output high." KEY DROP 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 Control_DIO CR ." press enter to set pins 13-16 to output high." KEY DROP 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 Control_DIO CR ." press enter to set pins 9-12 to output high." KEY DROP 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 Control_DIO ; FIND WHICH.MAP IFTRUE EXECUTE IFTRUE 4 PAGE.TO.FLASH 5 PAGE.TO.FLASH 6 PAGE.TO.FLASH STANDARD.MAP ENDIFTRUE ENDIFTRUE BASE ! SAVE
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 C Demo
Digital I/O Wildcard Users Guide
This page is about: Up to 20 Digital Input-output Lines, Instrument Control and Automation – Forth language demonstration program with examples of how to use the Digital IO Wildcard library of driver functions