我根據(jù)stm32的庫(kù)函數(shù)寫(xiě)的代碼
下面是串口一的初始化代碼
??? RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE);
??? GPIO_InitTypeDef? GPIO_InitStructure={0};
??? USART_InitTypeDef USART_InitStructure={0};
??? //NVIC_InitTypeDef NVIC_InitStructure;
??? GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
??? GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
??? GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
??? GPIO_Init(GPIOA, &GPIO_InitStructure);
?? GPIO_PinRemapConfig(GPIO_PartialRemap1_USART1, ENABLE);
??? GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
??? GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
??? GPIO_Init(GPIOA, &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_Rx | USART_Mode_Tx;
//??? NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
//??? NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=4 ;//搶占優(yōu)先級(jí)4
//??? NVIC_InitStructure.NVIC_IRQChannelSubPriority = 4;????? //子優(yōu)先級(jí)4
//??? NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;???????? //IRQ通道使能
//??? NVIC_Init(&NVIC_InitStructure); //根據(jù)指定的參數(shù)初始化VIC寄存器
??? USART_Init(USART1, &USART_InitStructure);
??? //USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//開(kāi)啟串口接受中斷
??? USART_Cmd(USART1, ENABLE);
接下來(lái)是通過(guò)串口一發(fā)消息出來(lái)的代碼,我直接放在main函數(shù)里面的
?? uint16_t Data=0x24;
??? while(1)
??? {
??????? USART_SendData(USART1, Data);
??????? Delay_Ms(500);
??? }
接下來(lái)是串口接收中斷
void USART1_IRQHandler(void)
{
??? uint8_t rec = 0;
??? if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
??? {
??????????? USART_ClearITPendingBit(USART1, USART_IT_RXNE);
??????????? rec = USART_ReceiveData(USART1); //讀取接收到的數(shù)據(jù)
????????????//把數(shù)據(jù)放到隊(duì)列中,隊(duì)列暫時(shí)沒(méi)寫(xiě)
??? }
}
代碼是參考stm32的庫(kù)函數(shù)代碼寫(xiě)的,我實(shí)在不知道哪里有問(wèn)題了,還請(qǐng)賜教,拜托拜托
另外還想請(qǐng)教一下,usb虛擬的串口接收中斷,是不是就是串口2的接收中斷