Analog I/O Forth Example
Forth language driver functions are provided for the Analog I/O Wildcard when you include the … . These functions provide a high level application interface to the full functions of the board. Using them you can select channels, convert voltages using the 16-bit ADC and produce voltages using the 12-bit DAC. You can load the following Forth example program onto your microcontroller to provide a template for doing your own data acquisition and control, or other instrumentation needs.
This Forth example program requires that you wire pins 14 and 18 together. They can also be connected with a low value resistor. The program outputs 8 voltages and then reads them back. For each measurement the error is calculated in bits. At the end the average error is printed.
\ **************************************************************************************** \ FILE NAME: aimdemo.4TH \ copyright 2009 Mosaic Industries, Inc. All rights reserved. \ --------------------------------------------------------------------- \ DATE: 4/7/2009 \ VERSION: 1.0, for V4.xx (QED/QCard line) or 6.xx (PDQ line) \ --------------------------------------------------------------------- \ This is the demonstration code for the Analod I/O Wildcard. \ Please see the User Guide for more details. \ The accompanying waim.fin driver code MUST be loaded before this file can be loaded. \ This is an illustrative demonstration program that \ shows how to set a voltage using the DAC and read a voltage using the ADC. \ You need the Mosaic Terminal program to run this demo. \ This demo requires that jumpers J4 and J6 are installed. Also, \ Pins 14 and 18 must be connected. They can also be connected with \ a low value resistor. \ When the top level function aim_demo is executed, the controller Board \ outputs 8 voltages and then reads them back. For each measurement \ the error is calculated in bits. At the end the average error is printed. \ ******************!!!! \ MAKE SURE THAT THE AIM_MODULE_NUM CONSTANT MATCHES YOUR HARDWARE JUMPER SETTINGS!! \ ******************!!!! \ --------------------------------------------------------------------- \ \ Demonstration functions defined in this file: \ AIM_MODULE_NUM \ MUST match hardware jumper settings! \ aim_Demo ( -- ) \ --------------------------------------------------------------------- \ \ 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. \ \ ********************************************************************* HEX FIND WHICH.MAP \ do this only for page-swapping platforms! IFTRUE \ nesting is allowed if ends are sequential 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 ENDIFTRUE F WIDTH ! \ set width of names stored in dictionary ANEW AIMDemo_Code \ define forget marker for easy re-loading DECIMAL \ These are regular variables. they are ints VARIABLE output_counts VARIABLE input_counts \ These are self-fetching variables. they are floats. \ self-fetching variables are set/get differently REAL: output_voltage REAL: input_voltage REAL: bits_error \ float output_voltage, input_voltage, error, bits_error; 0 CONSTANT WAIM_MODULE_NUM \ double check your hardware jumper settings!!! : set_channel6 DAC12_CH6 WAIM_MODULE_NUM TO_DAC12 ; : read_channel0 AD16_CH0 WAIM_MODULE_NUM AD16_Sample ; : aim_demo CR CR ." Welcome to the Analog I/O Wildcard demo!" CR ." This demo requires pins 14 and 18 to be connected" CR ." Press any key to start..." KEY DROP CR CR \ Init DAC to use 2.048 int ref. This must be done before any outputs are made INT_2V_DAC12 WAIM_MODULE_NUM Init_Analog_IO \ Allow the DAC to power up 10000 microsec.delay 0.0 TO bits_error 9 1 DO 4095 512 I * MIN DUP output_counts ! \ output voltage to dac channel 6 set_channel6 \ wait one millisecond 1000 Microsec.Delay \ read the voltage from channel 0 read_channel0 input_counts ! \ calcualte float values in Volts output_counts @ UFLOT 2048.0 f/ 2.048 f* TO output_voltage input_counts @ UFLOT 65536.0 f/ 4.096 f* TO input_voltage \ calculate error output_voltage input_voltage f- FABS \ the error in bits is: the error divided by the value of one bit 0.001 f/ bits_error f+ TO bits_error \ bits_error += error / 0.001; ." Output " output_voltage f. ." volts. Read back " input_voltage f. ." volts." CR LOOP ." Over 8 measurements there was an average absolute error of " bits_error 8.0 f/ f. ." bits." CR ; FIND WHICH.MAP IFTRUE \ for kernel V4.xx platforms... XDROP ( -- ) \ drop xcfa 4 PAGE.TO.FLASH 5 PAGE.TO.FLASH 6 PAGE.TO.FLASH STANDARD.MAP SAVE OTHERWISE \ for V6.xx kernels, store to shadow flash and save pointers \ SAVE.ALL . \ this takes some time, should print FFFF for success ENDIFTRUE
See also → Analog I/O C Example
Analog I/O Wildcard User Guide
Analog I/O C Library