void uart3_init(u32 bound)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);//使能USART1时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE);
GPIO_PinAFConfig(GPIOC,GPIO_PinSource10,GPIO_AF_USART3);
GPIO_PinAFConfig(GPIOC,GPIO_PinSource11,GPIO_AF_USART3);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11; //GPIOC10与GPIOC11
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度50MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_Init(GPIOC,&GPIO_InitStructure); //初始化PA9,PA10
//USART Initialization Settings 初始化设置
USART_InitStructure.USART_BaudRate = bound; //Port rate //串口波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //The word length is 8 bit data format //字长为8位数据格式
USART_InitStructure.USART_StopBits = USART_StopBits_1; //A stop bit //一个停止
USART_InitStructure.USART_Parity = USART_Parity_No; //Prosaic parity bits //无奇偶校验位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //No hardware data flow control //无硬件数据流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //Sending and receiving mode //收发模式
USART_Init(USART3, &USART_InitStructure); //Initialize serial port 3 //初始化串口3
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); //Open the serial port to accept interrupts //开启串口接受中断
USART_Cmd(USART3, ENABLE); //Enable serial port 3 //使能串口3
//UsartNVIC configuration //UsartNVIC配置
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
//Preempt priority //抢占优先级
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2 ;
//Preempt priority //抢占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
//Enable the IRQ channel //IRQ通道使能
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
//Initialize the VIC register with the specified parameters
//根据指定的参数初始化VIC寄存器
NVIC_Init(&NVIC_InitStructure);
}
int USART3_IRQHandler(void)
{
u8 Usart_Receive;
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) //Check if data is received //判断是否接收到数据
{
Usart_Receive = USART_ReceiveData(USART3);//Read the data //读取数据
}
return 0;
}
void usart3_send(u8 data)
{
while((USART3->SR&0x40)==0);
USART3->DR = data;
}