|
這個是初始化代碼
得到的溫度值都是偏大 譬如室溫是25攝氏度得到的值是65?
哪位大神有成功代碼參考參考?
- /* Temperature sensor calibration value address */
- #define TEMP30_CAL_ADDR ((uint16_t*) ((uint32_t) 0x1FFFF7B8))
- #define VDD_CALIB ((uint32_t) (3300))
- #define VDD_APPLI ((uint32_t) (3000))
- #define AVG_SLOPE ((uint32_t) (5336)) //AVG_SLOPE in ADC conversion step (@3.3V)/°C multiplied by 1000 for precision on the division
- int32_t temperature; /* will contain the temperature in degrees Celsius */
- temperature = ((uint32_t) *TEMP30_CAL_ADDR - ((uint32_t) ADC1->DR * VDD_APPLI / VDD_CALIB)) * 1000;
- temperature = (temperature / AVG_SLOPE) + 30;
復(fù)制代碼 這個是參考手冊里的源碼- static void MX_ADC_Init(void)
- {
- ADC_ChannelConfTypeDef sConfig;
- /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) */
- hadc.Instance = ADC1;
- hadc.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
- hadc.Init.Resolution = ADC_RESOLUTION_12B;
-
- hadc.Init.SamplingTimeCommon = ADC_SAMPLETIME_239CYCLES_5;
-
- hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
- hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
- hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
- hadc.Init.LowPowerAutoWait = DISABLE;
- hadc.Init.LowPowerAutoPowerOff = DISABLE;
- hadc.Init.ContinuousConvMode = DISABLE;
- hadc.Init.DiscontinuousConvMode = ENABLE;
- hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START;
- hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
- hadc.Init.DMAContinuousRequests = DISABLE;
- hadc.Init.Overrun = ADC_OVR_DATA_PRESERVED;
- /* Initialize ADC peripheral according to the passed parameters */
- if (HAL_ADC_Init(&hadc) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
-
- /* 校正ADC */
- if(HAL_ADCEx_Calibration_Start(&hadc) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
-
- /**Configure for the selected ADC regular channel 16 to be converted.*/
- sConfig.Channel = ADC_CHANNEL_TEMPSENSOR;
- if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- }
復(fù)制代碼
|
|