各位大神: 我用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 同样的配置就正确。 各位大神们,这个是咋回事啊 |
NUCLEO-F411RE板串口通讯问题
有人知道stm32cubemx中,如何自定义lwip平台。(默认只有一个lan和dp的芯片,我需要用yt8512的)。或者说cubemx生成的lan芯片的文件如
stm32f407 开启两路(FS和HS) Custom HID,与上位机通信偶尔中断
为什么自协商一直超时呢
RTL8201这款PHY在RMII模式下,外部晶振应该是25M合适还是50M合适呢?应该接在哪个引脚呢?
stm32 f405和stm32 f042 晶体管/mos数量
STM32CUBE ETH MII模式问题
STM32F429IIT6上电时引脚会上拉?
STM32F407 Custom HID在win11上被识别成条形码读取器
STM32F4 在APP运行时未知原因导致IAP程序被修改,求解。
说真的,我就是按照两个一模一样弄得,USART6就是不行,USART1就是没问题,愁死了~~,硬件是一个连的3845,一个是单纯的串口
你连的是485啊 这样问题我调试485遇到过,485接受发松那个控制脚要加延时,如果延时不够就有乱码,你试下
1. USART6的配置不正确,比如波特率、数据位、停止位等参数设置不正确;
2. USART6的硬件连接不正确,比如接线错误或者接触不良;
3. USART6的中断未正确配置,比如中断优先级设置不正确;
4. USART6的软件编程不正确,比如发送和接收函数的调用顺序不正确;
5. USART6的外设时钟未正确配置,比如时钟源选择不正确。
void USART6_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_USART6);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_USART6);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 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);
USART_Cmd(USART6, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = USART6_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART6, USART_IT_RXNE, ENABLE);
}
//发送数据
void USART6_SendData(uint8_t data)
{
USART_SendData(USART6, data);
while(USART_GetFlagStatus(USART6, USART_FLAG_TC) == RESET);
}