请选择 进入手机版 | 继续访问电脑版

你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

STM32F030 R8T6 USART2通信数据不对

[复制链接]
justsure91 提问时间:2019-2-25 15:04 /
数据手册上看.STM32F030R8T6 的PA2 PA3端口 是可以使用usart2的.
代码如下


  1. GPIO_InitTypeDef GPIO_InitStruct;
  2.   USART_InitTypeDef USART_InitStruct;        
  3.         NVIC_InitTypeDef NVIC_InitStructure;
  4.              //ʱÖÓÅäÖÃ
  5.         RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
  6.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);  
  7.                  //gpioÅäÖÃ

  8.   GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1);
  9.   GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1);
  10.         
  11.                 /* Configure USART Tx as alternate function push-pull */
  12.   GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2;        
  13.   GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
  14.   GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  15.   GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
  16.         GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
  17.   GPIO_Init(GPIOA, &GPIO_InitStruct);
  18.    
  19.   /* Configure USART Rx as alternate function push-pull */
  20.   GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3;
  21.         GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
  22.   GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  23.   GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
  24.         GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
  25.   GPIO_Init(GPIOA, &GPIO_InitStruct);
  26.         
  27.         
  28.                
  29.                 //´®¿ÚģʽÅäÖÃ
  30.         USART_InitStruct.USART_BaudRate =115200;
  31.         USART_InitStruct.USART_WordLength = USART_WordLength_8b;
  32.         USART_InitStruct.USART_StopBits = USART_StopBits_1;
  33.         USART_InitStruct.USART_Parity = USART_Parity_No ;
  34.         USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  35.         USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  36.         USART_Init(USART2, &USART_InitStruct);
  37.         
  38.         USART_Cmd(USART2, ENABLE);
  39.         USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
  40.                
  41.         NVIC_InitStructure.NVIC_IRQChannel=USART2_IRQn;
  42.         NVIC_InitStructure.NVIC_IRQChannelPriority = 0;   
  43.         NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
  44.         NVIC_Init(&NVIC_InitStructure);
  45. //        NVIC_Config(USART2_IRQn);
复制代码
发送使用的 USART_SendData(USART2, data);  不是printf;

可以发送也能接受进入中断.但是数据都不对.
我给电脑发送0XAA 接受的是F9  发送0x01 接受的是E0

电脑给芯片发送0xdd 接受的是FB?

何解啊...哪里的问题?
微信图片_20190225144952.png
微信图片_20190225145036.png
收藏 评论6 发布时间:2019-2-25 15:04

举报

6个回答
justsure91 回答时间:2019-2-25 15:06:21
何解啊~~~~
justsure91 回答时间:2019-2-25 15:10:12
wenyangzeng 回答时间:2019-2-25 15:17:22
本帖最后由 wenyangzeng 于 2019-2-25 15:19 编辑

应该是系统时钟频率有误引起的,检查晶振和倍频配置,下帖供楼主参考:
https://www.stmcu.org.cn/module/forum/thread-614091-1-1.html

评分

参与人数 1蝴蝶豆 +3 收起 理由
STMCU + 3

查看全部评分

justsure91 回答时间:2019-2-25 15:48:09
wenyangzeng 发表于 2019-2-25 15:17
应该是系统时钟频率有误引起的,检查晶振和倍频配置,下帖供楼主参考:
https://www.stmcu.org.cn/module/fo ...

谢谢大佬,,这么说来可能是 库函数是8MHZ*6=48MHZ 我用的是12MHZ*4=48MHZ 可能只修改了系统时钟而没修改APB2的时钟吧.
  1. #if defined (PLL_SOURCE_HSE)
  2.   /* Enable HSE */   
  3.   RCC->CR |= ((uint32_t)RCC_CR_HSEON);
  4. #elif defined (PLL_SOURCE_HSE_BYPASS)
  5.   /* HSE oscillator bypassed with external clock */   
  6.   RCC->CR |= (uint32_t)(RCC_CR_HSEON | RCC_CR_HSEBYP);
  7. #endif /* PLL_SOURCE_HSE */
  8.    
  9.   /* Wait till HSE is ready and if Time out is reached exit */
  10.   do
  11.   {
  12.     HSEStatus = RCC->CR & RCC_CR_HSERDY;
  13.     StartUpCounter++;  
  14.   } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));

  15.   if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  16.   {
  17.     HSEStatus = (uint32_t)0x01;
  18.   }
  19.   else
  20.   {
  21.     HSEStatus = (uint32_t)0x00;
  22.   }  

  23.   if (HSEStatus == (uint32_t)0x01)
  24.   {
  25.     /* Enable Prefetch Buffer and set Flash Latency */
  26.     FLASH->ACR = FLASH_ACR_PRFTBE | FLASH_ACR_LATENCY;

  27.     /* HCLK = SYSCLK */
  28.     RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
  29.       
  30.     /* PCLK = HCLK */
  31.     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE_DIV1;

  32.     /* PLL configuration = HSE * 6 = 48 MHz   12*4=48*/
  33.     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
  34. //    RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_PREDIV1 | RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLMULL6);// 原来库函数
  35.                 RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_PREDIV1 | RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLMULL4);// 我修改之后
  36.                
  37.             
  38.     /* Enable PLL */
  39.     RCC->CR |= RCC_CR_PLLON;

  40.     /* Wait till PLL is ready */
  41.     while((RCC->CR & RCC_CR_PLLRDY) == 0)
  42.     {
  43.     }

  44.     /* Select PLL as system clock source */
  45.     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  46.     RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;   

  47.     /* Wait till PLL is used as system clock source */
  48.     while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)RCC_CFGR_SWS_PLL)
  49.     {
  50.     }
复制代码


请问大佬 这个APB2的修改是哪个啊?
justsure91 回答时间:2019-2-25 15:57:38
wenyangzeng 发表于 2019-2-25 15:17
应该是系统时钟频率有误引起的,检查晶振和倍频配置,下帖供楼主参考:
https://www.stmcu.org.cn/module/fo ...

谢谢大佬 解决了
#if !defined  (HSE_VALUE)     
#define HSE_VALUE    ((uint32_t)12000000) /*!< Value of the External oscillator in Hz*/
#endif /* HSE_VALUE */  
把这个 从8000000改成12000000就ok了 么么哒~
wenyangzeng 回答时间:2019-2-25 16:25:43
狂吠的小疯狗 发表于 2019-2-25 15:57
谢谢大佬 解决了
#if !defined  (HSE_VALUE)     
#define HSE_VALUE    ((uint32_t)12000000) /*!< Val ...

很好
关于意法半导体
我们是谁
投资者关系
意法半导体可持续发展举措
创新和工艺
招聘信息
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
关注我们
st-img 微信公众号
st-img 手机版