完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
#include "usart.h" /* * 函数名:USART1_Config * 串口1配置 需要用跳线连接板子上的(RXD-PA9,TXD-PA10),同时还要改变重映向(即改USARTx (x为1)) */ void USART1_Config(uint32_t MyBaudRate) { GPIO_InitTypeDef GPIO_InitStructure; // 定义结构体 USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE); // 开启GPIOA时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE); // 开启串口1时钟 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; // 配置TXD GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 复用推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA , &GPIO_InitStructure); // 初始化PA9 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; // 配置RXD GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; // 浮空输入 GPIO_Init(GPIOA , &GPIO_InitStructure); // 初始化PA10 USART_InitStructure.USART_BaudRate = MyBaudRate; // 配置串口波特率 USART_InitStructure.USART_WordLength = USART_WordLength_8b; // 8位数据位 USART_InitStructure.USART_StopBits = USART_StopBits_1; // 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; // 配置RX | TX模式 USART_Init(USART1,&USART_InitStructure); // 初始化串口 NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; // 配置中断源 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; // 抢占优先级 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // 响应优先级 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // 使能中断通道 NVIC_Init(&NVIC_InitStructure); // 初始化中断 USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); // 使能串口接收中断 USART_Cmd(USART1,ENABLE); } /* * 函数名:USART2_Config * 串口2配置 需要用跳线连接板子上的(RXD-PA2,TXD-PA3),同时还要改变重映向(即改USARTx (x为2)) */ void USART2_Config(uint32_t MyBaudRate) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 , ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; 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_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA , &GPIO_InitStructure); USART_InitStructure.USART_BaudRate = MyBaudRate; 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); NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); USART_Cmd(USART2,ENABLE); } /* * 函数名:USART3_Config * 串口3配置 需要用跳线连接板子上的(RXD-PB10,TXD-PB11),同时还要改变重映向(即改USARTx (x为3)) */ void USART3_Config(uint32_t MyBaudRate) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3 , ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB , &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOB , &GPIO_InitStructure); USART_InitStructure.USART_BaudRate = MyBaudRate; 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(USART3,&USART_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); USART_Cmd(USART3,ENABLE); } /* * 函数名:Usart_SendByte * 描述 :发送一个字节 */ void Usart_SendByte( USART_TypeDef * pUSARTx, uint8_t ch) { USART_SendData(pUSARTx,ch); /* 发送一个字节数据到USART */ while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET); /* 等待发送数据寄存器为空 */ } /* * 函数名:Usart_SendArray * 描述 :发送8位的数组 */ void Usart_SendArray( USART_TypeDef * pUSARTx, uint8_t *array, uint16_t num) { uint8_t i; for(i=0; i Usart_SendByte(pUSARTx,array); /* 发送数组到USART */ } while(USART_GetFlagStatus(pUSARTx,USART_FLAG_TC)==RESET); /* 等待发送完成 */ } /* * 函数名:Usart_SendString * 描述 :发送字符串 */ void Usart_SendString( USART_TypeDef * pUSARTx, char *str) { unsigned int k=0; do { Usart_SendByte( pUSARTx, *(str + k) ); k++; } while(*(str + k)!=' |