
STM32F407VGT6的485通信程序.docx
4页本文格式为Word版,下载可任意编辑STM32F407VGT6的485通信程序 STM32F407VGT6的485通信程序 【SP3485芯片 extern void USART1_SendByte(u8 Data); extern unsigned char UART1_GetByte(u8 GetData); extern void delay(unsigned int dl); void delay(unsigned int dl) { unsigned int i,y; for(i = 0; i DR; USART1_SendByte(GetData); 发送数据 GPIO_SetBits(GPIOE, GPIO_Pin_8 ); //LED2灯闪烁,表示数据接收告成且发送完成 delay(1000); GPIO_ResetBits(GPIOE, GPIO_Pin_8 ); } } void uart_init(void) { USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; /* Enable GPIO clock */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); /* Enable USART clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); /* Connect USART pins to A9\\10 */ GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1); GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1); // /* Configure USART Tx and Rx as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //输出TX GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //务必为AF,OUT不行 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOA, GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //输入RX GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //务必为AF,与M3不同 GPIO_Init(GPIOA, GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 ; //485使能端配置 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, USART_InitStructure.USART_BaudRate = 115200; USART_InitStructure.USART_WordLength = USART_WordLength_8b; 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(USART1, NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init( /* Enable USART */ USART_Cmd(USART1, ENABLE); USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); USART_ClearFlag(USART1, USART_FLAG_TC); } 说明:已经经本人下载至STM32F4的开发板上告成调试,并且能够正确的收发数据!可供宏大奋斗在前线的机油们参考! — 4 —。





![河南新冠肺炎文件-豫建科[2020]63号+豫建科〔2019〕282号](http://img.jinchutou.com/static_www/Images/s.gif)






