STM32
直播中

万航渡路

8年用户 1295经验值
擅长:电源/新能源
私信 关注
[问答]

开发环境coocox,stm32f429串口输出总是乱码的原因?

  一个小小的串口居然让我捣鼓了两天还没出来,时钟正确,usart寄存器值也是对的。然后试了试源码居然一样乱码。不知道是怎么了。板子是官方的论坛送的STM32f429zit6。下面贴上代码和源码。
#include  quot;stm32f4xx_usart.h quot;
#include  quot;stm32f4xx_gpio.h quot;
#include  quot;stm32f4xx_rcc.h quot;
#include  quot;stm32f4xx_flash.h quot;
#include  quot;stm32f4xx_pwr.h quot;
#include  quot;stm32f4xx_exti.h quot;
#include  quot;misc.h quot;
#include
#include
#include
void USART_GPIO_Config(void);
void USART_Config(void);
void USART_SendString(char *str);
void RCC_Config(void);
void uart3_init(void);
int main(void)
{
SystemInit();
uart3_init();
//USART_GPIO_Config();
USART_Config();
USART_SendData(USART3, '9');
while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET)
       {}
    while(1)
    {
     USART_SendData(USART3, 'a');
     while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET)
       {}
     //USART_SendString( quot;hello worldr quot;);
     //Array[num++] = USART_ReceiveData(USART1);
     //printf( quot;hello world/r quot;);
    }
}
void uart3_init(void)
{
  USART_InitTypeDef USART_InitStructure;
  GPIO_InitTypeDef GPIO_InitStructure;
  /* Enable GPIO clock */
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
  /* Enable USART clock */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
  /* Connect USART pins to AF7 */
  GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_USART3);
  GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_USART3);
  /* Configure USART Tx and Rx as alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  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,  amp;GPIO_InitStructure);
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
     GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
     GPIO_Init(GPIOC,  amp;GPIO_InitStructure);
}
void USART_SendString(char *str)
{
while(*str!=0)
{
  while(RESET == USART_GetFlagStatus(USART1,USART_FLAG_TXE));
     USART_SendData(USART1, *str++);
}
}

void USART_Config(void)
{
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;

RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
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_Tx | USART_Mode_Rx;
USART_Init(USART3,  amp;USART_InitStructure);

   /* NVIC configuration */
   /* Configure the Priority Group to 2 bits */
   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
   /* Enable the USARTx Interrupt */
   NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
   NVIC_Init( amp;NVIC_InitStructure);
   USART_ITConfig(USART1, USART_IT_TXE|USART_IT_RXNE, ENABLE);
    //USART_ITConfig(USART1,USART_IT_CTS, DISABLE);
    USART_Cmd(USART3, ENABLE);
}

void uart_init(u32 pclk2,u32 bound)
{
float temp;
u16 mantissa;
u16 fraction;
temp=(float)(pclk2*1000000)/(bound*16);
mantissa=temp;
fraction=(temp-mantissa)*16;
    mantissa

回帖(1)

刘燕

2024-5-17 18:11:13
根据您提供的信息,您在使用STM32F429开发环境时遇到了串口输出乱码的问题。在解决这个问题之前,我们需要确保以下几个方面都已经正确设置:

1. 时钟配置:确保已经正确配置了系统时钟和串口时钟。

2. GPIO配置:确保串口的TX和RX引脚已经正确配置为复用功能。

3. USART配置:确保USART的波特率、数据位、停止位和奇偶校验位等参数已经正确设置。

4. 串口初始化:确保已经正确初始化了串口。

5. 串口中断:如果使用中断方式发送数据,请确保中断服务程序正确实现。

以下是一些建议,帮助您排查和解决问题:

1. 检查串口波特率是否与接收设备匹配。波特率不匹配可能导致乱码。

2. 检查串口引脚是否正确连接。确保TX和RX引脚没有交叉连接。

3. 检查串口初始化函数中的参数设置,确保与您的硬件配置相匹配。

4. 如果使用中断方式发送数据,请检查中断服务程序是否正确实现。

5. 尝试使用轮询方式发送数据,以排除中断服务程序的问题。

6. 使用示波器或逻辑分析仪检查串口TX引脚的信号,以确定是否为硬件问题。

7. 确保您的串口接收设备(如电脑串口助手)的串口参数与STM32F429的串口参数相匹配。

8. 检查代码中是否有其他可能导致乱码的部分,如错误的数据发送逻辑。

9. 尝试使用其他串口库或示例代码,以排除当前串口库的问题。


举报

更多回帖

发帖
×
20
完善资料,
赚取积分