board_android-IoT/STM32F

STM32F, STM32F103 ADC reading , multiple reading

cskimair 2024. 7. 1. 19:01

 

- 실행화면:

> Vrefint : 1705 -> 환산: 2.882  , PA0 ADC reading: 1.205

- main()함수 내부:

/* Infinite loop */
  /* USER CODE BEGIN WHILE */
  adcTickCalibration = HAL_GetTick() ; 
  while(HAL_ADCEx_Calibration_Start(&hadc1) != HAL_OK); //20240624 , ADC calibration ,  https://jeonhj.tistory.com/22 
//  while(HAL_ADCEx_Calibration_Start(&hadc2) != HAL_OK); //20240624 , ADC calibration ,  https://jeonhj.tistory.com/22 
  adcTickCalibration = HAL_GetTick() - adcTickCalibration ; 
  
  HAL_TIM_Base_Start_IT( &htim2) ; 
  
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  
adcTickBegin =  n_Timer2Interrupt ; // HAL_GetTick() ; 
HAL_ADC_Start( &hadc1) ; 
if(HAL_ADC_PollForConversion(&hadc1, 200) == HAL_OK)
{
adcVrefInt = HAL_ADC_GetValue(&hadc1);
vdd = 4095.0 * 1.20 / (float)adcVrefInt;   //vdd += 0.61; // .61 is the difference i'm getting in VDD
bufferLen = sprintf(buffer, "VREFINT: %u\tVDD: %.3f\t", adcVrefInt, vdd);   //HAL_UART_Transmit(&huart1, (uint8_t *)buffer, strlen(buffer), 100);

}  
// HAL_ADC_Stop( &hadc1) ;  // HAL_ADC_Start( &hadc1) ; 
if(HAL_ADC_PollForConversion(&hadc1, 300) == HAL_OK)
{
adcValue = HAL_ADC_GetValue(&hadc1);
adcVoltage = (vdd/4095.0) * adcValue;
bufferLen += sprintf(buffer + bufferLen, " , ADC_PA0: %u\tVoltage-PA0: %.3f, %.3f\n", 
adcValue, adcVoltage , (adcVoltage / 0.71)  ); //HAL_UART_Transmit(&huart1, (uint8_t *)buffer, strlen(buffer), 100);
}
HAL_ADC_Stop( &hadc1) ; 

adcTickEnd =  n_Timer2Interrupt; // HAL_GetTick(); 
adcTick += ( adcTickEnd - adcTickBegin) ; 
if( adcTickCnt++ > adcTickCntMAX){
adcTickCnt = 0 ; 
adcTick = adcTick / adcTickCntMAX  ; 

bufferLen += sprintf( buffer  + bufferLen , "  , TotalTick=%u , adcTickCalibration=%u   , adcTickBegin=%u, adcTickEnd=%u , [%u] ", 
adcTick  , adcTickCalibration ,adcTickBegin, adcTickEnd , adcTickEnd  - adcTickBegin ) ; 
sprintf( buffer  + bufferLen , "  ,  bufferLen=%d    ",  bufferLen) ; 

adcTick= 0 ; 
}else 
bufferLen += sprintf ( buffer  + bufferLen , "[%u]              ", adcTickCnt) ; 

  /*#define MY_PIN_13_Pin GPIO_PIN_13
#define MY_PIN_13_GPIO_Port GPIOC
  */
  HAL_GPIO_WritePin( GPIOC, GPIO_PIN_13, GPIO_PIN_SET) ; 
  HAL_Delay(500) ; 
  
  HAL_GPIO_WritePin( GPIOC, GPIO_PIN_13, GPIO_PIN_RESET) ; 
  HAL_Delay(500) ; 
//   printf("Hello World") ; 
     
  }//end of while 
  /* USER CODE END 3 */

 

- CubeMX설정:

- TIM2

 

- 배터리 전압 관련 회로도: STM32F103

 

-Vrefint 관련 자료1:

- 자료2: