void USART2_Initialise( u32 bound )
{
GPIO_InitTypeDef GPIO_InitStructure;//I/O¿ú3õê¼»ˉ½á11ìå
NVIC_InitTypeDef NVIC_InitStructure;//ÖD¶Ï3õê¼»ˉ½á11ìå
USART_InitTypeDef USART_InitStructure;//′®¿ú3õê¼»ˉ½á11ìå
/* Enable the USART2 Pins Software Remapping */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD ,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
/* Configure USART2 Rx (PD.06) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOD, &GPIO_InitStructure);
/* Configure USART2 Tx (PD.05) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
/* Enable the USART2 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_InitStructure.USART_BaudRate = bound;//ò»°ãéèÖÃÎa9600;
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£Ñéλ
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//¾Yá÷¿ØÖÆ
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //êÕ·¢Ä£ê½
USART_Init(USART2, &USART_InitStructure);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
//USART_ITConfig(USART2, USART_IT_TXE, ENABLE);
/* Enable USART2 */
USART_Cmd(USART2, ENABLE);
}
void USART2_IRQHandler(void)
{
if(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == SET)
{
USART_SendData(USART2, USART_ReceiveData(USART2));
}
}