STM32串口發送到PC機得到的結果不符

2019-07-14 23:47发布


最近開始學STM32 想實驗STM32串口發送到PC機
源碼如下 會循環發送 12 34 到PC機(PC機也是9600)
實驗的結果是PC機一直顯示 60 F8
不知為何會如此? 我的程序有缺少什麼嗎?
謝謝

  1. GPIO_InitTypeDef GPIO_InitStructure;
  2.         USART_InitTypeDef USART_InitStructure;

  3.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
  4.        
  5.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  6.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  7.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  8.         GPIO_Init(GPIOA, &GPIO_InitStructure);
  9.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  10.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOAtiNG;
  11.         GPIO_Init(GPIOA, &GPIO_InitStructure);

  12.         USART_InitStructure.USART_BaudRate = 9600;
  13.         USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  14.         USART_InitStructure.USART_StopBits = USART_StopBits_1;
  15.         USART_InitStructure.USART_Parity = USART_Parity_No ;
  16.         USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  17.         USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  18.         USART_Init(USART1, &USART_InitStructure);
  19.         USART_Cmd(USART1, ENABLE);
  20.        
  21.         USART_ClearFlag(USART1,USART_FLAG_TC);
  22.        
  23.         while(1)
  24.         {
  25.                 USART1->DR = 0x1234;
  26.                 while( USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET );
  27.                 Delay(0xfffff);
  28.                 Delay(0xfffff);
  29.         }
复制代码


友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。