串口波特率问题

2019-03-23 20:14发布

我串口设置的是9600的波特率,为什么发送到电脑的时候,串口调试助手接收,要设置成19200才能正常!求解! 下面是代码: /**
  ******************************************************************************
  * @file    GPIO/IOToggle/main.c
  * @author  MCD Application Team
  * @version V3.5.0
  * @date    08-April-2011
  * @brief   Main program body.
  ******************************************************************************
  * @attention
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  ******************************************************************************
  */ /* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include "Delay.h"
/** @addtogroup STM32F10x_StdPeriph_Examples
  * @{
  */ /** @addtogroup GPIO_IOToggle
  * @{
  */ /* Private typedef -----------------------------------------------------------*/
unsigned char TxBuf1[100] = "12345";
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/ /* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void GPIO_Configuration(void);
void USART_Configuration(void);
/* Private functions ---------------------------------------------------------*/ /**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)

  int i ;
  delay_init(72); 
  RCC_Configuration();
  GPIO_Configuration();
  USART_Configuration();   while (1)
  {
   //******LED1**********//
    GPIO_ResetBits(GPIOD,GPIO_Pin_2);
   GPIO_SetBits(GPIOD,GPIO_Pin_3);
   GPIO_SetBits(GPIOD,GPIO_Pin_4);
   GPIO_SetBits(GPIOD,GPIO_Pin_7);
   delay_ms(500);
   //******LED2**********//
   GPIO_ResetBits(GPIOD,GPIO_Pin_3);
   GPIO_SetBits(GPIOD,GPIO_Pin_2);
   GPIO_SetBits(GPIOD,GPIO_Pin_4);
   GPIO_SetBits(GPIOD,GPIO_Pin_7);
   delay_ms(500);
   //******LED3**********//
   GPIO_ResetBits(GPIOD,GPIO_Pin_4);
   GPIO_SetBits(GPIOD,GPIO_Pin_3);
   GPIO_SetBits(GPIOD,GPIO_Pin_2);
   GPIO_SetBits(GPIOD,GPIO_Pin_7);
   delay_ms(500);
   //******LED4**********//
   GPIO_ResetBits(GPIOD,GPIO_Pin_7);
   GPIO_SetBits(GPIOD,GPIO_Pin_3);
   GPIO_SetBits(GPIOD,GPIO_Pin_4);
   GPIO_SetBits(GPIOD,GPIO_Pin_2);
   delay_ms(500);
      for( i = 0; TxBuf1 != ''; i++) {
        USART_SendData(USART1 , TxBuf1);
        while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET);
      }
  }
}
//-----------------------
void RCC_Configuration(void)
{
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOD, ENABLE);
    /*使能串口1时钟*/
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
}
//-----------------------
void GPIO_Configuration(void)
{
    GPIO_InitTypeDef    GPIO_InitStructure;
    /*配置USART1->TX 推挽输出-TX*/
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(GPIOA , &GPIO_InitStructure);
    /*配置USART1->RX*/
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    /*配置LED*/
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_2 | GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_7;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_Init(GPIOD, &GPIO_InitStructure);
}
//----------------
void USART_Configuration(void)
{
    USART_InitTypeDef USART_InitStructure;     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_Mode = USART_Mode_Tx | USART_Mode_Rx;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;     /*配置串口1*/
    USART_Init(USART1 , &USART_InitStructure);
    /*使能串口1的发送和接受中断*/
    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); 
    USART_ITConfig(USART1, USART_IT_TXE, ENABLE); 
    /*使能串口1*/
    USART_Cmd(USART1 , ENABLE);
}

#ifdef  USE_FULL_ASSERT /**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d ", file, line) */   /* Infinite loop */
  while (1)
  {
  }
}
#endif /**
  * @}
  */ /**
  * @}
  */ /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
此帖出自小平头技术问答
0条回答

一周热门 更多>