ch32v103串口3 中斷接收


/*

?*@Note

?USART中斷例程:

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

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


?本例程演示 UART2 和 USART3 使用查詢(xún)發(fā)送,中斷接收。

?注:

? ? ?硬件連線: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;

? ? ? ?}

? ? }

}


各位大哥看看這到底那個(gè)標(biāo)志位不對(duì),發(fā)送沒(méi)問(wèn)題,一直接收不到,完全調(diào)不通,Rxfinish2一直等于1,也就是說(shuō)就算沒(méi)接收也是一直進(jìn)接收中斷,感覺(jué)向外發(fā)送字節(jié)也引發(fā)了USART_IT_RXEN中斷。串口助手發(fā)給103的根本收不到,RxBuffer2里面啥都沒(méi)有。

您好,注意一下程序中每次進(jìn)入中斷之后要在中斷函數(shù)中清除一下對(duì)應(yīng)的中斷標(biāo)志位。后續(xù)若有問(wèn)題,可將工程發(fā)我郵箱看一下(lzs@wch.cn)


開(kāi)發(fā)板附贈(zèng)的調(diào)試器? ? 和ch340 USB—TTL,這兩者問(wèn)題,硬件有毛病,最好在手冊(cè)里說(shuō)明,ch32v103 rx腳 選浮空輸入有毛病,最好用GPIO_Mode_IPU, 不用自帶的調(diào)試器上的串口 選浮空 就會(huì)有問(wèn)題。


只有登錄才能回復(fù),可以選擇微信賬號(hào)登錄

国产91精品新入口,国产成人综合网在线播放,九热这里只有精品,本道在线观看,美女视频a美女视频,韩国美女激情视频,日本美女pvp视频