To start the procedure, perform Configuration Example and Initialization with included extra channel, which can be re-used after this procedure is complete:
{
.channelPriority = 0U,
.isGroupEnd = true,
.pinAddress = 0U,
.portAddress = 0U,
.extMuxEnable = false,
.extMuxSelect = 0U,
.sampleTime = 120U,
.averageCount = 1U,
.rightShift = 0U,
.positiveReload = 0U,
.negativeReload = 0U,
.rangeDetectionLoThreshold = 0U,
.rangeDetectionHiThreshold = 4095U,
};
The ADC has an offset adjustment function to compensate for offset error.
It is possible to select code from +127 to -128 in a dec. for analog calibration.
The offset adjustment step is a quarter of 1LSb.
Offset = max( 0, min( 4095, floor( VIN/VREFH x 4096 + OFST/4 ) ) )
- Note
- Enable reference buffer mode using Cy_SAR2_SetReferenceBufferMode function.
Following code snippet can be used:
{
.gain = 15ul,
};
uint16_t getAdcValue(void)
{
}
bool offset_Calibration(void)
{
int16_t
result = getAdcValue();
int16_t offset_VREFH = 0U;
int16_t offset_VREFL = 0U;
uint32_t value = 0U;
if(result > 0)
{
for(offset_VREFL = 127; offset_VREFL >= -128; offset_VREFL -= 1)
{
calibrationConfig.
offset = offset_VREFL;
result = getAdcValue();
if(result == 0)
{
break;
}
if(offset_VREFL == -128)
{
return false;
}
}
}
else
{
return false;
}
calibrationConfig.
offset = -128;
result = getAdcValue();
if(result < 0xFFF)
{
for(offset_VREFH = -128; offset_VREFH <= 127; offset_VREFH += 1)
{
calibrationConfig.
offset = offset_VREFH;
result = getAdcValue();
if(result == 0xFFF)
{
break;
}
if(offset_VREFH == 127)
{
return false;
}
}
}
else
{
return false;
}
value = (floor((offset_VREFH+offset_VREFL)/2U) + 2U);
if(value > 125U)
{
return false;
}
calibrationConfig.
offset = (floor((offset_VREFH+offset_VREFL)/2U) + 2U);
return true;
}
After that, the Gain should also be calibrated as well. The ADC has a gain adjustment function to compensate for gain error. It is possible to set code from +15 to -15 dec. The gain adjustment step is a quarter of 1LSb.
Gain = max(0, min(4095, floor((4096 - GAIN)/VREFH x (VIN - VREFH/2) + 2048)))
bool gain_Calibration(void)
{
int16_t gain;
int16_t result = 0U;
calibrationConfig.
gain = 15;
result = getAdcValue();
if(result > 0)
{
for(gain = 15; gain >=-14; gain -= 1)
{
calibrationConfig.
gain = gain;
result = getAdcValue();
if(result == 0)
{
calibrationConfig.
gain = gain - 1;
break;
}
if(gain == -14)
{
return false;
}
}
}
return true;
}
Once done, the calibrationConfig global structure contains calibration data for SAR2 block 0. Calibration is advised to be done as frequent as possible.