#include "usart1.h" #include "dev.h" #include "delay.h" #include "string.h" #define USART1_RX_MAX 40 #define USART1_TX_MAX 40 uint8_t USART1_RX_BUF[USART1_RX_MAX] = {0}; //接收缓存区 uint8_t USART1_TX_BUF[USART1_TX_MAX] = {0}; //发送缓存区 Uart_Type u1; void usart1_init(void) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); //使能GPIOA时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); //使能USART1时钟 GPIO_PinAFConfig(DBG_RXD_Port,GPIO_PinSource9,GPIO_AF_USART1); //GPIOA9复用为USART1 GPIO_PinAFConfig(DBG_TXD_Port,GPIO_PinSource10,GPIO_AF_USART1); //GPIOA10复用为USART1 GPIO_InitStructure.GPIO_Pin = DBG_RXD_Pin | DBG_TXD_Pin; //GPIOA9与GPIOA10 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //复用功能 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度50MHz GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉 GPIO_Init(DBG_RXD_Port,&GPIO_InitStructure); //初始化 USART_InitStructure.USART_BaudRate = U1_BAUDRETE; //波特率设置 USART_InitStructure.USART_WordLength = USART_WordLength_8b; //字长为8位数据格式 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(USART1, &USART_InitStructure); //初始化串口1 USART_Cmd(USART1, ENABLE); //使能串口1 } void usart1_send_byte(uint8_t one_byte) { while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)!=SET);//等待发送结束 USART_SendData(USART1,one_byte); //向串口3发送数据 } uint8_t usart1_read_byte(uint8_t* c) { if ( USART_GetFlagStatus(USART1, USART_FLAG_RXNE) != RESET) { *c = USART_ReceiveData(USART1); return 1; } else { return 0; } }