2691|0

4

帖子

0

TA的资源

一粒金砂(中级)

楼主
 

串口波特率问题 [复制链接]

我串口设置的是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 != '\0'; 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\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}


#endif

/**
  * @}
  */

/**
  * @}
  */

/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/

此帖出自stm32/stm8论坛
点赞 关注
 

回复
举报
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/7 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表