打印
[N32G03x]

有没有串口中断的历程啊

[复制链接]
558|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
allbut|  楼主 | 2022-5-19 21:37 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
电脑串口发啥,回复啥的那种

使用特权

评论回复
沙发
麻花油条| | 2022-5-23 10:37 | 只看该作者
没有,可以帮忙顶一下

使用特权

评论回复
板凳
Yuannn| | 2022-5-25 15:49 | 只看该作者
串口中断配置
/**
* [url=home.php?mod=space&uid=247401]@brief[/url]  This function handles USART1 Handler.
*/
void USART1_IRQHandler(void)
{       
    if (USART_GetFlagStatus(USART1, USART_FLAG_RXDNE) != RESET)
    {
        /* Read one byte from the receive data register */
        RxBuffer[RxCounter++] = USART_ReceiveData(USART1);
                          USART_ConfigInt(USART1, USART_INT_TXDE, ENABLE);
        RxCounter = 0;
    }
                if (USART_GetIntStatus(USART1, USART_INT_TXDE) != RESET)
    {
        /* Write one byte to the transmit data register */
        USART_SendData(USART1,  RxBuffer[RxCounter++]);
                                /* Disable the USART1 Transmit interrupt */
                                USART_ConfigInt(USART1, USART_INT_TXDE, DISABLE);
        RxCounter = 0;
    }

}
串口配置
void USART1_Configuration()
{
                USART_InitType USART_InitStructure;

            /* USART1 configuration ------------------------------------------------------*/
    USART_InitStructure.BaudRate            = 115200;
    USART_InitStructure.WordLength          = USART_WL_8B;
    USART_InitStructure.StopBits            = USART_STPB_1;
    USART_InitStructure.Parity              = USART_PE_NO;
    USART_InitStructure.HardwareFlowControl = USART_HFCTRL_NONE;
    USART_InitStructure.Mode                = USART_MODE_RX | USART_MODE_TX;

    /* Configure USART1*/
    USART_Init(USART1, &USART_InitStructure);
  
    /* Enable USART1 Receive interrupts */
    USART_ConfigInt(USART1, USART_INT_RXDNE, ENABLE);
  
    /* Enable the USART1 */
    USART_Enable(USART1, ENABLE);
}
时钟配置
void RCC_Configuration(void)
{
    /* Enable GPIO  and USART1  clock */
    RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA | RCC_APB2_PERIPH_AFIO | RCC_APB2_PERIPH_USART1, ENABLE);
}
GPIO配置
/**
* @brief  Configures the different GPIO ports.
*/
void GPIO_Configuration(void)
{
    GPIO_InitType GPIO_InitStructure;
       
                /* Initialize GPIO_InitStructure */
    GPIO_InitStruct(&GPIO_InitStructure);
       
    /* Configure USARTx Rx  */
    GPIO_InitStructure.Pin            =  GPIO_PIN_10;
          GPIO_InitStructure.GPIO_Mode      =  GPIO_MODE_AF_OD;
                GPIO_InitStructure.GPIO_Alternate =  GPIO_AF4_USART1;
    GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);   
   
          /* Configure USART1 Tx  */
    GPIO_InitStructure.Pin            =  GPIO_PIN_9;   
    GPIO_InitStructure.GPIO_Mode      =  GPIO_MODE_AF_PP;
    GPIO_InitStructure.GPIO_Alternate =  GPIO_AF4_USART1;
    GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);
}
中断向量表
/**
* @brief  Configures the nested vectored interrupt controller.
*/
void NVIC_Configuration(void)
{
    NVIC_InitType NVIC_InitStructure;

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

}

main函数
int main(void)
{
       

         /* System Clocks Configuration */
    RCC_Configuration();

    /* NVIC configuration */
    NVIC_Configuration();

    /* Configure the GPIO ports */
    GPIO_Configuration();
   
         /* Configure the USART1 */
                USART1_Configuration();
       
        while(1)
        {
                ;
        }
}
测试效果【图】
注:定义全局变量Rxbuffer和RxCounter

usart.PNG (87.29 KB )

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

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

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

2

主题

6

帖子

0

粉丝