Advertisement
SomeJoe7777

STM32Gx Manual ADC Calibration

Jun 10th, 2025 (edited)
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.45 KB | Source Code | 0 0
  1.     // Set the cal flag to 2, and initialize cal boundaries, task will run the calibration procedure
  2.     _calFlag=2;
  3.     _calLowBound=0x00;
  4.     _calHighBound=0x7F;
  5.     _calTry=0x3F;
  6.    
  7.     void    CalibrateADC(void)
  8.     {
  9.         // Calibrates the ADC
  10.         // This routine is written specifically to bypass the HAL calibration procedure, as the HAL seems to result in inaccurate ADC calibration
  11.         // The way this routine works is that our own precision Vref value is connected to one of the ADC inputs (input channel 4)
  12.         // We will binary-search the calibration values until we find a calibration value that returns a value as close to 4095 as possible
  13.         // without equaling 4095.  Then set the calibration value in the ADC to one less than that.
  14.    
  15.         HAL_StatusTypeDef   result;
  16.    
  17.         // Make sure _calFlag!=1, otherwise we were called in error
  18.         if (_calFlag==1) return;
  19.    
  20.         // If _calFlag=0, we are getting a set of ADC readings, figure out what to do
  21.         if (_calFlag==0)
  22.         {
  23.             // Conversion finished, it's stored in _ADC_Results_Buffer[4]
  24.             // Compare that to 4095, and move the next try value and the boundary values accordingly
  25.             // 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
  26.             // 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
  27.             // If highbound-lowbound=1, the calibration is complete, calibration value = try value-1
  28.    
  29.             if (_ADC_Results_Buffer[_DEF_VrefIndex]==4095)
  30.             {
  31.                 // Try value is too low
  32.                 _calLowBound=_calTry+1;
  33.                 _calTry=(_calLowBound+_calHighBound)/2;
  34.             }
  35.             else
  36.             {
  37.                 // Try value might be too high
  38.                 _calHighBound=_calTry;
  39.                 _calTry=(_calLowBound+_calHighBound)/2;
  40.             }
  41.    
  42.             if (_calHighBound==_calLowBound)
  43.             {
  44.                 // Calibration is complete
  45.                 // The current value of cal try is the closest to 4095 without being 4095
  46.                 // We want to set the ADC calibration to one less than this
  47.                 _calTry-=1;
  48.                 // Make a final call to the calibration routine to set the value to _calTry
  49.                 HAL_ADC_Start(_hadc1);
  50.                 result=HAL_ERROR;
  51.                 do
  52.                 {
  53.                     result= HAL_ADCEx_Calibration_SetValue(_hadc1, _calTry);
  54.                 } while (result!=HAL_OK);
  55.                 HAL_ADC_Stop(_hadc1);
  56.    
  57.                 // Flag that calibration is complete
  58.                 _calFlag=1;
  59.             }
  60.             else
  61.             {
  62.                 // Set the calibration value to the new value
  63.                 HAL_ADC_Start(_hadc1);
  64.                 result=HAL_ERROR;
  65.                 do
  66.                 {
  67.                     result= HAL_ADCEx_Calibration_SetValue(_hadc1, _calTry);
  68.                 } while (result!=HAL_OK);
  69.                 HAL_ADC_Stop(_hadc1);
  70.             }
  71.         }
  72.         else
  73.         {
  74.             // _calFlag not equal to 0, this means this is the first call to the calibration routine.
  75.             // Set the calibration value to the initial value
  76.             HAL_ADC_Start(_hadc1);
  77.             result=HAL_ERROR;
  78.             do
  79.             {
  80.                 result= HAL_ADCEx_Calibration_SetValue(_hadc1, _calTry);
  81.             } while (result!=HAL_OK);
  82.             HAL_ADC_Stop(_hadc1);
  83.    
  84.             // Flag that we've started the first conversion test, but ADC is not calibrated yet
  85.             _calFlag=0;
  86.         }
  87.  
  88.         // Start a new ADC conversion
  89.         HAL_ADC_Start_DMA(_hadc1, (uint32_t *)_ADC_Results_Buffer,_DEF_NumADCChannels);
  90.     }
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement