STM32F107串口乱码问题?

[复制链接]
4396|9
 楼主| shijieqiji 发表于 2012-8-6 15:20 | 显示全部楼层 |阅读模式
用KEIL创建一个基于官方3.5版本的固件库文件串口发送工程,波特率9600但是串口软件收到的数据始终是乱码,请版主解答下,先谢谢了!void USART_InitConfig(void) {   

GPIO_InitTypeDef GPIO_InitStructure;

USART_InitTypeDef USART_InitStructure;
//
GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);

/* 第1步:打开GPIO和USART部件的时钟 */

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);



/* 第2步:将USART Tx的GPIO配置为推挽复用模式 */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOA, &GPIO_InitStructure);



/* 第3步:将USART Rx的GPIO配置为浮空输入模式

由于CPU复位后,GPIO缺省都是浮空输入模式,因此下面这个步骤不是必须的

*/

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

GPIO_Init(GPIOA, &GPIO_InitStructure);



/* 第4步:配置USART参数

    - BaudRate = 115200 baud

    - Word Length = 8 Bits

    - One Stop Bit

    - No parity

    - Hardware flow control disabled (RTS and CTS signals)

    - Receive and transmit enabled

*/

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_Rx | USART_Mode_Tx;

USART_Init(USART1, &USART_InitStructure);






/* 第5步:使能 USART, 配置完毕 */

USART_Cmd(USART1, ENABLE);




}
香水城 发表于 2012-8-6 15:29 | 显示全部楼层
数据长度是包括停止位的,你配置的8,去掉停止位就只有7位数据长度了,复合你的需求吗?
 楼主| shijieqiji 发表于 2012-8-6 15:34 | 显示全部楼层
符合啊!补充下我的外部晶振是25M,库文件哪里需要修改吗?
捡漏王子 发表于 2012-8-6 16:06 | 显示全部楼层
外部25M晶振,那你这个波特率就可能设置不对
捡漏王子 发表于 2012-8-6 16:07 | 显示全部楼层
把你的始终部分的代码贴出来看一下就清楚了
 楼主| shijieqiji 发表于 2012-8-6 17:58 | 显示全部楼层
[code][/code]void SystemInit (void)
{
  /* Reset the RCC clock configuration to the default reset state(for debug purpose) */
  /* Set HSION bit */
  RCC->CR |= (uint32_t)0x00000001;

  /* Reset SW, HPRE, PPRE1, PPRE2, ADCPRE and MCO bits */
#ifndef STM32F10X_CL
  RCC->CFGR &= (uint32_t)0xF8FF0000;
#else
  RCC->CFGR &= (uint32_t)0xF0FF0000;
#endif /* STM32F10X_CL */   
  
  /* Reset HSEON, CSSON and PLLON bits */
  RCC->CR &= (uint32_t)0xFEF6FFFF;

  /* Reset HSEBYP bit */
  RCC->CR &= (uint32_t)0xFFFBFFFF;

  /* Reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE/OTGFSPRE bits */
  RCC->CFGR &= (uint32_t)0xFF80FFFF;

#ifdef STM32F10X_CL
  /* Reset PLL2ON and PLL3ON bits */
  RCC->CR &= (uint32_t)0xEBFFFFFF;

  /* Disable all interrupts and clear pending bits  */
  RCC->CIR = 0x00FF0000;

  /* Reset CFGR2 register */
  RCC->CFGR2 = 0x00000000;
#elif defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)
  /* Disable all interrupts and clear pending bits  */
  RCC->CIR = 0x009F0000;

  /* Reset CFGR2 register */
  RCC->CFGR2 = 0x00000000;      
#else
  /* Disable all interrupts and clear pending bits  */
  RCC->CIR = 0x009F0000;
#endif /* STM32F10X_CL */
   
// #if defined (STM32F10X_HD) || (defined STM32F10X_XL) || (defined STM32F10X_HD_VL)
//   #ifdef DATA_IN_ExtSRAM
//     SystemInit_ExtMemCtl();
//   #endif /* DATA_IN_ExtSRAM */
// #endif
  /* Configure the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers */
  /* Configure the Flash Latency cycles and enable prefetch buffer */
  SetSysClock();

// #ifdef VECT_TAB_SRAM
//   SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */
// #else
//   SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */
// #endif
}
 楼主| shijieqiji 发表于 2012-8-6 17:59 | 显示全部楼层
5# 捡漏王子
void SystemInit (void)
{
  /* Reset the RCC clock configuration to the default reset state(for debug purpose) */
  /* Set HSION bit */
  RCC->CR |= (uint32_t)0x00000001;

  /* Reset SW, HPRE, PPRE1, PPRE2, ADCPRE and MCO bits */
#ifndef STM32F10X_CL
  RCC->CFGR &= (uint32_t)0xF8FF0000;
#else
  RCC->CFGR &= (uint32_t)0xF0FF0000;
#endif /* STM32F10X_CL */   
  
  /* Reset HSEON, CSSON and PLLON bits */
  RCC->CR &= (uint32_t)0xFEF6FFFF;

  /* Reset HSEBYP bit */
  RCC->CR &= (uint32_t)0xFFFBFFFF;

  /* Reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE/OTGFSPRE bits */
  RCC->CFGR &= (uint32_t)0xFF80FFFF;

#ifdef STM32F10X_CL
  /* Reset PLL2ON and PLL3ON bits */
  RCC->CR &= (uint32_t)0xEBFFFFFF;

  /* Disable all interrupts and clear pending bits  */
  RCC->CIR = 0x00FF0000;

  /* Reset CFGR2 register */
  RCC->CFGR2 = 0x00000000;
#elif defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)
  /* Disable all interrupts and clear pending bits  */
  RCC->CIR = 0x009F0000;

  /* Reset CFGR2 register */
  RCC->CFGR2 = 0x00000000;      
#else
  /* Disable all interrupts and clear pending bits  */
  RCC->CIR = 0x009F0000;
#endif /* STM32F10X_CL */
   
// #if defined (STM32F10X_HD) || (defined STM32F10X_XL) || (defined STM32F10X_HD_VL)
//   #ifdef DATA_IN_ExtSRAM
//     SystemInit_ExtMemCtl();
//   #endif /* DATA_IN_ExtSRAM */
// #endif
  /* Configure the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers */
  /* Configure the Flash Latency cycles and enable prefetch buffer */
  SetSysClock();

// #ifdef VECT_TAB_SRAM
//   SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */
// #else
//   SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */
// #endif
}
捡漏王子 发表于 2012-8-6 20:27 | 显示全部楼层
我一直用的是库,不太清楚你这个系统时钟是多大?
捡漏王子 发表于 2012-8-6 20:28 | 显示全部楼层
你最先那个设置波特率的应该是按照72M的频率来设置的,你可以换算下
qiujiahongde 发表于 2012-8-7 08:31 | 显示全部楼层
仿真看下你的波特率,估计你是用的103的工程直接用了!!!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

17

主题

71

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部