在一个新项目中要使用串口发送大量数据,之前使用过的串口最高波特率只用到了115200,最开始想用115200*16=1843200波特率的,但收发数据的时候测试有部分字节会出错,最后改用1382400波特率测试收发数据都正常。
主函数测试程序:
u32 SysTime; //系统时钟上 每毫秒加1
u8 senddata[256]; //发送数据BUF
u32 TestTime;
int main(void)
{
u16 i ;
SysTickConfiguration();
DriveGpioInit();
DriveGpioInputDefault();
DriveGpioOutputDefault();
DriveUsarUnifyInit(kUART_COM6_UPPER,1382400,USART_WordLength_8b,USART_StopBits_1,USART_Parity_No); //初始化串口 1382400
CreatSystemManageTask();
for(i = 0; i < 256; i++)
{
senddata[i] = i; //给发送BUF赋值
}
while(1)
{
TaskPoll();
TaskTimerUpdate(SysTime);
if(SysTime >= TestTime)
{
TestTime = SysTime + 1000; //每秒钟发送一包串口数剧
DriveUsartComUnifySend(kUART_COM6_UPPER,senddata,256); //发送数据
}
DriveUsartUnifyDataSendLoop();
}
}
串口初始化函数:
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
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; //GPIOC6与GPIOC7
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,PC7
/* 配置串口硬件参数 */
USART_InitStructure.USART_BaudRate = baud; //波特率设置
USART_InitStructure.USART_WordLength = len; //字长为8位数据格式
USART_InitStructure.USART_StopBits = stopBit; //一个停止位
USART_InitStructure.USART_Parity = checkType; //无奇偶校验位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //无硬件数据流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
USART_Init(USART6, &USART_InitStructure); //初始化串口6
//用于判断是否接收完一包数据.
USART_ITConfig(USART6, USART_IT_IDLE, ENABLE); // 使能串口空闲中断
USART_Cmd(USART6, ENABLE); // 串口使能
上位串口调试助手收到MCU的数据:
MCU收到串口调试助手的数据: