1、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 灯闪烁,表示数据接收成功且发送
2、完成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_APB2PeriphClockC
3、md(RCC_APB2Periph_USART1, ENABLE);/* Connect USART pins to A910 */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; /输出 TXGPIO_Ini
4、tStructure.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; /输入 RXGPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
5、/必须为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_InitStruc
6、ture.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_Mo
7、de = 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 的开发板上成功调试,并且能够正确的收发数据!可供广大奋斗在前线的机油们参考!