我这里有个双串口的,你可以看看……
串口1收到回车表示接收完成,串口二只收起个字节!#include "uart.h"
u8 receive1[50];
u8 reflag1;
u8 recount1;
u8 receive2[10];
u8 reflag2;
u8 recount2;
u8 sendflag;
void UART_INIT_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 , ENABLE); //使能串口2时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_2 ; //串口2 TX引脚 PA2
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_3; //串口2 RX引脚 PA3
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;
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(USART1, &USART_InitStructure);
USART_InitStructure.USART_BaudRate = 19200;
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); //串口2 配置
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //串口2 中断
USART_Cmd(USART1, ENABLE);
USART_Cmd(USART2, ENABLE); //串口2 使能
USART_ClearFlag(USART1, USART_FLAG_TC);
USART_ClearFlag(USART2, USART_FLAG_TC);
}
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
receive1[recount1++] = USART_ReceiveData(USART1);
}
if(sendflag && !strstr((char *)receive1, ">"))
{
sendflag = 0;
recount1 = 0;
}
else
{
if(receive1[recount1 - 2] == '\x00D' && receive1[recount1 - 1] == '\x00A' && recount1 >= 2)
{
reflag1 = 1;
recount1 = 0;
}
else if(recount1 >= 30)
{
recount1=0;
clearstring(receive1);
}
}
}
void USART2_IRQHandler(void)
{
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
USART_ClearITPendingBit(USART2, USART_IT_RXNE);
receive2[recount2++] = USART_ReceiveData(USART2);
}
if(receive2[0] == 0 && receive2[1] == 0 && recount2 == 2)
{
recount2 = 1;
receive2[1] = 0;
}
else if(receive2[0] == 0 && receive2[6] == 0xFF && recount2 == 7)
{
reflag2 = 1;
recount2 = 0;
}
}
void sendstring(u8 *p)
{
while(*p)
{
USART_SendData(USART1, *p++);
while (!(USART1->SR & USART_FLAG_TXE));
}
}
void clearstring(u8 *p)
{
while(*p)
{
*p = '\0';
p++;
}
}
|