[N32G03x] 有没有串口中断的历程啊

[复制链接]
949|2
 楼主| allbut 发表于 2022-5-19 21:37 来自手机 | 显示全部楼层 |阅读模式
电脑串口发啥,回复啥的那种
麻花油条 发表于 2022-5-23 10:37 来自手机 | 显示全部楼层
没有,可以帮忙顶一下
Yuannn 发表于 2022-5-25 15:49 | 显示全部楼层
串口中断配置
  1. /**
  2. * [url=home.php?mod=space&uid=247401]@brief[/url]  This function handles USART1 Handler.
  3. */
  4. void USART1_IRQHandler(void)
  5. {       
  6.     if (USART_GetFlagStatus(USART1, USART_FLAG_RXDNE) != RESET)
  7.     {
  8.         /* Read one byte from the receive data register */
  9.         RxBuffer[RxCounter++] = USART_ReceiveData(USART1);
  10.                           USART_ConfigInt(USART1, USART_INT_TXDE, ENABLE);
  11.         RxCounter = 0;
  12.     }
  13.                 if (USART_GetIntStatus(USART1, USART_INT_TXDE) != RESET)
  14.     {
  15.         /* Write one byte to the transmit data register */
  16.         USART_SendData(USART1,  RxBuffer[RxCounter++]);
  17.                                 /* Disable the USART1 Transmit interrupt */
  18.                                 USART_ConfigInt(USART1, USART_INT_TXDE, DISABLE);
  19.         RxCounter = 0;
  20.     }

  21. }
串口配置
  1. void USART1_Configuration()
  2. {
  3.                 USART_InitType USART_InitStructure;

  4.             /* USART1 configuration ------------------------------------------------------*/
  5.     USART_InitStructure.BaudRate            = 115200;
  6.     USART_InitStructure.WordLength          = USART_WL_8B;
  7.     USART_InitStructure.StopBits            = USART_STPB_1;
  8.     USART_InitStructure.Parity              = USART_PE_NO;
  9.     USART_InitStructure.HardwareFlowControl = USART_HFCTRL_NONE;
  10.     USART_InitStructure.Mode                = USART_MODE_RX | USART_MODE_TX;

  11.     /* Configure USART1*/
  12.     USART_Init(USART1, &USART_InitStructure);
  13.   
  14.     /* Enable USART1 Receive interrupts */
  15.     USART_ConfigInt(USART1, USART_INT_RXDNE, ENABLE);
  16.   
  17.     /* Enable the USART1 */
  18.     USART_Enable(USART1, ENABLE);
  19. }
时钟配置
  1. void RCC_Configuration(void)
  2. {
  3.     /* Enable GPIO  and USART1  clock */
  4.     RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA | RCC_APB2_PERIPH_AFIO | RCC_APB2_PERIPH_USART1, ENABLE);
  5. }
GPIO配置
  1. /**
  2. * @brief  Configures the different GPIO ports.
  3. */
  4. void GPIO_Configuration(void)
  5. {
  6.     GPIO_InitType GPIO_InitStructure;
  7.        
  8.                 /* Initialize GPIO_InitStructure */
  9.     GPIO_InitStruct(&GPIO_InitStructure);
  10.        
  11.     /* Configure USARTx Rx  */
  12.     GPIO_InitStructure.Pin            =  GPIO_PIN_10;
  13.           GPIO_InitStructure.GPIO_Mode      =  GPIO_MODE_AF_OD;
  14.                 GPIO_InitStructure.GPIO_Alternate =  GPIO_AF4_USART1;
  15.     GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);   
  16.    
  17.           /* Configure USART1 Tx  */
  18.     GPIO_InitStructure.Pin            =  GPIO_PIN_9;   
  19.     GPIO_InitStructure.GPIO_Mode      =  GPIO_MODE_AF_PP;
  20.     GPIO_InitStructure.GPIO_Alternate =  GPIO_AF4_USART1;
  21.     GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);
  22. }
中断向量表
  1. /**
  2. * @brief  Configures the nested vectored interrupt controller.
  3. */
  4. void NVIC_Configuration(void)
  5. {
  6.     NVIC_InitType NVIC_InitStructure;

  7.     /* Enable the USARTy Interrupt */
  8.     NVIC_InitStructure.NVIC_IRQChannel                   = USART1_IRQn;
  9.     NVIC_InitStructure.NVIC_IRQChannelPriority           = 0;
  10.     NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
  11.     NVIC_Init(&NVIC_InitStructure);

  12. }

main函数
  1. int main(void)
  2. {
  3.        

  4.          /* System Clocks Configuration */
  5.     RCC_Configuration();

  6.     /* NVIC configuration */
  7.     NVIC_Configuration();

  8.     /* Configure the GPIO ports */
  9.     GPIO_Configuration();
  10.    
  11.          /* Configure the USART1 */
  12.                 USART1_Configuration();
  13.        
  14.         while(1)
  15.         {
  16.                 ;
  17.         }
  18. }
测试效果【图】
注:定义全局变量Rxbuffer和RxCounter

串口接收到数据后再发送接收到的数据

串口接收到数据后再发送接收到的数据
您需要登录后才可以回帖 登录 | 注册

本版积分规则

2

主题

6

帖子

0

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