本帖最后由 xiaoqi976633690 于 2024-6-22 14:58 编辑
一、实验目的
通过ADC1获取板上3个电位器的实时电压参数并打印串口显示
原理图如下
对应关系
ADC_Channel_1:PA1(RV1)
ADC_Channel_4:PA4(RV2)
ADC_Channel_5:PA5(RV3)
二、实验代码
1.初始化ADC函数实现
void ADC_Configure(void)
{
ADC_InitTypeDef ADC_InitStruct;
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
ADC_CalibrationConfig(ADC1, 0x1FE);
ADC_StructInit(&ADC_InitStruct);
ADC_InitStruct.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStruct.ADC_Prescaler = ADC_Prescaler_16;
ADC_InitStruct.ADC_Mode = ADC_Mode_Scan;
ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right;
ADC_Init(ADC1, &ADC_InitStruct);
ADC_SampleTimeConfig(ADC1, ADC_Channel_1, ADC_SampleTime_240_5);
ADC_SampleTimeConfig(ADC1, ADC_Channel_4, ADC_SampleTime_240_5);
ADC_SampleTimeConfig(ADC1, ADC_Channel_5, ADC_SampleTime_240_5);
ADC_ChannelCmd(ADC1, ADC_Channel_1, ENABLE);
ADC_ChannelCmd(ADC1, ADC_Channel_4, ENABLE);
ADC_ChannelCmd(ADC1, ADC_Channel_5, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
/* PA1(RV1) PA4(RV2) PA5(RV3) */
GPIO_StructInit(&GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_4 | GPIO_Pin_5;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_High;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStruct);
ADC_Cmd(ADC1, ENABLE);
}
初始化步骤:
a.定义ADC结构体和GPIO结构体。
b.开启ADC时钟。c.通过ADC_CalibrationConfig(ADC1, 0x1FE); 函数校准adc。
d.初始化adc的功能模式 12bit分辨率、分频系数16、单周期扫描模式、数据右对齐模式。
e.采样时间配置
f.使能channel
g.开启GPIO时钟,初始化GPIO模式为模拟输入。
2.ADC channel 数值获取函数实现
初始化完成后就可以编写adc值获取函数了
uint16_t Get_ADC( uint8_t channel)
{
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
while (RESET == ADC_GetFlagStatus(ADC1, ADC_FLAG_EOS)){ }
ADC_ClearFlag(ADC1, ADC_FLAG_EOS);
return ADC_GetChannelConvertedValue(ADC1, channel) ;
}
首先开启转换,等待转换完成后,清除标志位,再返回通道数据。
3.串口初始化
串口初始化比较简单
主要关注板子串口1是通过PA9,PA10 和板载ch340连接的,注意GPIO复用
void PLATFORM_InitConsole(uint32_t Baudrate)
{
GPIO_InitTypeDef GPIO_InitStruct;
UART_InitTypeDef UART_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_UART1, ENABLE);
UART_StructInit(&UART_InitStruct);
UART_InitStruct.BaudRate = Baudrate;
UART_InitStruct.WordLength = UART_WordLength_8b;
UART_InitStruct.StopBits = UART_StopBits_1;
UART_InitStruct.Parity = UART_Parity_No;
UART_InitStruct.HWFlowControl = UART_HWFlowControl_None;
UART_InitStruct.Mode = UART_Mode_Tx|UART_Mode_Rx;
UART_Init(UART1, &UART_InitStruct);
UART_Cmd(UART1, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_7);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_7);
GPIO_StructInit(&GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_10;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_High;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStruct);
}
4.main函数实现int main(void)
{
float voltage=0.0;
PLATFORM_Init();
ADC_Configure();
while (1)
{
PLATFORM_DelayMS(500);
voltage=Get_ADC(ADC_Channel_1)*3.3/4096;
printf("CH1=%0.2f ",voltage);
voltage=Get_ADC(ADC_Channel_4)*3.3/4096;
printf("CH4=%0.2f ",voltage);
voltage=Get_ADC(ADC_Channel_5)*3.3/4096;
printf("CH4=%0.2f\n\r",voltage);
}
}
三、实验结果
依次扭转3个电位器电压实时变化状态
CH1=0.00 CH4=0.00 CH4=0.00
CH1=0.00 CH4=0.00 CH4=0.00
CH1=0.00 CH4=0.00 CH4=0.00
CH1=0.00 CH4=0.00 CH4=0.00
CH1=0.00 CH4=0.00 CH4=0.00
CH1=0.00 CH4=0.00 CH4=0.00
CH1=0.00 CH4=0.00 CH4=0.00
CH1=1.16 CH4=0.00 CH4=0.00
CH1=1.21 CH4=0.00 CH4=0.00
CH1=1.99 CH4=0.00 CH4=0.00
CH1=2.23 CH4=0.00 CH4=0.00
CH1=2.32 CH4=0.00 CH4=0.00
CH1=2.32 CH4=0.00 CH4=0.00
CH1=2.32 CH4=0.00 CH4=0.00
CH1=2.32 CH4=0.00 CH4=0.00
CH1=2.32 CH4=0.00 CH4=0.00
CH1=2.32 CH4=0.00 CH4=0.00
CH1=2.32 CH4=0.00 CH4=0.00
CH1=2.32 CH4=0.00 CH4=0.00
CH1=2.32 CH4=0.17 CH4=0.00
CH1=2.32 CH4=0.62 CH4=0.00
CH1=2.32 CH4=0.61 CH4=0.00
CH1=2.32 CH4=0.61 CH4=0.00
CH1=2.32 CH4=0.61 CH4=0.00
CH1=2.32 CH4=0.61 CH4=0.35
CH1=2.32 CH4=0.61 CH4=0.84
CH1=2.32 CH4=0.61 CH4=0.84
CH1=2.32 CH4=0.61 CH4=0.84
CH1=2.32 CH4=0.61 CH4=0.84
CH1=2.32 CH4=0.61 CH4=0.84
CH1=2.32 CH4=0.61 CH4=0.84
|