Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Set the cal flag to 2, and initialize cal boundaries, task will run the calibration procedure
- _calFlag=2;
- _calLowBound=0x00;
- _calHighBound=0x7F;
- _calTry=0x3F;
- void CalibrateADC(void)
- {
- // Calibrates the ADC
- // This routine is written specifically to bypass the HAL calibration procedure, as the HAL seems to result in inaccurate ADC calibration
- // The way this routine works is that our own precision Vref value is connected to one of the ADC inputs (input channel 4)
- // We will binary-search the calibration values until we find a calibration value that returns a value as close to 4095 as possible
- // without equaling 4095. Then set the calibration value in the ADC to one less than that.
- HAL_StatusTypeDef result;
- // Make sure _calFlag!=1, otherwise we were called in error
- if (_calFlag==1) return;
- // If _calFlag=0, we are getting a set of ADC readings, figure out what to do
- if (_calFlag==0)
- {
- // Conversion finished, it's stored in _ADC_Results_Buffer[4]
- // Compare that to 4095, and move the next try value and the boundary values accordingly
- // In the case of the new conversion value = 4095, the try value is too low. Move the low boundary to try+1, the try value to (lowbound + highbound)/2, rounded down
- // In the case of the new conversion value < 4095, the try value might be too high. Move the high boundary to try, the try value to (lowbound + highbound)/2, rounded down
- // If highbound-lowbound=1, the calibration is complete, calibration value = try value-1
- if (_ADC_Results_Buffer[_DEF_VrefIndex]==4095)
- {
- // Try value is too low
- _calLowBound=_calTry+1;
- _calTry=(_calLowBound+_calHighBound)/2;
- }
- else
- {
- // Try value might be too high
- _calHighBound=_calTry;
- _calTry=(_calLowBound+_calHighBound)/2;
- }
- if (_calHighBound==_calLowBound)
- {
- // Calibration is complete
- // The current value of cal try is the closest to 4095 without being 4095
- // We want to set the ADC calibration to one less than this
- _calTry-=1;
- // Make a final call to the calibration routine to set the value to _calTry
- HAL_ADC_Start(_hadc1);
- result=HAL_ERROR;
- do
- {
- result= HAL_ADCEx_Calibration_SetValue(_hadc1, _calTry);
- } while (result!=HAL_OK);
- HAL_ADC_Stop(_hadc1);
- // Flag that calibration is complete
- _calFlag=1;
- }
- else
- {
- // Set the calibration value to the new value
- HAL_ADC_Start(_hadc1);
- result=HAL_ERROR;
- do
- {
- result= HAL_ADCEx_Calibration_SetValue(_hadc1, _calTry);
- } while (result!=HAL_OK);
- HAL_ADC_Stop(_hadc1);
- }
- }
- else
- {
- // _calFlag not equal to 0, this means this is the first call to the calibration routine.
- // Set the calibration value to the initial value
- HAL_ADC_Start(_hadc1);
- result=HAL_ERROR;
- do
- {
- result= HAL_ADCEx_Calibration_SetValue(_hadc1, _calTry);
- } while (result!=HAL_OK);
- HAL_ADC_Stop(_hadc1);
- // Flag that we've started the first conversion test, but ADC is not calibrated yet
- _calFlag=0;
- }
- // Start a new ADC conversion
- HAL_ADC_Start_DMA(_hadc1, (uint32_t *)_ADC_Results_Buffer,_DEF_NumADCChannels);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement