【求助】stm32L151c8t6串口不能进接收中断

2019-08-19 16:23发布

求助各位大神!
代码查了几天了,一直没找到问题在哪,各位帮忙看一看,谢谢啦!
问题描述:
    用stm32L151c8t6作的串口通信,板子与pc连接,PC端是串口调试助手,
    stm发送出的数据,在PC端能够正常接收;
    但是pc端发送的数据,stm接收不到
    仿真器调试,发现是没有进到接收中断程序,但是TXE中断是可以进入的
请各位帮忙看一下。我的代码如下:
void usart1_init(void)
{
    usart1_rcc_config();
    usart1_gpio_config();
    usart1_usart_config();
    NVIC_Configuration();
}

//rcc config

void usart1_rcc_config(void)
{   
    //enable gpio clock
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
    //enable usart1 clock
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
}

//gpio config
void usart1_gpio_config(void)
{
    GPIO_InitTypeDef usart1_gpio_initstructure;
   
    //USART_DeInit(USART1);
    //connect PA.9 to usart1's tx
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
    //connect PA.10 to usart1's rx
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
   
    /* Configure USART Tx as alternate function push-pull */
    usart1_gpio_initstructure.GPIO_Pin = GPIO_Pin_9;
    usart1_gpio_initstructure.GPIO_Mode = GPIO_Mode_AF;
    usart1_gpio_initstructure.GPIO_Speed = GPIO_Speed_10MHz;
    usart1_gpio_initstructure.GPIO_OType = GPIO_OType_PP;
    usart1_gpio_initstructure.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_Init(GPIOA, &usart1_gpio_initstructure);
   
    /* Configure USART Rx as alternate function push-pull */
    usart1_gpio_initstructure.GPIO_Pin =GPIO_Pin_10;
      usart1_gpio_initstructure.GPIO_Mode = GPIO_Mode_IN;
      usart1_gpio_initstructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOA,&usart1_gpio_initstructure);
}


//usart config
void usart1_usart_config(void)
{
      USART_InitTypeDef    usart1_usart_initstructure;
      //USART_ClockInitTypeDef usart1_clock_initstructure;
   
      //usart1_clock_initstructure.USART_Clock = USART_Clock_Disable;
      //usart1_clock_initstructure.USART_CPHA = USART_CPOL_Low;
      //usart1_clock_initstructure.USART_CPOL = USART_CPHA_2Edge;
      //usart1_clock_initstructure.USART_LastBit = USART_LastBit_Disable;
      //USART_ClockInit(USART1,&usart1_clock_initstructure);
   
      //USART_DeInit(USART1);
      usart1_usart_initstructure.USART_BaudRate = 115200;
    usart1_usart_initstructure.USART_WordLength = USART_WordLength_8b;
    usart1_usart_initstructure.USART_StopBits = USART_StopBits_1;
    usart1_usart_initstructure.USART_Parity = USART_Parity_No;
    usart1_usart_initstructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    usart1_usart_initstructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
   
      //configuration
      USART_Init(USART1, &usart1_usart_initstructure);
            
      // uart_it enable
    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
      //USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
      
      //enable
      USART_Cmd(USART1, ENABLE);
}
void NVIC_Configuration(void)
{
    NVIC_InitTypeDef NVIC_InitStructure;
   
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);  

    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;  
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
}


void USART1_IRQHandler(void)
{         
    if(USART_GetFlagStatus(USART1, USART_FLAG_ORE) != RESET)
    {
            USART_ReceiveData(USART1);
    }
   
    if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
    {   
           USART_ReceiveData(USART1);
           USART_ClearITPendingBit(USART1,USART_IT_RXNE);
    }

    if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET)
    {   
           USART_SendData(USART1,'A');
           USART_ClearITPendingBit(USART1,USART_IT_TXE);
    }
}


友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
7条回答
plutobio
1楼-- · 2019-08-19 19:05
自己先顶一下,谢谢啦
plutobio
2楼-- · 2019-08-19 21:39
没有人理我呀
plutobio
3楼-- · 2019-08-19 21:52
下面是我用的时钟初始化,我用的是内部的HSI,数据手册说是16M

void SystemInit (void)
{
  //RCC_DeInit();
    //GPIO_InitTypeDef GPIO_InitStructure;
  /*!< Set MSION bit */
  RCC->CR |= (uint32_t)0x00000100;

  /*!< Reset SW[1:0], HPRE[3:0], PPRE1[2:0], PPRE2[2:0], MCOSEL[2:0] and MCOPRE[2:0] bits */
  RCC->CFGR &= (uint32_t)0x88FFC00C;
  
  /*!< Reset HSION, HSEON, CSSON and PLLON bits */
  RCC->CR &= (uint32_t)0xEEFEFFFE;

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

  /*!< Reset PLLSRC, PLLMUL[3:0] and PLLDIV[1:0] bits */
  RCC->CFGR &= (uint32_t)0xFF02FFFF;

  /*!< Disable all interrupts */
  RCC->CIR = 0x00000000;

  /*!< Configure the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers */
  /*!< Configure the Flash Latency cycles and enable prefetch buffer */
  SetSysClockToHSI();

  //SystemCoreClockUpdate();
   
}
static void SetSysClockToHSI(void)
{
  __IO uint32_t StartUpCounter = 0, HSIStatus = 0;
  
  /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/   
  /* Enable HSI */   
  RCC->CR |= ((uint32_t)RCC_CR_HSION);

  /* Wait till HSI is ready and if Time out is reached exit */
  do
  {
    HSIStatus = RCC->CR & RCC_CR_HSIRDY;
    StartUpCounter++;  
  } while((HSIStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));

  if ((RCC->CR & RCC_CR_HSIRDY) != RESET)
  {
    HSIStatus = (uint32_t)0x01;
  }
  else
  {
    HSIStatus = (uint32_t)0x00;
  }  
   
  if (HSIStatus == (uint32_t)0x01)
  {
    /* Enable 64-bit access */
    FLASH->ACR |= FLASH_ACR_ACC64;
   
    /* Enable Prefetch Buffer */
    FLASH->ACR |= FLASH_ACR_PRFTEN;   

    /* Flash 1 wait state */
    FLASH->ACR |= FLASH_ACR_LATENCY;

    /* Enable the PWR APB1 Clock */
    RCC->APB1ENR |= RCC_APB1ENR_PWREN;
  
    /* Select the Voltage Range 1 (1.8V) */
    PWR->CR = PWR_CR_VOS_0;
  
    /* Wait Until the Voltage Regulator is ready */
    while((PWR->CSR & PWR_CSR_VOSF) != RESET)
    {
    }     
      
    /* HCLK = SYSCLK , HPRE--AHB prescalar*/
    RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
      
    /* PCLK2 = HCLK , PPRE2--APB2 prescalar*/
    RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
   
    /* PCLK1 = HCLK PPRE1--APB1 prescalar*/
    RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1;
   
    /* Select HSI as system clock source */
    RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
    RCC->CFGR |= (uint32_t)RCC_CFGR_SW_HSI;   

    /* Wait till HSI is used as system clock source */
    while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x04)
    {
    }
  }
  else
  {
    /* If HSI fails to start-up, the application will have wrong clock
       configuration. User can add here some code to deal with this error */   
  }
}
plutobio
4楼-- · 2019-08-20 03:13
有没有哪位大神帮我看一下呀,谢谢了,纠结好几天了
plutobio
5楼-- · 2019-08-20 03:27
 精彩回答 2  元偷偷看……
阿侑kevin
6楼-- · 2019-08-20 06:34
GPIOA.10 mode设置成复用,且必须这么设置

一周热门 更多>