1. 串口初始化函数
void USART6_init(u32 bound)
{//GPIO端口设置USART_InitTypeDef USART_InitStructure;NVIC_InitTypeDef NVIC_InitStructure;GPIO_InitTypeDef GPIO_InitStructure; //定义GPIO变量 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC|RCC_AHB1Periph_GPIOB,ENABLE); //GPIO时钟配置 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6,ENABLE); //使能USART1时钟//串口1对应引脚复用映射GPIO_PinAFConfig(GPIOC,GPIO_PinSource6,GPIO_AF_USART6); //GPIOD9复用为USART1GPIO_PinAFConfig(GPIOC,GPIO_PinSource7,GPIO_AF_USART6); //GPIOD8复用为USART1//USART1端口配置GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7; //GPIOA10GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //复用功能GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度50MHzGPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉GPIO_Init(GPIOC,&GPIO_InitStructure); //初始化PA9/PA10//RS485_DIRGPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//普通输出模式GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHzGPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化// GPIO_ResetBits(GPIOB,GPIO_Pin_12); //GPIOF9,F10设置高,灯灭//USART1 初始化设置USART_InitStructure.USART_BaudRate = bound;//波特率设置USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式USART_Init(USART6, &USART_InitStructure); //初始化串口1 USART_ITConfig(USART6, USART_IT_RXNE, ENABLE);//开启相关中断USART_Cmd(USART6, ENABLE); //使能串口1 USART_ClearFlag(USART6, USART_FLAG_TC); //USART1 NVIC 配置NVIC_InitStructure.NVIC_IRQChannel = USART6_IRQn;//串口1中断通道NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;//抢占优先级1NVIC_InitStructure.NVIC_IRQChannelSubPriority =6; //子优先级1NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器RS485_DIR3 = 0;
}
2. 串口发送数据函数
void USART6_send(u8 *Send_array, u16 Length) /*发送程序(发送*p指向的n个数据)*/
{u16 i;RS485_DIR3 = 1;USART_ClearFlag(USART6,USART_FLAG_TC);for(i=0;i<Length;i++){USART_SendData(USART6,Send_array[i]); //向串口3发送数据while(USART_GetFlagStatus(USART6,USART_FLAG_TC)!=SET); //等待发送结束 }while(USART_GetFlagStatus(USART6,USART_FLAG_TC)==RESET); //等待发送结束 RS485_DIR3 = 0;}
3. 串口中断服务函数
void USART6_IRQHandler(void) //串口1中断服务程序
{u8 res; if(USART_GetITStatus(USART6, USART_IT_RXNE) != RESET) //接收到数据{ res = USART_ReceiveData(USART6);RS485_DIR3 = 1; USART_SendData(USART6, res); while(USART_GetFlagStatus(USART6,USART_FLAG_TC)==RESET); //等待发送结束 RS485_DIR3 = 0; }
}
(3条消息) STM32F407——串口通信_stm32f407串口_平平无奇的大学生的博客-CSDN博客
(3条消息) STM32F407 USART6串口使用DMA接收不定长数据和DMA中断发送_stm32f407dma串口_ba_wang_mao的博客-CSDN博客