注意:串口名,接收中断函数名,以及各管脚的时钟函数,之间的不同。 极容易出错搞混
串口USART1
RX----PB7
TX----PB6
//初始化IO 串口1
//bound:波特率
void uart1_init(u32 bound){
//GPIO端口设置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE); //使能GPIOA时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//使能USART1时钟
//串口1对应引脚复用映射
// GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1); //GPIOA9复用为USART1
// GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1); //GPIOA10复用为USART1
GPIO_PinAFConfig(GPIOB,GPIO_PinSource6,GPIO_AF_USART1); //GPIOB6复用为USART1
GPIO_PinAFConfig(GPIOB,GPIO_PinSource7,GPIO_AF_USART1); //GPIOB7复用为USART1
//USART1端口配置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; //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(GPIOB,&GPIO_InitStructure); //初始化PA9,PA10
//USART1 初始化设置
USART_InitStructure.USART_BaudRate = bound;//波特率设置
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
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //开启相关中断
//Usart1 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;//串口1中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =3; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器、
}
void send1_buff(u8 *p, u8 len)
{
while(len--)
{
USART_SendData(USART1, *(p++)); //向串口1发送数据
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);//等待发送结束
}
}
void USART1_IRQHandler(void) //串口1中断服务程序
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //接收中断
{
rx1_data[rx1_n++] =USART_ReceiveData(USART1); //(USART1->DR); 读取接收到的数据
}
}
串口USART2
TX-----PA2
RX------PA3
//初始化IO 串口2
//bound:波特率
void uart2_init(u32 bound){
//GPIO端口设置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); //使能GPIOA时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);//使能USART2时钟
//串口2对应引脚复用映射
GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_USART2); //GPIOA2复用为USART2
GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_USART2); //GPIOA3复用为USART3
//USART2端口配置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; //GPIOA2与GPIOA3
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(GPIOA,&GPIO_InitStructure); //初始化PA2,PA3
//USART2 初始化设置
USART_InitStructure.USART_BaudRate = bound;//波特率设置
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(USART2, &USART_InitStructure); //初始化串口2
USART_Cmd(USART2, ENABLE); //使能串口2
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//开启相关中断
//Usart2 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;//串口2中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =3; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器、
}
void send2_buff(u8 *p, u8 len)
{
while(len--)
{
USART_SendData(USART2, *(p++)); //向串口2发送数据
while(USART_GetFlagStatus(USART2,USART_FLAG_TC)!=SET);//等待发送结束
}
}
void USART2_IRQHandler(void) //串口2中断服务程序
{
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //接收中断
{
rx2_data[rx2_n++] =USART_ReceiveData(USART2); //(USART2->DR); 读取接收到的数据
}
}
串口USART3
TX----------PB10
RX---------PB11
//初始化IO 串口3
//bound:波特率
void uart3_init(u32 bound){
//GPIO端口设置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE); //使能GPIOB时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);//使能USART3时钟
//串口3对应引脚复用映射
GPIO_PinAFConfig(GPIOB,GPIO_PinSource10,GPIO_AF_USART3); //GPIOB10复用为USART3
GPIO_PinAFConfig(GPIOB,GPIO_PinSource11,GPIO_AF_USART3); //GPIOB11复用为USART3
//USART3端口配置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11; //GPIOB10与GPIOB11
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(GPIOB,&GPIO_InitStructure); //初始化PB10,PB11
//USART3 初始化设置
USART_InitStructure.USART_BaudRate = bound;//波特率设置
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(USART3, &USART_InitStructure); //初始化串口3
USART_Cmd(USART3, ENABLE); //使能串口3
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//开启相关中断
//Usart3 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;//串口3中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =3; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器、
}
void send3_buff(u8 *p, u8 len)
{
while(len--)
{
USART_SendData(USART3, *(p++)); //向串口3发送数据
while(USART_GetFlagStatus(USART3,USART_FLAG_TC)!=SET);//等待发送结束
}
}
void USART3_IRQHandler(void) //串口3中断服务程序
{
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) //接收中断
{
rx3_data[rx3_n++] =USART_ReceiveData(USART3); //(USART3->DR); 读取接收到的数据
}
}
串口UART4
TX-----------------PA0
RX-----------------PA1
注意:串口名及接收中断函数名
//初始化IO 串口4
//bound:波特率
void uart4_init(u32 bound){
//GPIO端口设置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); //使能GPIOA时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4,ENABLE);//使能UART4时钟
//串口4对应引脚复用映射
GPIO_PinAFConfig(GPIOA,GPIO_PinSource0,GPIO_AF_UART4); //GPIOA0复用为UART4
GPIO_PinAFConfig(GPIOA,GPIO_PinSource1,GPIO_AF_UART4); //GPIOA1复用为UART4
//UART4端口配置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1; //GPIOA0与GPIOA1
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(GPIOA,&GPIO_InitStructure); //初始化PA0,PA1
//UART4 初始化设置
USART_InitStructure.USART_BaudRate = bound;//波特率设置
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(UART4, &USART_InitStructure); //初始化串口4
USART_Cmd(UART4, ENABLE); //使能串口4
USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);//开启相关中断
//Uart4 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;//串口4中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =3; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器、
}
void send4_buff(u8 *p, u8 len)
{
while(len--)
{
USART_SendData(UART4, *(p++)); //向串口4发送数据
while(USART_GetFlagStatus(UART4,USART_FLAG_TC)!=SET);//等待发送结束
}
}
void UART4_IRQHandler(void) //串口4中断服务程序
{
if(USART_GetITStatus(UART4, USART_IT_RXNE) != RESET) //接收中断
{
rx4_data[rx4_n++] =USART_ReceiveData(UART4); //(UART4->DR); 读取接收到的数据
}
}
串口UART5
RX--------PD2
TX--------PC12
//初始化IO 串口5
//bound:波特率
void uart5_init(u32 bound){
//GPIO端口设置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC|RCC_AHB1Periph_GPIOD,ENABLE); //使能GPIOC,D时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5,ENABLE); //使能UART5时钟
//串口5对应引脚复用映射
GPIO_PinAFConfig(GPIOD,GPIO_PinSource2,GPIO_AF_UART5); //GPIOD2复用为UART5
GPIO_PinAFConfig(GPIOC,GPIO_PinSource12,GPIO_AF_UART5); //GPIOC12复用为UART5
//UART5端口配置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //GPIOC12
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(GPIOC,&GPIO_InitStructure); //初始化PC12
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_Init(GPIOD,&GPIO_InitStructure);
//UART5 初始化设置
USART_InitStructure.USART_BaudRate = bound;//波特率设置
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(UART5, &USART_InitStructure); //初始化串口5
USART_Cmd(UART5, ENABLE); //使能串口5
USART_ITConfig(UART5, USART_IT_RXNE, ENABLE);//开启相关中断
//Uart5 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn;//串口5中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =3; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器、
}
void send5_buff(u8 *p, u8 len)
{
while(len--)
{
USART_SendData(UART5, *(p++)); //向串口5发送数据
while(USART_GetFlagStatus(UART5,USART_FLAG_TC)!=SET);//等待发送结束
}
}
void UART5_IRQHandler(void) //串口5中断服务程序
{
if(USART_GetITStatus(UART5, USART_IT_RXNE) != RESET) //接收中断
{
rx5_data[rx5_n++] =USART_ReceiveData(UART5); //(UART5->DR); 读取接收到的数据
}
}
串口UART6
RX--------------PC7
TX---------------PC6
//初始化IO 串口6
//bound:波特率
void uart6_init(u32 bound){
//GPIO端口设置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE); //使能GPIOC时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6,ENABLE); //使能USART6时钟
//串口6对应引脚复用映射
GPIO_PinAFConfig(GPIOC,GPIO_PinSource6,GPIO_AF_USART6); //GPIOC6复用为USART6
GPIO_PinAFConfig(GPIOC,GPIO_PinSource7,GPIO_AF_USART6); //GPIOC7复用为USART6
//UART6端口配置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7; //GPIOC6,7
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(GPIOC,&GPIO_InitStructure); //初始化PC6,7
//USART6 初始化设置
USART_InitStructure.USART_BaudRate = bound;//波特率设置
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(USART6, &USART_InitStructure); //初始化串口6
USART_Cmd(USART6, ENABLE); //使能串口6
USART_ITConfig(USART6, USART_IT_RXNE, ENABLE);//开启相关中断
//Usart6 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART6_IRQn;//串口6中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =3; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器、
}
void send6_buff(u8 *p, u8 len)
{
while(len--)
{
USART_SendData(USART6, *(p++)); //向串口6发送数据
while(USART_GetFlagStatus(USART6,USART_FLAG_TC)!=SET);//等待发送结束
}
}
void USART6_IRQHandler(void) //串口6中断服务程序
{
if(USART_GetITStatus(USART6, USART_IT_RXNE) != RESET) //接收中断
{
rx6_data[rx6_n++] =USART_ReceiveData(USART6); //(USART6->DR); 读取接收到的数据
}
}
串口UART7
RX------------PE7
TX------------PE8
//初始化IO 串口7
//bound:波特率
void uart7_init(u32 bound){
//GPIO端口设置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE,ENABLE); //使能GPIOE时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART7,ENABLE); //使能UART7时钟
//串口7对应引脚复用映射
GPIO_PinAFConfig(GPIOE,GPIO_PinSource7,GPIO_AF_UART7); //GPIOE7复用为UART7
GPIO_PinAFConfig(GPIOE,GPIO_PinSource8,GPIO_AF_UART7); //GPIOE8复用为UART7
//UART7端口配置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7|GPIO_Pin_8; //GPIOE7,8
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(GPIOE,&GPIO_InitStructure); //初始化PE7,8
//UART7 初始化设置
USART_InitStructure.USART_BaudRate = bound;//波特率设置
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(UART7, &USART_InitStructure); //初始化串口7
USART_Cmd(UART7, ENABLE); //使能串口7
USART_ITConfig(UART7, USART_IT_RXNE, ENABLE);//开启相关中断
//Uart7 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = UART7_IRQn;//串口7中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =3; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器、
}
void send7_buff(u8 *p, u8 len)
{
while(len--)
{
USART_SendData(UART7, *(p++)); //向串口7发送数据
while(USART_GetFlagStatus(UART7,USART_FLAG_TC)!=SET);//等待发送结束
}
}
void UART7_IRQHandler(void) //串口7中断服务程序
{
if(USART_GetITStatus(UART7, USART_IT_RXNE) != RESET) //接收中断
{
rx7_data[rx7_n++] =USART_ReceiveData(UART7); //(UART7->DR); 读取接收到的数据
}
}
串口UART8
TX-----------PE1
RX-----------PE0
//初始化IO 串口8
//bound:波特率
void uart8_init(u32 bound){
//GPIO端口设置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE,ENABLE); //使能GPIOE时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART8,ENABLE); //使能UART8时钟
//串口8对应引脚复用映射
GPIO_PinAFConfig(GPIOE,GPIO_PinSource0,GPIO_AF_UART8); //GPIOE0复用为UART8
GPIO_PinAFConfig(GPIOE,GPIO_PinSource1,GPIO_AF_UART8); //GPIOE1复用为UART8
//UART8端口配置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1; //GPIOE1,0
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(GPIOE,&GPIO_InitStructure); //初始化PE0,1
//UART8 初始化设置
USART_InitStructure.USART_BaudRate = bound;//波特率设置
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(UART8, &USART_InitStructure); //初始化串口8
USART_Cmd(UART8, ENABLE); //使能串口8
USART_ITConfig(UART8, USART_IT_RXNE, ENABLE);//开启相关中断
//Uart8 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = UART8_IRQn;//串口8中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =3; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器、
}
void send8_buff(u8 *p, u8 len)
{
while(len--)
{
USART_SendData(UART8, *(p++)); //向串口8发送数据
while(USART_GetFlagStatus(UART8,USART_FLAG_TC)!=SET);//等待发送结束
}
}
void UART8_IRQHandler(void) //串口8中断服务程序
{
if(USART_GetITStatus(UART8, USART_IT_RXNE) != RESET) //接收中断
{
rx8_data[rx8_n++] =USART_ReceiveData(UART8); //(UART8->DR); 读取接收到的数据
}
}
主函数MAIN()
#include <stm32f4xx.h>
u8 rx1_n=0; //串口1接收数据计数
u8 rx2_n=0; //串口2接收数据计数
u8 rx3_n=0; //串口3接收数据计数
u8 rx4_n=0;
u8 rx5_n=0;
u8 rx6_n=0;
u8 rx7_n=0;
u8 rx8_n=0;
u8 tx1_data[255]={0}; //串口1发送缓存
u8 rx1_data[255]={0}; //串口1接收缓存
u8 tx2_data[255]={0}; //串口2发送缓存
u8 rx2_data[255]={0}; //串口2接收缓存
u8 tx3_data[255]={0}; //串口3发送缓存
u8 rx3_data[255]={0}; //串口3接收缓存
u8 tx4_data[255]={0};
u8 rx4_data[255]={0};
u8 tx5_data[255]={0};
u8 rx5_data[255]={0};
u8 tx6_data[255]={0};
u8 rx6_data[255]={0};
u8 tx7_data[255]={0};
u8 rx7_data[255]={0};
u8 tx8_data[255]={0};
u8 rx8_data[255]={0};
int main(void)
{
u8 t,n,m,h;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
uart1_init(38400); //串口1 波特率=38400
uart2_init(38400); //串口2 波特率=38400
uart3_init(38400); //串口3 波特率=38400
uart4_init(38400);
uart5_init(38400);
uart6_init(38400);
uart7_init(38400);
uart8_init(38400);
while(1)
{
;
}
}