#include "stm32f4xx.h"\n#include "stm32f4xx_gpio.h"\n#include "stm32f4xx_rcc.h"\n#include "stm32f4xx_usart.h"\n\n#define USART_TX_PIN GPIO_Pin_9\n#define USART_RX_PIN GPIO_Pin_10\n\nvoid USART_Config()\n{\n GPIO_InitTypeDef GPIO_InitStructure;\n USART_InitTypeDef USART_InitStructure;\n \n RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);\n RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);\n \n GPIO_InitStructure.GPIO_Pin = USART_TX_PIN | USART_RX_PIN;\n GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;\n GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;\n GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;\n GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;\n GPIO_Init(GPIOA, &GPIO_InitStructure);\n \n GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);\n GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);\n \n USART_InitStructure.USART_BaudRate = 9600;\n USART_InitStructure.USART_WordLength = USART_WordLength_8b;\n USART_InitStructure.USART_StopBits = USART_StopBits_1;\n USART_InitStructure.USART_Parity = USART_Parity_No;\n USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;\n USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;\n USART_Init(USART1, &USART_InitStructure);\n \n USART_Cmd(USART1, ENABLE);\n}\n\nvoid USART_SendData(uint16_t data)\n{\n while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);\n USART_SendData(USART1, data);\n}\n\nuint16_t USART_ReceiveData()\n{\n while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);\n return USART_ReceiveData(USART1);\n}\n\nvoid USART_SendString(char* str)\n{\n while(*str)\n {\n USART_SendData(*str++);\n }\n}\n\nint main(void)\n{\n USART_Config();\n \n while(1)\n {\n USART_SendString("Hello, World!\r\n");\n delay(1000);\n }\n}

STM32F407 485 通信代码实现 - 基于 HAL 库

原文地址: https://www.cveoy.top/t/topic/py1A 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录