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.         }
复制代码


友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
2条回答
kagaya
2019-07-15 12:19
本帖最后由 kagaya 于 2014-4-16 10:47 编辑

上網查了一下

  1. #define HSE_VALUE    ((uint32_t)8000000)
复制代码

這部份是對的

  1.   float temp;
  2.   u16 mantissa;
  3.   u16 fraction;     
  4.   temp=(float)(pclk2*1000000)/(bound*16);//得到USARTDIV
  5.   mantissa=temp;              //得到整数部分
  6.   fraction=(temp-mantissa)*16; //得到小数部分
  7.   mantissa<<=4;
  8.   mantissa+=fraction;
  9.   USART1->BRR=mantissa; // 波特率设置
复制代码

上面的部份還看得懂
所以再檢查官方的原碼內容 stm32f10x_usart.c是否一樣

  1. /* Determine the integer part */
  2.   if ((USARTx->CR1 & CR1_OVER8_Set) != 0)
  3.   {
  4.     /* Integer part computing in case Oversampling mode is 8 Samples */
  5.     integerdivider = ((25 * apbclock) / (2 * (USART_InitStruct->USART_BaudRate)));   
  6.   }
  7.   else /* if ((USARTx->CR1 & CR1_OVER8_Set) == 0) */
  8.   {
  9.     /* Integer part computing in case Oversampling mode is 16 Samples */
  10.     integerdivider = ((25 * apbclock) / (4 * (USART_InitStruct->USART_BaudRate)));   
  11.   }
  12.   tmpreg = (integerdivider / 100) << 4;

  13.   /* Determine the fractional part */
  14.   fractionaldivider = integerdivider - (100 * (tmpreg >> 4));

  15.   /* Implement the fractional part in the register */
  16.   if ((USARTx->CR1 & CR1_OVER8_Set) != 0)
  17.   {
  18.     tmpreg |= ((((fractionaldivider * 8) + 50) / 100)) & ((uint8_t)0x07);
  19.   }
  20.   else /* if ((USARTx->CR1 & CR1_OVER8_Set) == 0) */
  21.   {
  22.     tmpreg |= ((((fractionaldivider * 16) + 50) / 100)) & ((uint8_t)0x0F);
  23.   }
  24.   
  25.   /* Write to USART BRR */
  26.   USARTx->BRR = (uint16_t)tmpreg;
复制代码
看來並不一樣
這個就看不太懂了
用apbclock乘上25再除以兩倍的USART_BaudRate........
它跟上面的程式會得到相同的USARTx->BRR ?
今天晚上回家再試試看

一周热门 更多>