ch32v103串口3 中断接收


/*

 *@Note

 USART中断例程:

 Master:USART2_Tx(PA2)、USART2_Rx(PA3)。

 Slave:USART3_Tx(PB10)、USART3_Rx(PB11)。


 本例程演示 UART2 和 USART3 使用查询发送,中断接收。

 注:

     硬件连线:PA2 —— PB11

               PA3 —— PB10


*/


//#include "debug.h"

#include "ch32v10x.h"

/* Global define */

#define TxSize1    (size(TxBuffer1))

#define TxSize2    (size(TxBuffer2))

#define size(a)    (sizeof(a) / sizeof(*(a)))


/* Global typedef */

typedef enum

{

    FAILED = 0,

    PASSED = !FAILED

} TestStatus;


/* Global Variable */

u8 TxBuffer1[] = "*Buffer1 Send from USART2 to USART3 using Interrupt!"; /* Send by UART2 */

u8 TxBuffer2[] = "#Buffer2 Send from USART3 to USART2 using Interrupt!"; /* Send by UART3 */

u8 RxBuffer1[TxSize1] = {0};                                             /* USART2 Using */

u8 RxBuffer2[10] = {0};                                             /* USART3 Using  */


u8 TxCnt1 = 0, RxCnt1 = 0;

u8 TxCnt2 = 0, RxCnt2 = 0;


u8 Rxfinish2 = 0;


TestStatus TransferStatus1 = FAILED;

TestStatus TransferStatus2 = FAILED;



void USART3_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));


/*********************************************************************

 * @fn      Buffercmp

 *

 * @brief   Compares two buffers

 *

 * @param   Buf1,Buf2 - buffers to be compared

 *          BufferLength - buffer's length

 *

 * @return  PASSED - Buf1 identical to Buf

 *          FAILED - Buf1 differs from Buf2

 */

TestStatus Buffercmp(uint8_t *Buf1, uint8_t *Buf2, uint16_t BufLength)

{

    while(BufLength--)

    {

        if(*Buf1 != *Buf2)

        {

            return FAILED;

        }

        Buf1++;

        Buf2++;

    }

    return PASSED;

}


/*********************************************************************

 * @fn      USARTx_CFG

 *

 * @brief   Initializes the USART2 & USART3 peripheral.

 *

 * @return  none

 */

void USARTx_CFG(void)

{

    GPIO_InitTypeDef  GPIO_InitStructure = {0};

    USART_InitTypeDef USART_InitStructure = {0};

    NVIC_InitTypeDef  NVIC_InitStructure = {0};


    RCC_APB2PeriphClockCmd(  RCC_APB2Periph_GPIOB, ENABLE);

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);



    /* USART3 TX-->B.10  RX-->B.11 */

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

    GPIO_Init(GPIOB, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

    GPIO_Init(GPIOB, &GPIO_InitStructure);


    USART_InitStructure.USART_BaudRate = 115200;

    USART_InitStructure.USART_WordLength = USART_WordLength_8b;

    USART_InitStructure.USART_StopBits = USART_StopBits_1;

    USART_InitStructure.USART_Parity = USART_Parity_No;

    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

    USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;


    USART_Init(USART3, &USART_InitStructure);

    USART3->STATR = 0x00C0;

    USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);


    NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;

    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;

    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;

    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

    NVIC_Init(&NVIC_InitStructure);


    USART_Cmd(USART3, ENABLE);

}

void usart_send(u8 *ch)

{

 while(*ch)

    {

        USART_SendData(USART3,*ch++) ;

         while (USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET);

    }

}


/*********************************************************************

 * @fn      main

 *

 * @brief   Main program.

 *

 * @return  none

 */

int main(void)

{

    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);

    Delay_Init();

   // USART_Printf_Init(115200);

   //printf("SystemClk:%d\r\n", SystemCoreClock);


    //printf("33333333 Interrupt TEST\r\n");

    USARTx_CFG(); /* USART2 & USART3 INIT */

    Delay_Ms(100);


    while(1){

         usart_send(RxBuffer2);

        Delay_Ms(1000);

        if(Rxfinish2==1)

    {

            Rxfinish2=0;

            usart_send("0");


           // USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);


    }

}

}

/*********************************************************************

 * @fn      USART3_IRQHandler

 *

 * @brief   This function handles USART3 global interrupt request.

 *

 * @return  none

 */

void USART3_IRQHandler(void)

{

    if(USART_GetITStatus(USART3, USART_IT_RXNE) !=RESET)

    {

        RxBuffer2[RxCnt2++] = USART_ReceiveData(USART3);

        if(RxCnt2 >10)

       {Rxfinish2 = 1;

           // USART_ClearITPendingBit(USART3, USART_IT_RXNE);

         //  USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);

       RxCnt2=0;

       }

    }

}


各位大哥看看这到底那个标志位不对,发送没问题,一直接收不到,完全调不通,Rxfinish2一直等于1,也就是说就算没接收也是一直进接收中断,感觉向外发送字节也引发了USART_IT_RXEN中断。串口助手发给103的根本收不到,RxBuffer2里面啥都没有。

您好,注意一下程序中每次进入中断之后要在中断函数中清除一下对应的中断标志位。后续若有问题,可将工程发我邮箱看一下(lzs@wch.cn)


开发板附赠的调试器    和ch340 USB—TTL,这两者问题,硬件有毛病,最好在手册里说明,ch32v103 rx脚 选浮空输入有毛病,最好用GPIO_Mode_IPU, 不用自带的调试器上的串口 选浮空 就会有问题。


只有登录才能回复,可以选择微信账号登录