Link here

Analog I/O C Example


C driver functions are provided for the Analog I/O Wildcard when you include the waim.h file, as shown in the example below. 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 C example program onto your microcontroller to provide a template for doing your own data acquisition and control, or other instrumentation needs.

This C 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.

Analog I/O C Demo

// ****************************************************************************************
// NAME: waim demo
// copyright 2008 Mosaic Industries, Inc. All rights reserved.
// ---------------------------------------------------------------------
// DATE: 12/8/2008
// VERSION: 0812, for QED/Q-Line (HC-11) and PDQ line (HCS-12) controllers
// ---------------------------------------------------------------------
// This is the demonstration code for the Analog I/O Wildcard.
// Please see its User Guide for more details.
// QED/Q-Line:
// The Analog I/O Wildcard kernel extension file Install.txt
// MUST be loaded into memory before this file can be loaded.
//
//
// MAKE SURE THAT THE WAIM_MODULE_NUM CONSTANT MATCHES YOUR HARDWARE JUMPER SETTINGS!!
// ---------------------------------------------------------------------
// Demonstration functions defined in this file:
//
// WAIM_MODULE_NUM // this constant MUST match hardware jumper settings!
// unsigned int read_channel0(void) // reads voltage from 16 bit ADC channel 0
// void set_channel6( int value ) // sets voltage of 12 bit DAC channel 6
// int main(void) // runs the demo program
//
// ---------------------------------------------------------------------
// 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 <stdio.h>
#include <stdlib.h>
#include <math.h>
 
#ifdef __GNUC__
// For PDQ line platforms, the driver is enabled by simply including
// the header file below.
#include <waim.h>
#else
// For the QED/Q line platform, we include the kernel extension manager
// generated library.c. We assume that it is present in this directory.
#include "library.h"
#include "library.c"
#endif // __GNU__
 
 
// ******************* DEMONSTRATION PROGRAM ***************************
// This demonstration requires that you wire pins 14 and 18 together.  They can
// also be connected with a low value resistor.
// This 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.
 
 
 
// NOTE: YOU MUST MAKE SURE THAT THIS CONSTANT MATCHES YOUR HARDWARE JUMPERS!!
#define WAIM_MODULE_NUM 0 // Make sure jumpers J1 and J2 are removed!!!
 
 
#define min(a,b) ((a)>(b)?(b):(a))
 
 
void set_channel6( int value )
{
  To_DAC12( value, DAC12_CH6, WAIM_MODULE_NUM);
}
 
unsigned int read_channel0( void )
{
  return AD16_Sample( AD16_CH0, WAIM_MODULE_NUM );
}
 
int main ( void )
{
  int i;
  unsigned output_counts, input_counts;
  float output_voltage, input_voltage, error, bits_error;
 
  printf("\n\nWelcome to the Analog I/O Wildcard demo!\n");
  printf("This demo requires pins 14 and 18 to be connected\n");
  printf("Press any key to start...\n");
  Key();
  printf("\n");
 
  // Init DAC to use 2.048 int ref.  This must be done before any outputs are made
  Init_Analog_IO(INT_2V_DAC12,WAIM_MODULE_NUM);
 
  // Set to 0.  This variable is added to in the for loop
  bits_error = 0;
 
  for(i = 1; i < 9; i++ )
  {
      // we use min to insure we stay within the bounds of the DAC
      output_counts = min( 4095, 512*i );
 
      // output voltage to dac channel 6
      set_channel6( output_counts );
 
      // wait one millisecond
      MicrosecDelay( 1000 );
 
      // read the voltage from channel 0
      input_counts = read_channel0();
 
      // calcualte float values in Volts
      output_voltage = 2.048*(output_counts/2048.0);
      input_voltage = ( input_counts / 65536.0 ) * 4.096;
 
      // calculate error
      error = fabs(output_voltage-input_voltage);
 
      // the error in bits is: the error divided by the value of one bit
      bits_error += error / 0.001;
 
      printf("Output %2.3f volts.  Read back %2.4f volts.\n", output_voltage, input_voltage);
 
  }
 
  bits_error = bits_error / 8;
  printf("Over 8 measurements there was an average absolute error of %1.1f bits.\n", bits_error);
 
    return 0;
 
 
}



See also → Analog I/O Wildcard User Guide

Analog I/O C Library

 
This page is about: C Example Program for 16-bit ADC and 12-bit DAC, Data Acquisition, Analog Voltage Input Output, Instrumentation and Control – C language example program shows input and output of voltage using an Analog I/O Wildcard and Mosaic's microcontrollers. analog voltage, input output, c demo, demonstration code, variable, constant, channel, software, hardware jumpers
 
 
Navigation