使用 STM32F103C8T6 的 ADC 模块读取9路电压值,并通过串口发送的示例工程代码
时间: 2024-03-15 22:46:02 浏览: 98
STM32 ADC 电压读取 串口输出数字电压和模拟电压
以下是使用 STM32F103C8T6 的 ADC 模块读取9路电压值,并通过串口发送的示例工程代码,你可以参考一下:
```c
#include "stm32f10x.h"
#include "stdio.h"
#define ADC1_DR_Address ((u32)0x4001244C)
#define USARTx USART1
#define USARTx_IRQn USART1_IRQn
#define USARTx_IRQHandler USART1_IRQHandler
void RCC_Configuration(void);
void GPIO_Configuration(void);
void USART_Configuration(void);
void NVIC_Configuration(void);
void ADC1_Configuration(void);
void USARTx_SendByte(u8 byte);
void USARTx_SendString(u8 *str);
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
USART_Configuration();
NVIC_Configuration();
ADC1_Configuration();
while (1)
{
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
while (!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC))
;
u16 adc_value[9];
for (int i = 0; i < 9; i++)
{
adc_value[i] = ADC_GetConversionValue(ADC1);
}
USARTx_SendString("ADC values:\r\n");
char buffer[20];
for (int i = 0; i < 9; i++)
{
sprintf(buffer, "ADC%d: %d\r\n", i + 1, adc_value[i]);
USARTx_SendString(buffer);
}
USARTx_SendString("\r\n");
delay_ms(1000);
}
}
void RCC_Configuration(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_ADC1 | RCC_APB2Periph_USART1, ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx;
USART_Init(USARTx, &USART_InitStructure);
USART_Cmd(USARTx, ENABLE);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel = USARTx_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void ADC1_Configuration(void)
{
ADC_InitTypeDef ADC_InitStructure;
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 9;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_239Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 2, ADC_SampleTime_239Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 3, ADC_SampleTime_239Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 4, ADC_SampleTime_239Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 5, ADC_SampleTime_239Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 6, ADC_SampleTime_239Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 7, ADC_SampleTime_239Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_7, 8, ADC_SampleTime_239Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 9, ADC_SampleTime_239Cycles5);
ADC_DMACmd(ADC1, ENABLE);
ADC_Cmd(ADC1, ENABLE);
ADC_ResetCalibration(ADC1);
while (ADC_GetResetCalibrationStatus(ADC1))
;
ADC_StartCalibration(ADC1);
while (ADC_GetCalibrationStatus(ADC1))
;
}
void USARTx_SendByte(u8 byte)
{
while (USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET)
;
USART_SendData(USARTx, byte);
while (USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET)
;
}
void USARTx_SendString(u8 *str)
{
while (*str)
{
USARTx_SendByte(*str++);
}
}
void delay_ms(u32 ms)
{
u32 i;
while (ms--)
{
i = 12000;
while (i--)
;
}
}
void USARTx_IRQHandler(void)
{
if (USART_GetITStatus(USARTx, USART_IT_RXNE) != RESET)
{
USART_ReceiveData(USARTx);
}
}
```
这个例子使用 PA0~PA8 引脚读取9路电压值,使用 ADC1 模块进行转换,然后通过串口 USART1 发送出来。你可以根据自己的需求修改引脚和串口参数。
阅读全文