1.查询,阻塞方式,启动ADC
int main(void)
{
/* USER CODE BEGIN 1 */
float ADC0_Value = 0;
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_ADC1_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
printf("STM32 ADC查询阻塞模式 读取芯片温度\n");
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
HAL_ADC_Start(&hadc1); //开启ADC转换
if(HAL_OK == HAL_ADC_PollForConversion(&hadc1,10)) //转换成功
{
ADC0_Value = HAL_ADC_GetValue(&hadc1); //获取AD值
printf("MCU Temperature : %.1f C\r\n",((ADC0_Value*3300/4096-760)/2.5+25)); //打印芯片温度
HAL_ADC_Stop(&hadc1);
}
HAL_Delay(500);
}
/* USER CODE END 3 */
}
2.中断查询 非阻塞
float ADC0_Value = 0;
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{
ADC0_Value = HAL_ADC_GetValue(&hadc1);
}
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_ADC1_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
printf("STM32 ADC查中断非询阻塞模式 读取芯片温度\n");
HAL_ADC_Start_IT(&hadc1);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
printf("MCU Temperature : %.1f C\r\n",((ADC0_Value*3300/4096-760)/2.5+25)); //打印芯片温度
HAL_Delay(500);
}
/* USER CODE END 3 */
}