仿照串口1例程,编写串口2启动代码如何,不知什么地方有问题,始终无法进入中断服务程序?望大侠指点!
void uart2_init(u32 bound)
{
//GPIO¶Ë¿úéèÖÃ
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //ê1ÄüGPIOAê±Öó
RCC_APB2PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);//ê1ÄüUSART2ê±Öó
//′®¿ú2¶Ôó|òy½Å¸′óÃó3éä
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2); //GPIOA2¸′óÃÎaUSART2-TX
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2); //GPIOA3¸′óÃÎaUSART2-RX
//USART2¶Ë¿úÅäÖÃ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; //GPIOA2óëGPIOA3
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //¸′óÃ1|Äü
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //Ëù¶è50MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //íÆíì¸′óÃêä3ö
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //éÏà-
GPIO_Init(GPIOA, &GPIO_InitStructure); //3õê¼»ˉPA2£¬
A3
//USART2 3õê¼»ˉéèÖÃ
USART_InitStructure.USART_BaudRate = bound; //2¨ìØÂêéèÖÃ
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //×Ö3¤Îa8λêy¾Y¸ñê½
USART_InitStructure.USART_StopBits = USART_StopBits_1; //ò»¸öí£Ö1λ
USART_InitStructure.USART_Parity = USART_Parity_No; //ÎTÆæżD£Ñéλ
//#define USART_HardwareFlowControl_None ((uint16_t)0x0000)
//#define USART_HardwareFlowControl_RTS ((uint16_t)0x0100)
//#define USART_HardwareFlowControl_CTS ((uint16_t)0x0200)
//#define USART_HardwareFlowControl_RTS_CTS ((uint16_t)0x0300)
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//ÎTó2¼têy¾Yá÷¿ØÖÆ
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //êÕ·¢Ä£ê½
USART_Init(USART2, &USART_InitStructure); //3õê¼»ˉ′®¿ú2
USART_Cmd(USART2, ENABLE); //ê1Äü′®¿ú2
//USART_ClearFlag(USART2, USART_FLAG_TC);
#if EN_USART2_RX
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //¿aÆôÏà1ØÖD¶Ï
//Usart2 NVIC ÅäÖÃ
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; //′®¿ú2ÖD¶Ïí¨μà
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3; //ÇàÕ¼óÅÏ輶3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =3; //×óóÅÏ輶3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μàê1Äü
NVIC_Init(&NVIC_InitStructure); //¸ù¾YÖ¸¶¨μÄ2Îêy3õê¼»ˉVIC¼Ä′æÆ÷¡¢
#endif
}
一周热门 更多>