各位老師好,請教:CH32V307VCT6 串口中斷只能進入1次問題?
環(huán)境:沁恒官方申請的307開發(fā)板+MRS。為驗證和實驗,用mrs生成工程后,即對串口初始,沒有多余的其他應(yīng)用了。
現(xiàn)象:串口1,中斷方式,板子每次復(fù)位后能夠接收1個字節(jié),之后再收不到了。只能再復(fù)位,后只能收到1個字節(jié)。
?? 例:
?????????我測試應(yīng)用里,串口助手 發(fā),0xA1 ,點亮了PA0 掛載的LED;再發(fā)0xA2 無反應(yīng)。感覺觸發(fā)串口中斷后就掛在哪個地方死循環(huán)出不來一樣。
? 板子復(fù)位啟動時能收到printf發(fā)來的字符,說明串口和硬件是無問題的。
(??????? 后用CH32V203C8T6 也用一樣的方法測試,結(jié)果一樣,肯定是我那個地方用的不對,肯定是我那個地方用的不對,
????困擾2天了,遲遲無法移植到業(yè)務(wù)項目中,
??? 懇請各位老大指導(dǎo)指導(dǎo) ??!? 感謝?。?br>
? )
#include "debug.h"
char RxPacket[64]; //接收緩沖區(qū)
char TxPacket[64]; //發(fā)送緩沖區(qū)
uint8_t Serial_RxFlag; //接收完畢標志位
uint8_t pRxPacket = 0;
//===========================================
// 串口初始化
// 使用COM1 口:PA9、10
//===========================================
void Uart_Init(void)
{
??? RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
??? RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
??? //GPIO端口設(shè)置
??? GPIO_InitTypeDef GPIO_InitStructure;
??????? //普通IO設(shè)置
??????? GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
??????? GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 |GPIO_Pin_1;
??????? GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
??????? GPIO_Init(GPIOA, &GPIO_InitStructure);
??? //USART1_TX?? PA.9
??? GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
??? GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;// USART1_Tx(PA9).
??? GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
??? GPIO_Init(GPIOA, &GPIO_InitStructure);
??? //USART1_RX?? PA.10
??? GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
??? GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
??? GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
??? GPIO_Init(GPIOA, &GPIO_InitStructure);
??? //Usart1 NVIC 配置
??? NVIC_InitTypeDef NVIC_InitStructure;
??? NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
??? NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//IRQ通道使能
??? NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;//搶占優(yōu)先級
??? NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;//子優(yōu)先級
??? NVIC_Init(&NVIC_InitStructure);
??? //USART 初始化設(shè)置
??? USART_InitTypeDef USART_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(USART1, &USART_InitStructure);//初始化串口
??? USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//開啟中斷
??? USART_Cmd(USART1, ENABLE);//使能串口1
}
//===========================================
// 串口1 中斷
//===========================================
void USART1_IRQHandler(void)
{
??? uint8_t RxData;
??????? if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET )
??????? {
??????????? RxData = USART_ReceiveData(USART1); //獲取數(shù)據(jù)
??????????? if(RxData==0xA1) GPIO_ResetBits(GPIOA, GPIO_Pin_0);
??????????? if(RxData==0xA2) GPIO_SetBits(GPIOA, GPIO_Pin_0);
??????? }
}
//==========================================================
// 主函數(shù)
//==========================================================
int main(void)
{
??? NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
?? ?SystemCoreClockUpdate();
?? ?Delay_Init();
?? ?USART_Printf_Init(115200);
?? ?printf("SystemClk:%d\r\n",SystemCoreClock);
?? ?Uart_Init();? //串口初始化
?? ?GPIO_SetBits(GPIOA, GPIO_Pin_0 | GPIO_Pin_1);
?? ?while(1)
??? {
????}
}