一、串口的初始化
/*************************************************
uart1
配置串口一的发送和接收的GPIO口功能,以及中断
**************************************************/
void uart1_init(u32 pclk,u32 baud)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1); //Tx
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1); //Rx
GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_1); //RTS/CTS
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 |GPIO_Pin_10|GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//配置串口中断优先级
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_DeInit(USART1);
USART_InitStructure.USART_BaudRate = baud; //波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //数据长度8
USART_InitStructure.USART_StopBits = USART_StopBits_1; //1个停止位
USART_InitStructure.USART_Parity = USART_Parity_No; //无奇偶校验
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_RTS_CTS; //硬件流控制
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
USART_DECmd(USART1,ENABLE); //485硬件控制驱动使能
USART_DEPolarityConfig(USART1,USART_DEPolarity_High); //DE 触发后极性
USART_SetDEAssertionTime(USART1,4);
USART_SetDEDeassertionTime(USART1,4);
USART_OverSampling8Cmd(USART1,ENABLE); //设置过采样率为 8 默认为16
USART_Init(USART1, &USART_InitStructure);
//USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);//使能串口空闲中断
//USART_ClearITPendingBit(USART1, USART_IT_IDLE);//清除串口空闲中断标志位
USART_SetReceiverTimeOut(USART1,2);//空闲2bit后出发中断
USART_ITConfig(USART1,USART_IT_RTO,ENABLE);//使能接收超时中断
USART_ReceiverTimeOutCmd(USART1, ENABLE);
USART_ClearITPendingBit(USART1, USART_IT_RTO);
USART_Cmd(USART1, ENABLE);//使能串口
DMA1_Config();
StaFlag.D_A_Pro = BUF_NO2;//首次进来让 dma使用数组2
StaFlag.Busy_Buf = BUF_NO2;
}
|