manufacturer of I/O-rich SBCs, operator interfaces, handheld instruments, and development tools for embedded control low cost single board computers, embedded controllers, and operator interfaces for scientific instruments & industrial control development tools for embedded control order our low cost I/O-rich embedded control products embedded controller manufacturer profile single board computers & embedded controllers development tools & starter kits for your embedded design operator interfaces with touchscreens and graphical user interface plug-in expansion modules for digital & analog I/O C language & Forth language integrated development tools, IDE single board and embedded computer accessories embedded controller enclosures, bezels, environmental gaskets

QCard C User Guide

Table of Contents


Chapter 1: Getting to Know Your QCard Controller

Chapter 2: Using Your PowerDock

Chapter 3: Your First Program


Chapter 4: The IDE: Writing, Compiling, Downloading and Debugging

Chapter 5: Making Effective Use of Memory

Chapter 6: Real Time Programming

Chapter 7: Failure and Run-Time Error Recovery


Chapter 8: Digital and Timer-Controlled I/O

Chapter 9: Data Acquisition Using Analog to Digital Conversion

Data Acquisition Using the QCard Controller

Examining the Demonstration Program

Fundamentals of Analog to Digital Conversion

Determining the Resolution of an A/D Converter

Converting an A/D Count into Its Equivalent Voltage Reading

Using the 8-bit A/D

Chapter 10: Serial Communications

Chapter 11: The Battery-Backed Real-Time Clock


Chapter 12: A Turnkeyed Application


Appendix A: QCard Electrical Specifications

Appendix B: Connector Pinouts

Appendix C: Schematics (zip)

Chapter 9

<< Previous | Next>>

Data Acquisition Using Analog to Digital Conversion

This chapter describes the analog inputs available on the  QCard, explains how to connect the converters to external signals, and details the built-in driver routines that make the analog inputs easy to use. Simple code is presented to calculate measured voltages based on A/D readings.

Data Acquisition Using the QCard Controller

Many instrument applications require monitoring of analog signals.  Analog to digital (A/D) converters can perform this function.  An A/D converter samples analog signals and converts them to digital values that can be stored, processed, or displayed. 

The resolution of an A/D is specified in bits.  For example, an 8-bit A/D converts an analog signal into one of 256 discrete digital numbers.

Table 9‑1      Analog I/O

I/O Lines


Port Address

Comments / Alternate Uses


8-bit 0-5 V analog inputs

PE 0-7

Alternately may be used as digital inputs.

The  QCard Controller hosts an analog to digital converter to address a wide variety of instrumentation and control applications.  It includes an 8-channel 8-bit analog to digital (A/D) converter that is built into the 68HC11 processor chip.  It converts unipolar signals with a nominal 0 to +5 volt range;

The analog inputs are brought out to the 24 pin Field Header on the  QCard; the connector diagrams in Appendix B specify the pin assignments.

The 8 bit A/D Converter

The 8 bit A/D converter in the processor converts unipolar signals with a nominal 0 to +5 volt range, and conversion results are returned in registers in the 68HC11.  The analog inputs are connected to the PORTE pins on the processor; these can be used as digital inputs if the 8 bit A/D is disabled. 

Examining the Demonstration Program

The code discussed in this section is located in the file named AD8.C in the \MOSAIC\DEMOS_AND_DRIVERS\MISC\C EXAMPLES directory.  Most of the functions in this file are interactive versions of functions declared in the ANALOG.H file in the \MOSAIC\FABIUS\INCLUDE\MOSAIC directory; they are described in detail in the Control-C Glossary.

We recommend that you compile and download the AD8.C program now so that you can interactively work through the exercises in this chapter.  Simply use your TextPad editor to open AD8.C, and click on the Make Tool to create the download file named AD8.DLF.  Then enter the Mosaic Terminal program, and type:


to terminate any prior multitasking program that might be running, and select the “Send File” menu item to send AD8.DLF to the  QCard Controller.

Fundamentals of Analog to Digital Conversion

An analog to digital converter samples an analog signal and outputs a digital number that is proportional to the analog signal.  The A/D converters on the QCard Controller sample input voltages and communicate the digital result to the 68HC11 processor.

The analog input signal must be within the input range of the A/D converter.  On the  QCard Controller, the lower bound of the range is equal to the voltage on VRL (voltage reference/low) and the upper bound of the allowable input range is equal to the voltage on VRH (voltage reference/high). By default, their values are 0 Volts (analog ground) and 5.0 Volts (analog +5V), respectively.

The converter measures the input voltage with a certain specified resolution.  The resolution is the granularity with which the measurement is performed.  It can be specified as a number of bits or as a voltage increment.  For example, an A/D converter with only 1 bit of resolution and a 5 Volt input range would classify all voltages from 0 to just under 2.5 Volts as the digital value 0, and voltages from 2.5 Volts to 5 Volts as the digital value 1.  This converter has a resolution equal to 1 bit, which corresponds to 2.5 Volts per count.

The converter measures the input voltage with a specified accuracy.  The accuracy tells how close the measured value is to the actual voltage.  A typical 8 bit A/D converter is accurate to within plus or minus one least significant bit.

Determining the Resolution of an A/D Converter

The VRL and VRH analog input reference pins define the lower and upper voltages that can be converted by the 8 bit A/D.  The resolution depends on the input voltage range.  The measurement resolution of a B-bit A/D, expressed in Volts per count, is

Eqn. 9‑1      Resolution = (VRH - VRL) / 2B                         [Volts per count]

where 2B is the number of counts that can be represented by a B-bit number.  From this equation we see that resolution becomes finer (better) as B grows larger or as the reference voltage range (VRH - VRL) gets smaller.  For the 8 bit A/D the resolution is

Eqn. 9‑2      8 bit Resolution = (VRH - VRL)/256                [Volts per count]

With the default VRL and VRH of 0 V and 5 V, respectively, the resolution of the 8 bit converter is 19.5 mV per count.

Converting an A/D Count into Its Equivalent Voltage Reading

To convert the 8-bit count returned by the A/D converter into an equivalent voltage, use the formula

Eqn. 9‑3      Input Voltage = VRL + ( Count * Resolution)

Combining this with Eqn. 9‑1 yields

Eqn. 9‑4      Input Voltage = VRL + Count * (VRH - VRL) / 2B

Figure 9‑1    Conversion function for the 8-bit A/D.

Using the 8-bit A/D

Initializing the 8 Bit A/D

The 8 bit A/D is inside the 68HC11 processor chip, and is accessed via PORTE. This 8 bit port can be used as either an 8 channel A/D or as an octal digital input port.  When the  QCard Controller is first powered up, or after a reset or restart, the 8 bit A/D converter is disabled and PORTE is configured as a digital input port.  To turn on the A/D converter, a program must call the function:





Another function named AD8Off() A/D8.OFF is available to turn off the 8 bit A/D so that PORTE once again acts as a digital input port. 

The function named



defined in the AD8.C file calls AD8On().  If you interactively execute InitAnalog(), as,

InitAnalog( )

the 8 bit A/D converter will be turned on and ready for use. 

In an autostarting application you should include InitAnalog()A/D8.ON in your start-up code.

Hardware Connections

To sample an analog voltage, attach a voltage with a value between zero and +5 Volts to the 8 bit A/D channel 0 input named PE0. The 8 bit A/D inputs are the PORTE signals named PE0 through PE7 available at pins 17 through 24 on the Field Header (see Appendix A).  Each analog signal is converted to a number between 0 and 255 indicating its value relative to VRL (the low voltage reference) and VRH (the high voltage reference).  The default values are VRL = 0 Volts (analog ground) and VRH = 5 Volts.  The exact voltage difference between VRH and VRL varies slightly from board to board; it is a good idea to measure the value on your board to obtain the most exact voltage equivalents of the measured A/D results.

We can calculate the range of resolutions possible from the 8 bit A/D based on Motorola’s suggested limits on VRL and VRH.  Referring to Eqn. 9‑2, we see that the finest resolution is

  3 Volts / 256 bits = 11.7 mV/bit

and the coarsest resolution is

  5.1 Volts / 256 bits = 19.9 mV/bit

This resolution is the voltage equivalent of one (least significant) bit in the A/D count.  The accuracy of the 68HC11’s A/D converter is plus or minus one least significant bit.

Once you have obtained a digital conversion result from the A/D, it is easy to calculate the measured voltage.  With B equal to 8 bits in Eqn. 9‑4, the input voltage corresponding to a given digital conversion “count” is given by

Input Voltage = VRL + Count * (VRH - VRL)  Eqn. 6.6


For example, with VRL = 0 Volts, VRH = 5.0 Volts, and a measured count of 128 (one half the full scale count), the measured input voltage is


Input Voltage = 128 * 5.0 / 256 = 2.5 Volts

which is at the midpoint of the input voltage range, as expected.

Performing Conversions

Before using the 8-bit A/D, execute


to initialize the converter.  Connect the desired analog signals to the PortE inputs available at the Analog I/O connector (see Appendix A).

Placing a channel number between 0 and 7 on the stack and executing the routine

A/D8.SAMPLE  ( channel# -- result )

reads the specified 8 bit A/D channel and returns the result on the data stack.  For example, we can connect an analog voltage to channel 7 (pin 3 on the analog connector, labeled PE7/AN7) and print the conversion result by executing

7  A/D8.SAMPLE  .

A/D8.SAMPLE GETs and RELEASEs the resource variable named A/D8.RESOURCE which mediates access to the 8-bit A/D converter in multitasking applications (the “Multitasking” chapter in the Software Manual covers these topics in detail).  Unfortunately, about 80% of the execution time of the A/D8.SAMPLE routine is devoted to the GET and RELEASE overhead.  In non-multitasked applications or in applications where only one task uses the 8 bit A/D, the faster routine

(A/D8.SAMPLE)  ( channel# -- result )

may be used.  It acquires the A/D sample without executing GET or RELEASE, and with an 8 MHz crystal it executes in only 35 microseconds compared to 180 microseconds for A/D8.SAMPLE.

The QED-Forth routine

A/D8.MULTIPLE  ( xaddr\#samples\channel# -- )

rapidly obtains a multiple number of samples from an A/D channel and stores the results as sequential bytes in memory starting at the specified extended address.  If the specified xaddr is in common RAM (consult the memory map appendix in the Software Manual), the sampling frequency is approximately 40 kHz (corresponding to 25 microseconds per sample).  If the specified xaddr is in paged memory, the sampling frequency is approximately 10 kHz (corresponding to 100 microseconds per sample).  These sampling frequencies double if the QED Board is clocked at 16 MHz.

A/D8.MULTIPLE GETs the resource variable A/D8.RESOURCE before performing the first conversion, and RELEASEs the resource variable after performing the last conversion.  The GET and RELEASE operations require about 150 microseconds to execute with an 8 MHz crystal.  In non-multitasked applications or in applications where only one task uses the 8 bit A/D, the programmer may use

(A/D8.MULTIPLE)  ( xaddr\#samples\channel# -- )

which does not call GET and RELEASE and thus executes more rapidly.  The sampling frequency is the same as specified for A/D8.MULTIPLE, but the setup and exit time is reduced by 150 microseconds.

For example, to acquire sixteen samples from channel 7 and store them starting at address B000H\0 in common memory, execute

HEX  B000 0  10 7  A/D8.MULTIPLE

To view the results as stored in memory, execute

B000 0 10 DUMP

If a stable noise-free signal is attached to the PE7/AN7 input, the acquired samples should all be equal to within 1 count.


Interactively Perform the Conversion

The Convert8() function is defined near the middle of the AD8.C file; you can see from its definition that it is simply an interactively callable version of the AD8Sample() function.  Now that you have connected an input voltage to channel PE0, you can type from your terminal:

Convert8( int 0)

The printed return value summary displays the conversion count; you’ll see a printout that looks something like this for a 1.5 volt signal:

Rtn:  -30722    77 =0x87FE004D =fp: -3.822E-34

We know that this function returns an integer, so we identify “77” (hex 0x004D) as the return value; the other numbers in the summary are irrelevant.  If the input is 1.5 volts, the result should be approximately 77 counts (256 * 1.5 / 5.0).  If the input voltage equals VRL (the low reference voltage, typically at 0 Volts), the result will equal 0.  If the input is within 1 bit of VRH (the high reference voltage, typically at 5.0 Volts), the result will equal decimal 255.  If the input is exactly half of (VRH - VRL), the result will equal decimal 128.   

Multiple 8 Bit A/D Conversions with Results Stored in a C Array

The function AD8Multiple() which is defined in the ANALOG.H file expects as inputs a buffer xaddress (32 bit extended address), a sampling interval parameter, the number of samples, and a channel number.  When called, it performs the specified number of conversions and saves the results as single bytes in the specified buffer.  As explained in the Glossary, the sampling interval parameter specifies the timing of the samples, with 0 representing the fastest sampling, and 65,535 representing the slowest sampling.  You can sample at up to 100 kHz (100,000 samples per second) using the 8-bit A/D provided the processor is devoted to this single task.

The AD8ToCArray() function defined in AD8.C uses a standard C one-dimensional array named results_8 as the data buffer.  The function accepts a channel number as input, performs conversions at the fastest sampling speed (100 kHz), and places the results in the array.  The number of samples is specified by the DEFAULT_NUMSAMPLES constant which equals 16.  Because we have connected the channel 0 input (AN0), let’s perform the multiple conversion on this channel. Type at your terminal:

AD8ToCArray( int 0)

remembering to type at least one space after the ( character.  This function does not return a value, so the printed return value summary is not relevant. To see the results of the conversion, you could write a simple C function that prints the contents of results_8; this is left as an exercise for you.  You can also use the debugging routine named DUMP to view a hexadecimal dump of the buffer.  To do this, type at your terminal:

results_8  0  16 DUMP

DUMP is a QED-Forth function that expects as inputs an address (in this case, results_8 puts the address on the data stack), a page (0, representing the common page), and the number of bytes to be dumped (16).  You should see a printout similar to this if you connected a 1.5 volt signal:

pg  addr 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F  0123456789ABCDEF

00  8E2D 4D 4D 4D 4D 4D 4D 4D 4D 4D 4D 4D 4D 4D 4D 4D 4D  MMMMMMMMMMMMMMMM   

This tells us that the contents of the 16 bytes starting at address 0x8E2D (which is the address of results_8) all equal 0x4D.  To the right of the hex dump the ascii equivalent of the memory contents is displayed; this is not relevant for the numerical data here.  We conclude that the A/D conversion result was 0x4D, equivalent to decimal 77; this is the same value returned by the Convert8() function above.  Your results may vary slightly depending on the exact value of supply voltage on the QCard Controller, as the +5V supply is used as the default reference voltage.

For Experts: How To Use Additional Features of the 8 Bit A/D

The built-in driver routines for the 8 bit A/D are easy to use and address the requirements of most applications.  If you wish to gain a detailed understanding of the operation of the 8 bit A/D, or need to use one of its special modes, the information in the Analog-To-Digital Section of the MC68HC11F1 Technical Data Manual may prove useful.  For example, one of the operating modes allows the 8 bit A/D converter to continuously sample four different analog inputs in rapid succession.

<< Previous | Next>>

Home|Site Map|Products|Manuals|Resources|Order|About Us
Copyright (c) 2006 Mosaic Industries, Inc.
Your source for single board computers, embedded controllers, and operator interfaces for instruments and automation