ST AN1711 Application note

AN1711
APPLICATION NOTE
SOFTWARE TECHNIQUES
FOR COMPENSATING ST7 ADC ERRORS
INTRODUCTION
The purpose of this document is to explain in detail some software techniques which you can apply to compensate and minimise ADC errors. The document also gives some general tips on writing software for the ADC. For a list of related application notes that contain other useful information about ADCs see section 6 on page 39.
This document provides some methods of calibrating the ADC. Some ADC errors like Offset and Gain errors can be cancelled using these simple software techniques. Other errors like Differential Linearity Error and Integral Linearity Error are associated with the ADC design and cannot be compensated easily.
The example software provided with this application note is explained in brief in section 5 on
page 35.
Rev. 1.0
AN1711/0804 1/40
1
Table of Contents
1 GENERAL SOFTWARE CONSIDERATIONS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.1 CHECKING FOR FADC (MAX) SUPPORTED BY THE DEVICE . . . . . . . . . . . . 4
1.2 SELECTING CONVERSION CHANNEL (AND STARTING CONVERSION) . . . 5
1.3 POLLING FOR END OF CONVERSION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.4 ADC CONVERSION RESULT FORMAT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.5 READING THE ADC CONVERSION RESULTS . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.6 ENTERING HALT MODE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.7 USING A TIMER TO MAKE PERIODIC CONVERSIONS . . . . . . . . . . . . . . . . . . 6
1.8 USING A 10-BIT ADC AS AN 8-BIT ADC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.9 COMBINED REGISTER FOR CONTROL BITS AND LSB OF CONVERSION RE­SULT 7
1.10ZOOMING TO LOW VOLTAGE SIGNALS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2 SOFTWARE TECHNIQUES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.1 AVERAGING TECHNIQUE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.2 AVERAGING BY QUEUE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.3 HISTOGRAM TECHNIQUE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.4 NOISE FILTERING ALGORITHM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3 REDUCING SYSTEM NOISE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3.1 INSERT “NOP” WHILE CHECKING FOR EOC . . . . . . . . . . . . . . . . . . . . . . . . 19
3.2 USING SLOW MODE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
3.3 USING WAIT MODE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
3.4 EXECUTING CODE FROM RAM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
4 CALIBRATING THE ADC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
4.1 CALIBRATION ISSUES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
4.2 CALIBRATION METHODS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
4.2.1 Use accurate voltage reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
2/40
1
40
Table of Contents
4.2.2 Use of external DAC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
4.2.3 Maintaining a Lookup table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
4.2.4 Linear compensation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
4.2.5 Zone compensation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
4.2.6 Autocalibration for Offset and Gain errors . . . . . . . . . . . . . . . . . . . . . . . . . 30
4.2.7 Calibration for Errors using 2 different zones . . . . . . . . . . . . . . . . . . . . . . 33
5 SOFTWARE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
5.1 FILE PACKAGE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
5.1.1 ADC_tech.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
5.1.2 ADC_tech.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
5.1.3 Main.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
5.2 DEPENDENCIES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
5.3 GLOBAL VARIABLES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
5.4 INTERRUPTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
5.5 CODE SIZE AND EXECUTION TIME . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
6 RELATED DOCUMENTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
3/40
SOFTWARE TECHNIQUES FOR COMPENSATING ST7 ADC ERRORS

1 GENERAL SOFTWARE CONSIDERATIONS

This section gives some basic guidelines for programming the ADC.
General Procedure
Check for f
Select the conversion channel (and start conversion)
Poll for End of Conversion
Read the ADC conversion results
Special Procedures
Entering HALT mode
Using a timer with the ADC to perform periodic conversions
Other Special Features
Not all ST7 ADCs have the same features, refer to the datasheet of the ST7 product you are using for specific information. Depending on the device, you may need to apply these tips in your ADC software:
(max) supported by the device
ADC
Using 10-bit ADC as 8-bit ADC
Handling Control Bits located in same register as Data LSBs
Zooming to low voltage signals with embedded amplifier
1.1 CHECKING FOR F
Before configuring the ADC and starting any conversions, you need to check the f
(MAX) SUPPORTED BY THE DEVICE
ADC
ADC
max­imum supported by the device. This value is documented in the ST7 datasheets, you should refer to the ADC electrical characteristics section.
For example:- Some devices have a SPEED bit for working at f MHz. For ST7 devices, f because it will boost f
can be up to 8 MHz. In this case you cannot utilize the SPEED bit,
CPU
to 4 MHz, which is greater than the allowed maximum (f
ADC
/2 but the f
CPU
(max) is 2
ADC
ADC
(max)
=2 MHz).
If f at f
Some devices support f
is 4 MHz or lower (in Run or Slow mode), you can use the SPEED bit to run the ADC
CPU
/2, and still respect the 2MHz. f
CPU
(max) = 4MHz. It is thus necessary to check the electrical charac-
ADC
ADC
(max).
teristics before configuring the ADC.
4/40
SOFTWARE TECHNIQUES FOR COMPENSATING ST7 ADC ERRORS

1.2 SELECTING CONVERSION CHANNEL (AND STARTING CONVERSION)

The ADC control register provides control bits for selecting the conversion channel. Whenever you change the channel or write in the control register, the ADC conversion starts again (if the ADC is already enabled). The voltage is sampled from the selected channel.
There is no stabilization time required by the ADC after changing the conversion channel and starting conversion. Please refer to the datasheets.

1.3 POLLING FOR END OF CONVERSION

The ADC status register has an EOC bit which is for notifying the end of conversion. In some ST7 devices this bit is named COCO for “conversion complete”.
Once the ADC is enabled, the conversion is started in continuous mode (except in ADCs with single-conversion feature). When you check and find that the EOC bit is set, the data is avail­able in the data registers (see next section).

1.4 ADC CONVERSION RESULT FORMAT

The conversion result of the ADC is available in the ADC data registers. In devices with an 8­bit ADC, an 8-bit register generally called ADCDR, is available for reading the conversion re­sult.
In a 10-bit ADC, 2 registers are available for reading the 10-bit result. The most significant 8 bits are available in a register called ADCDRH and the 2 least significant bits are available in the other register generally named ADCDRL.
This requires reading the ADCDRL and then ADCDRH. The 10-bit result is obtained by left­shifting ADCDRH by 2 bits and then ‘OR’ing the value of the 2 bits ADCDRL, read previously into a variable. All ST7 10-bit ADCs use the same format, which makes it easy to port software from one device to another. Please take care that the 2-bits of ADCDRH are not lost when left­shifting the register by 2 bits. Please refer to the datasheet for the conversion result format.
5/40
SOFTWARE TECHNIQUES FOR COMPENSATING ST7 ADC ERRORS

1.5 READING THE ADC CONVERSION RESULTS

It is recommended to read the ADCDRL first and then the ADCDRH. When the ADC is in con­tinuous mode, the EOC is set at the end of conversion and a new conversion is started again (unless the ADC supports one-shot conversion).
The ADC conversion results are not latched on ADCDRL and ADCDRH. This means that if, between reading ADCDRL and ADCDRH, there is an interrupt which takes a lot of time (more than the ADC conversion time), then the software will read the ADCDRL from one conversion and the ADCDRH from another conversion. It is thus recommended to disable interrupts be­fore reading the conversion results from ADCDRL and ADCDRH and then enable interrupts again.
However, if you are reading the ADC registers (ADCDRL and ADCDRH) in a peripheral inter­rupt subroutine, for example, if you are reading the registers in a timer interrupt or external in­terrupt sub-routine then, do not disable and enable the interrupts.
The “Enable Interrupt” instruction in an interrupt subroutine (in concurrent interrupt mode) will enable interrupts and cause a nested mode interrupt.

1.6 ENTERING HALT MODE

It is always recommended to shut down the ADC before entering the HALT mode. When ex­iting from HALT mode, put the ADC ON again. The stabilization time for the ADC, after exiting from HALT is specified in the datasheet.

1.7 USING A TIMER TO MAKE PERIODIC CONVERSIONS

Some applications may have special requirements for ADC conversion. For example, in an audio application, you may need to sample an audio signal of maximum 3 kHz. You can choose to sample 6K samples per second or higher (12K samples/s or 24K samples/s). The ST7 ADCs do not have a feature for doing this.
In this case it is recommended to use the timer and configure it to generate 6K interrupts (or 12K/ 24K depending on the design) per second. In your timer interrupt routine, the ADC con­version results can be read and stored.
This kind of configuration is easy to use because of the very fast conversion time of ST7 ADCs and the built-in continuous conversion feature.
6/40
SOFTWARE TECHNIQUES FOR COMPENSATING ST7 ADC ERRORS

1.8 USING A 10-BIT ADC AS AN 8-BIT ADC

The EOC bit is cleared only when you read the ADCDRH. You can use the ADC in 8-bit mode if you do not need 10-bit ADC resolution. Thus there is no need to read the ADCDRL register.
Here is the software flow:
1. Check for EOC
2. If EOC is set, read the ADCDRH. This clears the EOC bit.
3. Do not read the ADCDRL. It is not mandatory to read this register.

1.9 COMBINED REGISTER FOR CONTROL BITS AND LSB OF CONVERSION RESULT

Some ST7 devices have a single register containing both the 2 least significant bits of the con­version result and by some control bits in the rest of the register. In this case you need to mask the control bits to filter the 2 bits of the ADC conversion result.

1.10 ZOOMING TO LOW VOLTAGE SIGNALS

Some the ST7 devices (for example ST7LITE) have a built-in amplifier to amplify the input signal. A control bit is available to switch the amplifier ON.
It is thus possible to zoom for lower voltages by switching the ADC amplifier ON and then switching it OFF for higher voltages.
This is very useful for interfacing sensors directly connected to the ADC inputs.
7/40
SOFTWARE TECHNIQUES FOR COMPENSATING ST7 ADC ERRORS

2 SOFTWARE TECHNIQUES

2.1 AVERAGING TECHNIQUE

Averaging is a simple technique where you sample an analog input several times and take the average of the results. This technique is helpful in eliminating the effect of noise on the analog input or wrong conversion.
As we take the average of several readings, these readings must correspond to the same an­alog input voltage. You should take care that the analog input remains at the same voltage during the time period when the conversions are done. Otherwise you will add digital values corresponding to different analog inputs and introduce errors.
In other words the analog input should not change in-between the different readings consid­ered for the averaging.
It is better to collect the samples in multiples of 2. This makes it more efficient to compute the average because you can do the division by right-shifting the sum of the converted values. This saves CPU time and code memory needed to execute a division algorithm.
For example take 8,16, 32 samples etc., and then take the average.
Figure 1. Graphical representation of Averaging technique
Digital Output
Average Value
Number of Conversions
Practical measurement
To obtain the results, this averaging technique is used to measure the voltage on one of the microcontroller’s analog input pins. A total of 16 conversions is taken and the average is cal­culated. This is done in a loop in the firmware.
8/40
SOFTWARE TECHNIQUES FOR COMPENSATING ST7 ADC ERRORS
A switch connected to a port pin can be used to inform the software to send the data to a host PC for display via the SCI communications interface. The port pin used for the switch must be configured as input. The firmware checks if the switch is pressed (0 is read if switch is pressed and 1 if open), the ADC readings are then sent to the PC using RS232 communication. You can use the HyperTerminal application to display the results.
Figure 2. Averaging Algorithm
START
Initialise ADC, variables, and select analog
channel, Total=0
Start ADC
Read the ADC output registers after End of Conversion
Add read value to Total
no
Num. of Conv.
= 16?
yes
Average = total/16
Use the Result
Total conversion time = (number of samples*ADC conversion time)+ computation time.
Computation time = time taken to read the results, add them together and calculate the av­erage by dividing the total by the number of samples.
There is a tradeoff between the total conversion time and number of samples used for aver­aging, depending on the analog signal variations and time available for computation.
9/40
SOFTWARE TECHNIQUES FOR COMPENSATING ST7 ADC ERRORS
Figure 3. Hardware Setup
Vain
ADC
10nf
Application Board
Multi­Meter
The following results are obtained:
V
, VDD= 4.950 V
AREF
Table 1. Averaging Results
Vin
0.5 V 103 103 101 102
1.0 V 206 206 205 205
1.5 V 310 310 309 309
2 V 413 415 414 414
2.5 V 516 516 516 516
3 V 620 621 620 620
3.5 V 723 725 724 724
4 V 826 831 829 830
Ideal
results
RS232 communication
Maximum value
obtained
Minimum value
obtained
Average
4.5 V 930 935 934 934
4.95 1023 1023 1023 1023
Tips:
1. It is always better to take the average of 16 samples rather than to take only one conversion
result. If you take a single conversion it can be erroneous because of noise.
2. It is recommended to always keep the analog path as short as possible between the source
of the analog voltage and the ADC inputs.
10/40
SOFTWARE TECHNIQUES FOR COMPENSATING ST7 ADC ERRORS
3. Even when you connect a multimeter to the analog input signal, it may introduce noise if the
probes of the multimeter are not shielded. Hence, they act like antennae. The results vary by several (6 to 7) LSBs. The average value is always near to the center of the variations.
Figure 4. Illustration of Averaging technique
Maximum Converted Value
Average Value
Minimum Converted Value
Difference (Max - Min)
Comments
1. Averaging is the most popular technique because it requires only a little extra RAM space.
2. The disadvantage is, values affected by noise and the least occurring values (outliers) af-
fect the average.

2.2 AVERAGING BY QUEUE

This technique is Averaging of a LIFO queue (Last-in, first-out). The queue is maintained by using an array to fill the ADC results. To do this:
– Maintain a variable which contains an index for array.
– After every new conversion, overwrite the converted value at the index and then take the
average and increment the index.
– Once the index reaches the end of array, reset the index to the start of array.
Thus, for every conversion you fill the queue and overwrite the old values with the new values.
This technique is useful when the application cannot wait for the required number of conver­sions because the conversion time is too long. The time required to calculate the average should be less than the time required to make the total number of conversions.
This technique can be used for slowly varying ADC signals. for example, battery monitoring.
11/40
SOFTWARE TECHNIQUES FOR COMPENSATING ST7 ADC ERRORS
Figure 5. LIFO implementation of averaging
START
Initialise ADC, variables, and select analog channel, initialise index for array.
Read the ADC output registers after End of Conversion Store the result in an Array pointed by index.
No
Read the ADC output registers after End of Conversion Store the result in an Array pointed by index. Overwrite the original value pointed at index.
Start ADC
Is Array full
Yes
Index = 0
Take average of all the values in the array.
Use the value
12/40
No
Increment the Index
Index > Size of Array
index = 0
yes
Queue implementation
Loading...
+ 28 hidden pages