我刚做了USART,贴出程序共享一下吧
void Usart_int(void) { USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
USART_DeInit(USART1); /* --------------------------------------------------------------- USART Configuration: FPCLK = 36 MHz, Baud rate =36000,MODE: RX & TX --------------------------------------------------------------- */ USART_InitStructure.USART_BaudRate = 19200; 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; USART_Init(USART1, &USART_InitStructure); /* Enable the USART Receive interrupt: this interrupt is generated when the USART1 receive data register is not empty */ USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
/* Enable USART1 */ USART_Cmd(USART1, ENABLE); USART_ClearFlag(USART1,USART_FLAG_TXE); } void NVIC_init(void) { NVIC_InitTypeDef NVIC_InitStructure;
#ifdef VECT_TAB_RAM /* Set the Vector Table base location at 0x20000000 */ NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); #else /* VECT_TAB_FLASH */ /* Set the Vector Table base location at 0x08000000 */ NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); #endif
// Enable the USART1 gloabal Interrupt NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); }
_IT.C void USART1_IRQHandler(void) { if(USART_GetITStatus(USART1,USART_IT_RXNE)!= RESET) { USART_ClearITPendingBit(USART1, USART_IT_RXNE);
//接收数据在以下地点加代码 if(USART_ReceiveData(USART1)==0xaa) GPIO_CPLBits(GPIOF,GPIO_Pin_6); } }
|
|