当前位置:网站首页>Cubemx completes STM32F103 dual serial port 485 transceiver transmission
Cubemx completes STM32F103 dual serial port 485 transceiver transmission
2022-06-30 07:17:00 【liwuxing】
One 、 Hardware circuit
STM32F103EVT6 Basic circuit ; Select serial port 1、 A serial port 2; A serial port 2 adopt 485 Interface output , The control end is PC9 port ,PC9=1 Time to receive ,0 Time send .

Two 、CubeMX Complete the configuration , Build project
Configure clock 、 Debug mode 、IO、 A serial port 1、 A serial port 2 Parameters 、 Open the serial port interrupt .

3、 ... and 、Keil Add code , Complete the function
/* USER CODE BEGIN PV */
// Reception status , Completion flag is 1; In the receiving state, it is 0
uint8_t uart1Rxstate=0;
// Total number of bytes currently received
uint8_t uart1RxCounter=0;
// Receive buffer
uint8_t uart1RxBuff[128]={0};
//uart2 receive
// Reception status , Completion flag is 1; In the receiving state, it is 0
uint8_t uart2Rxstate=0;
// Total number of bytes currently received
uint8_t uart2RxCounter=0;
// Receive buffer
uint8_t uart2RxBuff[128]={0};
void USER_UART2_IRQHandler(void);
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
int fputc(int ch,FILE *f)
{
HAL_UART_Transmit(&huart1,(uint8_t *)&ch,1,0xffff);
return ch;
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_TIM6_Init();
MX_USART1_UART_Init();
MX_USART2_UART_Init();
/* USER CODE BEGIN 2 */
// Open serial port interrupt
__HAL_UART_ENABLE_IT(&huart1,UART_IT_RXNE);
__HAL_UART_ENABLE_IT(&huart2,UART_IT_RXNE);
// A serial port 2 485 Receive enable
HAL_GPIO_WritePin(RS485_TX_EN_PORT, RS485_TX_EN_PIN, GPIO_PIN_RESET);
printf("Test UART1 out Hello STM32!");
char tr2[]="UART2 transmit sentence for test !";
HAL_Delay(500);
HAL_GPIO_WritePin(RS485_TX_EN_PORT, RS485_TX_EN_PIN, GPIO_PIN_SET);
HAL_UART_Transmit(&huart2,(uint8_t *)&tr2,sizeof(tr2),0xffff);
HAL_GPIO_WritePin(RS485_TX_EN_PORT, RS485_TX_EN_PIN, GPIO_PIN_RESET);
HAL_Delay(500);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
// A serial port 1 Data received , A serial port 2 Output
if(uart1Rxstate==1)
{
HAL_GPIO_WritePin(RS485_TX_EN_PORT, RS485_TX_EN_PIN, GPIO_PIN_SET);
HAL_UART_Transmit(&huart2,(uint8_t *)&uart1RxBuff,uart1RxCounter,0xffff);
HAL_GPIO_WritePin(RS485_TX_EN_PORT, RS485_TX_EN_PIN, GPIO_PIN_RESET);
memset(uart1RxBuff,0,128);
uart1Rxstate=0;
uart1RxCounter=0;
}
// A serial port 2 Data received , A serial port 1 Output
if(uart2Rxstate==1)
{
HAL_UART_Transmit(&huart1,(uint8_t *)&uart2RxBuff,uart2RxCounter,0xffff);
memset(uart2RxBuff,0,128);
uart2Rxstate=0;
uart2RxCounter=0;
}
}
/* USER CODE END 3 */
}

Four 、 Serial port debugging tool debugging test
Use the serial port debugging tool to debug .

边栏推荐
- MAX6675 usage notes
- 【Hot100】11. Container with the most water
- 【已解决】Failed! Error: Unknown error 1130
- [most complete] install MySQL on a Linux server
- oracle数据库报列表中最大表达式为1000错误
- Skillfully use 5 keys to improve office efficiency
- [Hot100]10. Regular Expression Matching
- Go常用命令
- 2022年6月29日--使用C#迈出第一步--使用 C# 中的“if”、“else”和“else if”语句向代码添加决策逻辑
- Win10踩坑-开机0xc0000225
猜你喜欢

oracle数据库报列表中最大表达式为1000错误

MySQL Optimization: from more than ten seconds to 300 milliseconds

Xshell transfer file

我今年毕业,但我不知道我要做什么

The first up Master of station B paid to watch the video still came! Price "Persuading" netizens

1285_ Expand macros defined by AUTOSAR functions and variables with scripts to improve readability

网络安全-抓包和IP包头分析

Egret P2 pit encountered by physical engine (1)

Starting MySQL ERROR! Couldn‘t find MySQL server (/usr/local/mysql/bin/mysqld_safe)

2、 Layout system
随机推荐
[hot100] palindrome substring and longest palindrome substring
【Hot100】15. Sum of three numbers
Linu foundation - zoning planning and use
手机开户股票开户安全吗?开户需要准备什么?
【SemiDrive源码分析】【X9芯片启动流程】34 - RTOS侧 Display模块 sdm_display_init 显示初始化源码分析
Stm32g0 Tim interrupt use
Go项目目录结构介绍
nRF52832 GPIO LED
Golan common shortcut key settings
【已实现】服务器jar包启动脚本、shell脚本
Raspberry pie trivial configuration
视频播放器(一):流程
Out of class implementation of member function of class template
app闪退
网络安全-单臂路由、DHCP中继和ICMP协议
Class template case - encapsulation of array classes
Qstring to const char*
Stm32g0 and FreeRTOS learning summary
Deploying web projects using idea
QT elementary notes