狂吠的小疯狗 发表于 2019-2-25 15:04:13

STM32F030 R8T6 USART2通信数据不对

数据手册上看.STM32F030R8T6 的PA2 PA3端口 是可以使用usart2的.
代码如下


GPIO_InitTypeDef GPIO_InitStruct;
USART_InitTypeDef USART_InitStruct;      
      NVIC_InitTypeDef NVIC_InitStructure;
             //ʱÖÓÅäÖÃ
      RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
      RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
               //gpioÅäÖÃ

GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1);
      
                /* Configure USART Tx as alternate function push-pull */
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2;      
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
      GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStruct);
   
/* Configure USART Rx as alternate function push-pull */
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3;
      GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
      GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStruct);
      
      
               
                //´®¿ÚģʽÅäÖÃ
      USART_InitStruct.USART_BaudRate =115200;
      USART_InitStruct.USART_WordLength = USART_WordLength_8b;
      USART_InitStruct.USART_StopBits = USART_StopBits_1;
      USART_InitStruct.USART_Parity = USART_Parity_No ;
      USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
      USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
      USART_Init(USART2, &USART_InitStruct);
      
      USART_Cmd(USART2, ENABLE);
      USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
               
      NVIC_InitStructure.NVIC_IRQChannel=USART2_IRQn;
      NVIC_InitStructure.NVIC_IRQChannelPriority = 0;   
      NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
      NVIC_Init(&NVIC_InitStructure);
//      NVIC_Config(USART2_IRQn);发送使用的 USART_SendData(USART2, data);不是printf;

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

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

何解啊...哪里的问题?

狂吠的小疯狗 发表于 2019-2-25 15:06:21

何解啊~~~~

狂吠的小疯狗 发表于 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

狂吠的小疯狗 发表于 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的时钟吧.
#if defined (PLL_SOURCE_HSE)
/* Enable HSE */   
RCC->CR |= ((uint32_t)RCC_CR_HSEON);
#elif defined (PLL_SOURCE_HSE_BYPASS)
/* HSE oscillator bypassed with external clock */   
RCC->CR |= (uint32_t)(RCC_CR_HSEON | RCC_CR_HSEBYP);
#endif /* PLL_SOURCE_HSE */
   
/* Wait till HSE is ready and if Time out is reached exit */
do
{
    HSEStatus = RCC->CR & RCC_CR_HSERDY;
    StartUpCounter++;
} while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));

if ((RCC->CR & RCC_CR_HSERDY) != RESET)
{
    HSEStatus = (uint32_t)0x01;
}
else
{
    HSEStatus = (uint32_t)0x00;
}

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

    /* HCLK = SYSCLK */
    RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
      
    /* PCLK = HCLK */
    RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE_DIV1;

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

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

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

    /* Wait till PLL is used as system clock source */
    while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)RCC_CFGR_SWS_PLL)
    {
    }

请问大佬 这个APB2的修改是哪个啊?

狂吠的小疯狗 发表于 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 ...

很好
页: [1]
查看完整版本: STM32F030 R8T6 USART2通信数据不对