Design of the battery voltage detecting application
STM32 understand: https://blog.csdn.net/u010893262/article/details/68942710
A, ADC (Analog-to-Digital Converter) analog to digital converter
Analog -----> Digital Signal
Second, the operating parameters of the ADC peripheral configuration
Second, the operating parameters of the ADC peripheral configuration
Third, the configuration of the operating parameters USART peripherals
Fourth, generate code
Set the required variable in the main.c file
/ * The USER CODE the BEGIN the PV * / // the ADC converted value is stored uint16_t = adc_value 0 ; // voltage value of a float Voltage = 0.0 ; // voltage results show char voltString [ 50 ] = { 0 }; / * the USER CODE the END the PV * /
In the while loop
while (1) { // 启动ADC转换 HAL_ADC_Start(&hadc1); // 等待转换完成,第二个参数表示超时时间,单位ms HAL_ADC_PollForConversion(&hadc1, 100); // 为换取ADC状态 adc_value = HAL_ADC_GetValue(&hadc1); // 采取的右对齐除以2的12次方,参考电压为3.3V voltage = (float)adc_value / 4096 * 3.3; sprintf(voltString, "²É¼¯µ½µÄµçѹֵΪ: %.2f V", voltage); printf("%s\r\n", voltString); HAL_Delay(1000); /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ }
在USART.c添加
#include "usart.h" int fputc(int ch, FILE *f) { HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF); return ch; }
在USART.h添加
#include "main.h" #include <stdio.h>