编写stm32f103c8t6利用AD采集电压信号并通过串口输出的程序
时间: 2023-10-29 19:06:01 浏览: 339
以下是基于STM32CubeIDE的程序示例:
```c
#include "main.h"
#include "stdio.h"
/* ADC handle */
ADC_HandleTypeDef hadc1;
/* UART handle */
UART_HandleTypeDef huart1;
/* Function prototypes */
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
static void MX_ADC1_Init(void);
int main(void)
{
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
MX_ADC1_Init();
/* Infinite loop */
while (1)
{
/* Start ADC conversion */
HAL_ADC_Start(&hadc1);
/* Wait until ADC conversion is complete */
HAL_ADC_PollForConversion(&hadc1, 100);
/* Read ADC value */
uint16_t adc_value = HAL_ADC_GetValue(&hadc1);
/* Convert ADC value to voltage */
float voltage = adc_value * 3.3f / 4096.0f;
/* Print voltage value via UART */
char buffer[32];
sprintf(buffer, "Voltage: %.2fV\r\n", voltage);
HAL_UART_Transmit(&huart1, (uint8_t *)buffer, strlen(buffer), HAL_MAX_DELAY);
/* Delay before next conversion */
HAL_Delay(1000);
}
}
/* ADC1 Initialization Function */
static void MX_ADC1_Init(void)
{
ADC_ChannelConfTypeDef sConfig = {0};
/* Enable ADC clock */
__HAL_RCC_ADC1_CLK_ENABLE();
/* Configure ADC */
hadc1.Instance = ADC1;
hadc1.Init.ScanConvMode = DISABLE;
hadc1.Init.ContinuousConvMode = DISABLE;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.NbrOfConversion = 1;
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
Error_Handler();
}
/* Configure ADC channel */
sConfig.Channel = ADC_CHANNEL_0;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_13CYCLES_5;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
}
/* USART1 Initialization Function */
static void MX_USART1_UART_Init(void)
{
/* Enable UART clock */
__HAL_RCC_USART1_CLK_ENABLE();
/* Configure UART */
huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
}
/* GPIO Initialization Function */
static void MX_GPIO_Init(void)
{
/* Enable GPIO clock */
__HAL_RCC_GPIOA_CLK_ENABLE();
/* Configure UART TX pin */
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* Configure ADC input pin */
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
/* System Clock Configuration */
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
| RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
}
/* Error handler */
void Error_Handler(void)
{
while (1)
{
}
}
```
在这个程序示例中,我们使用了:
- ADC1模块,配置了ADC通道0以采集电压信号。
- USART1模块,用于串口输出电压值。
- GPIOA0作为ADC输入引脚,GPIOA9作为USART1的TX引脚。
在主函数中,我们采用了循环的方式,不断地采集电压值并通过串口输出。每次采集完成后,我们将ADC的值转换为电压值,并将其打印到串口上。注意,我们使用了HAL_Delay函数来控制每次采样之间的时间间隔。
需要注意的是,这个程序示例中并没有进行中断处理,因此采集速率可能会受到限制。如果需要更高的采样速率,建议使用中断方式进行ADC采样。
阅读全文