|
void uart_init(u32 bound){
GPIO_InitTypeDef GPIO_InitStrue;
USART_InitTypeDef USART_InitStrue;
NVIC_InitTypeDef NVIC_InitStrue;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA ,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
GPIO_InitStrue.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStrue.GPIO_Pin=GPIO_Pin_2;
GPIO_InitStrue.GPIO_Speed=GPIO_Speed_10MHz;
GPIO_Init(GPIOA,&GPIO_InitStrue);//?
GPIO_InitStrue.GPIO_Mode=GPIO_Mode_IN_FLOATING;
// GPIO_InitStrue.GPIO_Mode=GPIO_Mode_IPU;
GPIO_InitStrue.GPIO_Pin=GPIO_Pin_3;
GPIO_InitStrue.GPIO_Speed=GPIO_Speed_10MHz;
GPIO_Init(GPIOA,&GPIO_InitStrue);//?
USART_InitStrue.USART_BaudRate=bound;
USART_InitStrue.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_InitStrue.USART_Mode=USART_Mode_Tx|USART_Mode_Rx;
USART_InitStrue.USART_Parity=USART_Parity_No;
USART_InitStrue.USART_StopBits=USART_StopBits_1;
USART_InitStrue.USART_WordLength=USART_WordLength_8b;
USART_Init(USART2,&USART_InitStrue);//USART2 init
USART_Cmd(USART2,ENABLE);//USART2 enable
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
NVIC_InitStrue.NVIC_IRQChannel=USART2_IRQn;
NVIC_InitStrue.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStrue.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStrue.NVIC_IRQChannelSubPriority=1;
NVIC_Init(&NVIC_InitStrue);
}
void USART2_IRQHandler(void) //′®¿ú2ÖD¶Ï·tÎñ3ìDò
{
u8 Res;
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //
{
Res =USART_ReceiveData(USART2); //
USART_SendData(USART2,Res);
}
}
int main(void)
{
u16 t; unsigned char i = 0;
u16 len;
u16 times=0;
delay_init(); //
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //
uart_init(9600); //
LED_Init();
while(1)
{
delay_ms(1000);
USART_SendData(USART2,0x36);
}
}
|
|