#include "sys.h"
void My_usart(void)
{
GPIO_InitTypeDef GPIO_Initstrue;
NVIC_InitTypeDef NVIC_Initsture;
USART_InitTypeDef USART_Initsture;
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_Pin=GPIO_Pin_3;
GPIO_Initstrue.GPIO_Speed=GPIO_Speed_10MHz;
GPIO_Init(GPIOA,&GPIO_Initstrue);//³õʼ»¯IO¿Ú
USART_Initsture.USART_BaudRate=115200;
USART_Initsture.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_Initsture.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
USART_Initsture.USART_Parity=USART_Parity_No;
USART_Initsture.USART_StopBits=USART_StopBits_1;
USART_Initsture.USART_WordLength=USART_WordLength_8b;
USART_Init(USART2,&USART_Initsture);//³õʼ»¯´®¿Ú
USART_Cmd(USART2,ENABLE);//ʹÄÜ´®¿Ú1
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);//¿ªÆô½ÓÊÕÖжÏ
NVIC_Initsture.NVIC_IRQChannel=USART2_IRQn ;
NVIC_Initsture.NVIC_IRQChannelCmd=ENABLE;
NVIC_Initsture.NVIC_IRQChannelPreemptionPriority=1;
NVIC_Initsture.NVIC_IRQChannelSubPriority=1;//
NVIC_Init(&NVIC_Initsture);//ÖжÏÓÅÏȼ¶
}
void USART2_IRQHandler(void)
{
u8 res;
if(USART_GetITStatus(USART2,USART_IT_RXNE))
{
res = USART_ReceiveData(USART2);
USART_SendData(USART2,res);
}
}
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
My_usart();
while(1);
} |