四旋翼无人机从0到1的实现(十四)无人机MCU驱动→UART

Author:家有仙妻谢掌柜
Date:2021/2/18

今年会更新一个系列,小四轴无人机从功能设计→思维导图→原理图设计→PCBLayout→焊接PCB→程序代码的编写→整机调试一系列,以此记录自己的成长历程!
这个小四轴无人机是大学时期学习制作的,加上现在工作学习对嵌入式的理解更加深入,因此想要重新梳理一下小四轴,之后在此基础上实现大四轴的飞控设计,这些都将在工作之余完成!

//小四轴无人机设计,串口与接收机进行通讯
#include "uart.h"

/*******************************************************************************
 * fuction	uart1_init    
 * brief	串口1的配置初始化
 * param	无
 * return	无
 *******************************************************************************/  
void uart1_init(void)
{
    
    
	/*结构体变量定义*/
	GPIO_InitTypeDef  GPIO_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;					//中断嵌套控制函数	
	USART_InitTypeDef	USART_InitStructure;				//串口配置函数		
	/*开启引脚时钟*/
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);//开的是IO口的时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);//开的是uart的时钟
	/*开启引脚复用功能PA9/PA10*/
 	GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_7);
	GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_7);
	/*引脚的配置PA9/PA10*/
	GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_9|GPIO_Pin_10;
	GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
	GPIO_Init(GPIOA,&GPIO_InitStructure);		
	/*串口的配置*/
	USART_InitStructure.USART_BaudRate            = 115200;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; 
	USART_InitStructure.USART_Mode 								= USART_Mode_Rx|USART_Mode_Tx;
	USART_InitStructure.USART_Parity 							= USART_Parity_No;
	USART_InitStructure.USART_StopBits 						= USART_StopBits_1;
	USART_InitStructure.USART_WordLength 					= USART_WordLength_8b;
	USART_Init(USART1,&USART_InitStructure);
	/*串口的中断配置*/
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	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);
  	/*串口的接收中断使能*/
  	USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  	/*串口的使能*/
  	USART_Cmd(USART1,  ENABLE);
}
//中断服务程序     中断标志位需要软件清零
/*******************************************************************************
 * fuction	USART1_IRQHandler   
 * brief	串口1中断服务函数
 * param	无
 * return	无
 *******************************************************************************/  
void USART1_IRQHandler(void)
{
    
    
	static uint8_t i = 0;                          //静态变量
	/*判断中断标志位  */
	if(USART_GetITStatus(USART1,USART_IT_RXNE)==SET )
	{
    
    		
		USART_ClearITPendingBit(USART1,USART_IT_RXNE);       //中断标志位  清零
	 	RF_DATA[i]  = USART_ReceiveData(USART1);                                          // 0  1... 13
		bind_count ++;		
		if(RF_DATA[0]==0x66)
		{
    
    			
			i++;
			if(i==13)
			{
    
    
				RF_DATA_SUCCES = true;
				i = 0;
			}			
		}
		if((bind_flag==true)||(two_point_four_bind_flag == false))
		{
    
    
			bind_flag=false;
			if(RF_DATA[0]==0xAA)
			{
    
    			
				two_point_four_bind_flag = true;				
			}		
	  	}
	}
}




#ifndef _UART_H__
#define _UART_H__

#include "board_define.h"
#include "var_global.h"

void uart1_init(void);

#endif


猜你喜欢

转载自blog.csdn.net/FutureStudio1994/article/details/113854177