Compensation

The run-time compensation code can be implemented as a small function. Every ADC measurement is run through this function, which uses the constants 'factor' and 'correction'.

Figure 1. Flowchart for Offset and Gain Compensation

The calculation in the Figure 1 can be implemented using the following C function, or alternatively as a macro:

signed int adc_compensate (signed int adcvalue, signed int factor, signed long correction)
{
return (((((signed long)adcvalue*factor)+correction)<<2)>>16);
}

The parameter 'constants' stored in EEPROM could be copied to SRAM variables during startup for quicker access later.