STM32 串口打印ADC采集电压
扫描二维码
随时随地手机看文章
ADC是单片机也是STM32在工控上常用功能之一,用来采集电压,温度等作为指标提供给其他部分进行对应的操作,使用32的固件库进行配置相当方便。
这里利用串口打印的方式观察采集到的电压。
#include "adc.h"
void adc_init()
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1;//ADC GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AIN; // GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
ADC_InitStructure.ADC_Mode =ADC_Mode_Independent;//独立工作ADC_InitStructure.ADC_ScanConvMode = DISABLE; //单次扫描 ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //单次转换 ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;//软件启动
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//右对齐数据 ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1,ADC_SampleTime_7Cycles5);//通道规则组设置
ADC_Cmd(ADC1,ENABLE);
ADC_ResetCalibration(ADC1);//ADC寄存器重置
while(ADC_GetResetCalibrationStatus(ADC1));//获取重置状态
ADC_StartCalibration(ADC1);//开启指定校准寄存器
while(ADC_GetCalibrationStatus(ADC1));//获取校准
ADC_SoftwareStartConvCmd(ADC1, ENABLE);//开启软件启动
}
#include "printf.h"
int fputc(int ch,FILE *p) //
{
USART_SendData(USART1,(u8)ch);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
return ch;
}
void printf_init()
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;
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_Mode_Rx;
// USART_InitStructure.USART_Clock = USART_Clock_Disable;
// USART_InitStructure.USART_CPOL = USART_CPOL_High;
// USART_InitStructure.USART_CPHA = USART_CPHA_1Edge;
// USART_InitStructure.USART_LastBit = USART_LastBit_Enable;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_ClearFlag(USART1,USART_FLAG_TC);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
int main()
{
u8 i = 0;
float ad = 0;
printf_init();
adc_init();
while(1){
for(i = 0;i < 10;i++){
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
while(!ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC));//转换结束标志
ad=ad+ADC_GetConversionValue(ADC1);//累加转换结果
}
ad /= 10;
printf("ad : %.3f Vrn",ad*3.3/4096);
delay_ms(1000);
delay_ms(1000);
}
}