完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
/*
*@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:%drn", SystemCoreClock); //printf("33333333 Interrupt TESTrn"); 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里面啥都没有。 |
|
相关推荐
1个回答
|
|
您好,注意一下程序中每次进入中断之后要在中断函数中清除一下对应的中断标志位。
|
|
|
|
只有小组成员才能发言,加入小组>>
481 浏览 1 评论
CH579M+RT-Thread,RTC从Sleep模式唤醒失败是什么原因?
2877 浏览 2 评论
2366 浏览 1 评论
821浏览 2评论
CH569通过HSPI实现USB3.0和FPGA高速双向通讯
654浏览 1评论
502浏览 1评论
CH32F103C8T6使用当前官网上的CDC例程会出现设备描述符请求失败
367浏览 1评论
645浏览 1评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-29 09:07 , Processed in 1.181560 second(s), Total 99, Slave 82 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号