STM32学习笔记-ADC采集-串口2发送至上位机

首先感谢河北科技大学航模协会-能给我这样的学习机会-感谢那些带我们教我们的学长学姐们.

串口2的配置(一根杜邦线不行纠结了半天)

void USART2_Initialise( u32 bound )
{
    GPIO_InitTypeDef GPIO_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
    USART_InitTypeDef USART_InitStructure;
    
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO,ENABLE);

	/* Enable the USART2 Pins Software Remapping */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); 
    
    
    /* Configure USART2 Rx (PA.03) as input floating */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;    
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    
    /* Configure USART2 Tx (PA.02) as alternate function push-pull */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    
    /* Enable the USART2 Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);    
    
    USART_InitStructure.USART_BaudRate = bound;                
    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_Rx | USART_Mode_Tx;

    
    USART_Init(USART2, &USART_InitStructure);
    USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
    USART_Cmd(USART2, ENABLE);
}

原理与串口一相同,只是io口不同,所在总线不同

ADC1的配置

void  Adc_Init(void)
{ 	
	ADC_InitTypeDef ADC_InitStructure; 
	GPIO_InitTypeDef GPIO_InitStructure;

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |RCC_APB2Periph_ADC1, ENABLE );	
 
	RCC_ADCCLKConfig(RCC_PCLK2_Div6);  
                   
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;	
	GPIO_Init(GPIOA, &GPIO_InitStructure);	

	ADC_DeInit(ADC1);  

	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_Cmd(ADC1, ENABLE);	
	
	ADC_ResetCalibration(ADC1);
	 
	while(ADC_GetResetCalibrationStatus(ADC1));	
	
	ADC_StartCalibration(ADC1);	
 
	while(ADC_GetCalibrationStatus(ADC1));

}				  

io口配置与点亮第一个led灯类似,模拟输入模式即可

与配置其他功能类似,ADC的配置也是一个结构体变量传入参数

ADC_InitTypeDef结构体变量名

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |RCC_APB2Periph_ADC1, ENABLE );

使能ADC1通道时钟


RCC_ADCCLKConfig(RCC_PCLK2_Div6); 设置ADC分频因子 72/6=12M


ADC_DeInit(ADC1);复位ADC


ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;ADC工作模式ADC1和ADC2工作在独立模式


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数据对齐方式


ADC_InitStructure.ADC_NbrOfChannel = 1;顺序进行转换的ADC通道数


ADC_Init(ADC1, &ADC_InitStructure);传入结构体地址,初始化ADC


 ADC_Cmd(ADC1, ENABLE);使能ADC


ADC_ResetCalibration(ADC1);使能复位校准


while(ADC_GetResetCalibrationStatus(ADC1));等待复位校准结束


ADC_StartCalibration(ADC1);开启AD校准


while(ADC_GetCalibrationStatus(ADC1));等待校准结束


获取ADC函数传入参数adc通道号

u16 Get_Adc(u8 ch)   
{
  
	ADC_RegularChannelConfig(ADC1, ch, 1, ADC_SampleTime_239Cycles5 );	  			    
  
	ADC_SoftwareStartConvCmd(ADC1, ENABLE);	
	 
	while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC ));

	return ADC_GetConversionValue(ADC1);
}

ADC_RegularChannelConfig(ADC1, ch, 1, ADC_SampleTime_239Cycles5 );ADC1,通道1,采样时间为239.5个周期

ADC_SoftwareStartConvCmd(ADC1, ENABLE);使能指定ADC1软件转换启动功能

while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC ));等待转换结束

return ADC_GetConversionValue(ADC1);返回最后一次ADC的转换结果



ADC通道对应io口如图

STM32学习笔记-ADC采集-串口2发送至上位机_第1张图片

         p=adcx;
	 p=(3.3/4096)*p;
         p=p*10000;

         v=(int)p;
	
         USART_SendData(USART2,0x76);
	 while(USART_GetFlagStatus(USART2,USART_FLAG_TC)!=SET);
	 USART_SendData(USART2,0x3D);
         while(USART_GetFlagStatus(USART2,USART_FLAG_TC)!=SET);
	 USART_SendData(USART2,(48+v/10000));
	 while(USART_GetFlagStatus(USART2,USART_FLAG_TC)!=SET);
	 USART_SendData(USART2,0x2E);
	 while(USART_GetFlagStatus(USART2,USART_FLAG_TC)!=SET);
	 USART_SendData(USART2,(48+v%10000/1000));
	 while(USART_GetFlagStatus(USART2,USART_FLAG_TC)!=SET);
	 USART_SendData(USART2,(48+v%10000%1000/100));
	 while(USART_GetFlagStatus(USART2,USART_FLAG_TC)!=SET);
	 USART_SendData(USART2,(48+v%10000%1000%100/10));
	 while(USART_GetFlagStatus(USART2,USART_FLAG_TC)!=SET);
	 USART_SendData(USART2,(48+v%10000%1000%100%10));		
	 while(USART_GetFlagStatus(USART2,USART_FLAG_TC)!=SET);
	 USART_SendData(USART2,0x0A);
	 while(USART_GetFlagStatus(USART2,USART_FLAG_TC)!=SET); 

采集adc以ASCII方式发送给上位机

while(USART_GetFlagStatus(USART2,USART_FLAG_TC)!=SET);等待发送完成

STM32学习笔记-ADC采集-串口2发送至上位机_第2张图片


声明:转载请注明出处,本人入门级菜鸟,内容大多学习于网络,存在问题太多不一一列举,大佬多多指教.本人QQ941131649








你可能感兴趣的:(stm32)