各位大神:
我用STM32F401 进行串口通信,UASRT1跟UASRT2使用正常,使用USART6时不正常,发送HELLO WORLD为乱码怎么破?
代码如下:
main
{
SystemInit();
/*定义一个GPIO_InitTypeDef类型的结构体*/
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStruct;
GPIO_StructInit(&GPIO_InitStructure);
//USART_DeInit(USART6);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); //开启GPIOC时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE); //开启USART6时钟
/*USART6管脚复用配置*/
GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_USART6);//配置复用的引脚,
GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_USART6);//
/*配置USART6管脚及相关参数*/
/*配置GPIOC_Pin6为USART6_TX:输出*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //设置为复用,必须为AF
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //没有上拉和下拉
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //输出推免
GPIO_Init(GPIOC,&GPIO_InitStructure);
/*配置GPIOC_Pin7为USART6_RX:输入*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //这也必须为复用,与M3不同!
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC,&GPIO_InitStructure);
/*配置USART6*/
USART_StructInit(&USART_InitStructure); //这个要屏蔽是不行的??
USART_InitStructure.USART_BaudRate = 115200;//345600;//115200;//
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(USART6, &USART_InitStructure);
/*使能USART6*/
USART_ClockStructInit(&USART_ClockInitStruct); //填充外设时钟默认值
USART_ClockInit(USART6, &USART_ClockInitStruct); //初始化外设时钟
USART_ITConfig(USART6, USART_IT_RXNE, ENABLE); //使能 USART1中断
USART_Cmd(USART6, ENABLE);
/*串口6中断配置*/
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//使能GPIOC时钟
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //嵌套优先级分组为 1
NVIC_InitStructure.NVIC_IRQChannel = USART6_IRQn; //嵌套通道为USART6_IRQn
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //抢占优先级为 0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //响应优先级为 0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //通道中断使能
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART6, USART_IT_RXNE, ENABLE);//开启中断
while
{
USART6_TX("Hello world!\n");
delay(0xFFFFF);
}
}
VOID USART6_TX(INT8 *usStr)
{
while (*usStr)
{
USART_SendData(USART6, *usStr++);
/* 循环发送知道结束*/
while (USART_GetFlagStatus(USART6, USART_FLAG_TXE) == RESET); //当TXE被置起时,一帧数据传输完成
}
}
VOID delay(UINT32 uDelayVal)
{
UINT32 uTemp = 0 ;
for(uTemp=0;uTemp<uDelayVal;uTemp++);
}
连接串口调试工具始终收到是乱码:5''!縌!'7诫 串口1跟串口2 同样的配置就正确。
各位大神们,这个是咋回事啊
|