文章目录
- 1. 背景
- 2. 改写方式
- 2.1 cursor
- 2.2 使用方式
- 3. 移植过程
- 修改概述
- 修改的文件和内容
- 1. PRO2/Core/Inc/usart.h
- 2. PRO2/Core/Src/usart.c
- 3. PRO2/Drivers/BSP/STM32MB/port/portserial.c
- 4. PRO2/Core/Src/stm32f1xx_it.c
- 5. PRO2/Core/Src/main.c
- 6. PRO2/Core/Src/gpio.c
- 引脚对应关系
- 修改前(串口2)
- 修改后(串口1)
- 注意事项
- 编译和测试
- 附:会话过程
- 修改stm32f103串口配置
- 总结
- 串口配置修改:
- 修改的文件:
- 关键修改点:
1. 背景
上一节是在stm32f103re上完成对野火的modbus程序修改,这一次要在gd32f103re上实现移植,由于这次用到的功能两个芯片没有什么区别,试了一下,居然可以直接在gd32上面用stm32的HAL库,甚至芯片选型也用的stm32
2. 改写方式
由于移植过程机械并且繁琐,我使用cursor,来帮助改写。
2.1 cursor
Cursor 是一款专为开发者设计的智能代码编辑器,它结合了传统代码编辑器的功能与人工智能辅助编程能力。Cursor 基于 VS Code 的内核构建,继承了 VS Code 的优秀特性,同时加入了 AI 编程助手功能,可以帮助开发者提高编码效率。
2.2 使用方式
将要改的工程放在工作区,在右侧写入指令就可以了,非常好用方便。
3. 移植过程
修改概述
将STM32F103工程从使用串口2(PA2/PA3)改为串口1(PA9/PA10),485控制引脚从PA1改为PC9。
修改的文件和内容
1. PRO2/Core/Inc/usart.h
- 将
DEBUG_USART
从USART2
改为USART1
- 将时钟使能宏从
__USART2_CLK_ENABLE()
改为__USART1_CLK_ENABLE()
- 将引脚定义从
GPIO_PIN_2/GPIO_PIN_3
改为GPIO_PIN_9/GPIO_PIN_10
- 将中断处理宏从
USART2_IRQHandler/USART2_IRQn
改为USART1_IRQHandler/USART1_IRQn
- 将Modbus串口号从
MB_MASTER_USARTx = 2
改为MB_MASTER_USARTx = 1
- 将485控制引脚从
GPIOA/GPIO_PIN_1
改为GPIOC/GPIO_PIN_9
- 将外部变量从
huart2
改为huart1
- 将函数声明从
MX_USART2_UART_Init
改为MX_USART1_UART_Init
2. PRO2/Core/Src/usart.c
- 将UART句柄从
huart2
改为huart1
- 将初始化函数从
MX_USART2_UART_Init
改为MX_USART1_UART_Init
- 将端口检查从
ucPORT != 2
改为ucPORT != 1
- 将MSP初始化中的串口实例检查从
USART2
改为USART1
- 将时钟使能从
__HAL_RCC_USART2_CLK_ENABLE()
改为__HAL_RCC_USART1_CLK_ENABLE()
- 将引脚配置注释从
PA2/PA3
改为PA9/PA10
- 将中断处理从
USART2_IRQn
改为USART1_IRQn
- 将发送函数中的句柄从
huart2
改为huart1
3. PRO2/Drivers/BSP/STM32MB/port/portserial.c
- 将初始化函数调用从
MX_USART2_UART_Init
改为MX_USART1_UART_Init
- 将所有串口句柄引用从
huart2
改为huart1
- 将注释从"串口2"改为"串口1"
4. PRO2/Core/Src/stm32f1xx_it.c
- 将外部变量声明从
huart2
改为huart1
- 将中断处理函数从
USART2_IRQHandler
改为USART1_IRQHandler
- 将中断处理中的句柄从
huart2
改为huart1
- 将中断清除从
USART2_IRQn
改为USART1_IRQn
5. PRO2/Core/Src/main.c
- 将注释从"串口2初始化"改为"串口1初始化"
6. PRO2/Core/Src/gpio.c
- 添加
__HAL_RCC_GPIOC_CLK_ENABLE()
启用GPIOC时钟 - 将GPIO配置从
GPIOA/GPIO_PIN_1
改为GPIOC/GPIO_PIN_9
- 将引脚初始化从PA1改为PC9
引脚对应关系
修改前(串口2)
- 串口TX: PA2
- 串口RX: PA3
- 485控制: PA1
修改后(串口1)
- 串口TX: PA9
- 串口RX: PA10
- 485控制: PC9
注意事项
- 确保硬件连接正确,将485模块连接到新的引脚
- 串口1的波特率和其他参数保持不变(115200, 8-N-1)
- 485控制逻辑保持不变,低电平接收,高电平发送
- 所有中断优先级和Modbus配置保持不变
编译和测试
修改完成后,重新编译工程,没有编译错误。串口通信和485功能正常工作。
附:会话过程
修改stm32f103串口配置
Exported on 2025/8/1 at GMT+8 16:56:25 from Cursor (1.3.7)
User
这是stm32f103的工程,原来是使用了串口2,连的是PA2和PA3,485控制引脚用的PA1。现在请你把工程改成使用串口1,连PA9和PA10,485控制引脚用PC9,改这些就行了
Cursor
我来帮您将STM32F103工程从串口2改为串口1,并修改相应的引脚配置。首先让我查看当前的配置情况。
现在让我查看相关的源文件,了解当前的实现:
现在让我查看main.c文件,了解如何调用这些函数:
现在让我查看portserial.c文件,了解串口在Modbus中的使用:
现在让我查看portevent.c文件,了解中断处理:
现在让我查看stm32f1xx_hal_msp.c文件,了解MSP初始化:
现在我已经了解了整个工程的结构。我需要将串口2改为串口1,并修改相应的引脚配置。让我开始修改:
- 首先修改usart.h文件,将串口2改为串口1,引脚从PA2/PA3改为PA9/PA10,485控制引脚从PA1改为PC9:
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __usart_H
#define __usart_H
#ifdef __cplusplusextern "C" {
#endif/* Includes ------------------------------------------------------------------*/
#include "main.h"//引脚定义
/***********************usart*****************************/
#define DEBUG_USART USART1
#define DEBUG_USART_CLK_ENABLE() __USART1_CLK_ENABLE();#define RCC_PERIPHCLK_UARTx RCC_PERIPHCLK_USART1
#define RCC_UARTxCLKSOURCE_SYSCLK RCC_USART1CLKSOURCE_SYSCLK
#define __HAL_RCC_USARTx_CLK_DISABLE __HAL_RCC_USART1_CLK_DISABLE();#define DEBUG_USART_RX_GPIO_PORT GPIOA
#define DEBUG_USART_RX_GPIO_CLK_ENABLE() __GPIOA_CLK_ENABLE()
#define DEBUG_USART_RX_PIN GPIO_PIN_10
//#define DEBUG_USART_RX_AF GPIO_AF7_USART1#define DEBUG_USART_TX_GPIO_PORT GPIOA
#define DEBUG_USART_TX_GPIO_CLK_ENABLE() __GPIOA_CLK_ENABLE()
#define DEBUG_USART_TX_PIN GPIO_PIN_9
//#define DEBUG_USART_TX_AF GPIO_AF7_USART1#define DEBUG_USART_IRQHandler USART1_IRQHandler
#define DEBUG_USART_IRQ USART1_IRQn#define MB_MASTER_USARTx 1 //使用串口1
#define MB_MASTER_USART_BAUDRATE 115200 //波特率
#define MB_MASTER_USART_PARITY UART_PARITY_NONE/*********************usart end***************************************/
/***********************485*****************************//* 如果需要使用串口转485 请打开此宏 */
#define MODBUS_MASTER_USE_CONTROL_PIN #define MODBUS_MASTER_GPIO_PORT GPIOC
#define MODBUS_MASTER_GPIO_PIN GPIO_PIN_9
#define MODBUS_MASTER_GPIO_PIN_HIGH GPIO_PIN_RESET
#define MODBUS_MASTER_GPIO_PIN_LOW GPIO_PIN_SET
#define MODBUS_MASTER_GPIO_CLK_ENABLE() __GPIOC_CLK_ENABLE()/***********************485 end*************************************/extern UART_HandleTypeDef huart1;/* USER CODE BEGIN Private defines *//* USER CODE END Private defines */extern void MX_USART1_UART_Init(uint8_t ucPORT, uint32_t ulBaudRate, uint8_t eParity);
extern void usart_send_string( uint8_t *s);
void modbus_master_control_init(void);
/* USER CODE BEGIN Prototypes *//* USER CODE END Prototypes */#ifdef __cplusplus
}
#endif
#endif /*__ usart_H *//*** @}*//*** @}*//************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
- 现在修改usart.c文件,将串口2改为串口1:
/********************************************************************************* @file bsp_basic_tim.c* @author STMicroelectronics* @version V1.0* @date 2020-xx-xx* @brief 串口范例******************************************************************************* @attention** 实验平台:野火 STM32 指南者 F103 开发板 * 论坛 :http://www.firebbs.cn* 淘宝 :http://fire-stm32.taobao.com********************************************************************************//* Includes ------------------------------------------------------------------*/
#include "usart.h"UART_HandleTypeDef huart1;/*** @brief DEBUG_USART GPIO 配置,工作模式配置。115200 8-N-1* @param 无* @retval 无*/
void MX_USART1_UART_Init(uint8_t ucPORT, uint32_t ulBaudRate, uint8_t eParity)
{if(ucPORT != 1) //必须设置为串口1return ;huart1.Instance = DEBUG_USART;huart1.Init.BaudRate = ulBaudRate;huart1.Init.WordLength = UART_WORDLENGTH_8B;huart1.Init.StopBits = UART_STOPBITS_1;huart1.Init.Parity = eParity;huart1.Init.Mode = UART_MODE_TX_RX;huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;huart1.Init.OverSampling = UART_OVERSAMPLING_16;if (HAL_UART_Init(&huart1) != HAL_OK){while(1);}}
/*** @brief UART MSP 初始化 * @param huart: UART handle* @retval 无*/
void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
{GPIO_InitTypeDef GPIO_InitStruct = {0};if(uartHandle->Instance==USART1){/* USER CODE BEGIN USART1_MspInit 0 *//* USER CODE END USART1_MspInit 0 *//* USART1 clock enable */__HAL_RCC_USART1_CLK_ENABLE();__HAL_RCC_GPIOA_CLK_ENABLE();/**USART1 GPIO ConfigurationPA9 ------> USART1_TXPA10 ------> USART1_RX*//* 配置Tx引脚为复用功能 */GPIO_InitStruct.Pin = DEBUG_USART_TX_PIN;GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;GPIO_InitStruct.Pull = GPIO_NOPULL;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;HAL_GPIO_Init(DEBUG_USART_TX_GPIO_PORT, &GPIO_InitStruct);GPIO_InitStruct.Pin = DEBUG_USART_RX_PIN;GPIO_InitStruct.Mode=GPIO_MODE_AF_INPUT; HAL_GPIO_Init(DEBUG_USART_RX_GPIO_PORT, &GPIO_InitStruct); /* USART1 interrupt Init */HAL_NVIC_SetPriority(DEBUG_USART_IRQ, 0, 0);//抢占优先级0,子优先级0HAL_NVIC_EnableIRQ(DEBUG_USART_IRQ); //使能USART1中断通道 }
}void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
{if(uartHandle->Instance==USART1){/* Peripheral clock disable */__HAL_RCC_USARTx_CLK_DISABLE;/**USART1 GPIO ConfigurationPA9 ------> USART1_TXPA10 ------> USART1_RX*/HAL_GPIO_DeInit(DEBUG_USART_TX_GPIO_PORT, DEBUG_USART_TX_PIN|DEBUG_USART_RX_PIN);/* USART1 interrupt Deinit */HAL_NVIC_DisableIRQ(DEBUG_USART_IRQ);}
}//串口1发送一个字符串
void usart_send_string( uint8_t *s)
{while(*s != NULL){HAL_UART_Transmit(&huart1,s,1,0xffff);s++;}
}//485收发状态切换管脚
void modbus_master_control_init(void)
{GPIO_InitTypeDef GPIO_InitStruct = {0};GPIO_InitStruct.Pin = MODBUS_MASTER_GPIO_PIN;GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;GPIO_InitStruct.Pull = GPIO_NOPULL;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;HAL_GPIO_Init(MODBUS_MASTER_GPIO_PORT, &GPIO_InitStruct);
}/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
- 现在修改portserial.c文件,将串口2改为串口1:
/** FreeModbus Libary: BARE Port* Copyright (C) 2006 Christian Walter <wolti@sil.at>** This library is free software; you can redistribute it and/or* modify it under the terms of the GNU Lesser General Public* License as published by the Free Software Foundation; either* version 2.1 of the License, or (at your option) any later version.** This library is distributed in the hope that it will be useful,* but WITHOUT ANY WARRANTY; without even the implied warranty of* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU* Lesser General Public License for more details.** You should have received a copy of the GNU Lesser General Public* License along with this library; if not, write to the Free Software* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA** File: $Id$*/#include "port.h"
#include "stm32f1xx_hal.h"
#include "usart.h"
/* ----------------------- Modbus includes ----------------------------------*/
#include "mb.h"
#include "mbport.h"/* ----------------------- static functions ---------------------------------*/
//static void prvvUARTTxReadyISR( void );
//static void prvvUARTRxISR( void );/* ----------------------- Start implementation -----------------------------*/BOOL
xMBPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity )
{/*** set 485 mode receive and transmit control IO* @note MODBUS_MASTER_RT_CONTROL_PIN_INDEX need be defined by user*//* 使用485时需要在usart.h中打开RT_MODBUS_MASTER_USE_CONTROL_PIN宏定义 */#if defined(MODBUS_MASTER_USE_CONTROL_PIN) modbus_master_control_init();#endifMX_USART1_UART_Init(ucPORT,ulBaudRate,eParity);return TRUE;
}void
vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable )
{/* If xRXEnable enable serial receive interrupts. If xTxENable enable* transmitter empty interrupts.*/if (xRxEnable) {/* 串口1接收中断使能 */__HAL_UART_ENABLE_IT(&huart1,UART_IT_RXNE); #if defined(MODBUS_MASTER_USE_CONTROL_PIN) /* 485低电平接收 */HAL_GPIO_WritePin(MODBUS_MASTER_GPIO_PORT,MODBUS_MASTER_GPIO_PIN,MODBUS_MASTER_GPIO_PIN_LOW);#endif}else{/* 串口1接收中断关闭 */__HAL_UART_DISABLE_IT(&huart1,UART_IT_RXNE);#if defined(MODBUS_MASTER_USE_CONTROL_PIN)/* 485高电平发送 */HAL_GPIO_WritePin(MODBUS_MASTER_GPIO_PORT,MODBUS_MASTER_GPIO_PIN,MODBUS_MASTER_GPIO_PIN_HIGH);#endif}if (xTxEnable){/* 串口1发送中断使能 */__HAL_UART_ENABLE_IT(&huart1,UART_IT_TXE);#if defined(MODBUS_MASTER_USE_CONTROL_PIN)/* 485高电平发送*/HAL_GPIO_WritePin(MODBUS_MASTER_GPIO_PORT,MODBUS_MASTER_GPIO_PIN,MODBUS_MASTER_GPIO_PIN_HIGH);#endif}else{/* 串口1发送中断关闭 */__HAL_UART_DISABLE_IT(&huart1,UART_IT_TXE);#if defined(MODBUS_MASTER_USE_CONTROL_PIN) /* 485低电平接收*/HAL_GPIO_WritePin(MODBUS_MASTER_GPIO_PORT,MODBUS_MASTER_GPIO_PIN,MODBUS_MASTER_GPIO_PIN_LOW);#endif}
}BOOL
xMBPortSerialPutByte( CHAR ucByte )
{/* Put a byte in the UARTs transmit buffer. This function is called* by the protocol stack if pxMBFrameCBTransmitterEmpty( ) has been* called. */if(HAL_UART_Transmit (&huart1 ,(uint8_t *)&ucByte,1,0x01) != HAL_OK ) //添加发送一位代码return FALSE ;elsereturn TRUE;
}BOOL
xMBPortSerialGetByte( CHAR * pucByte )
{/* Return the byte in the UARTs receive buffer. This function is called* by the protocol stack after pxMBFrameCBByteReceived( ) has been called.*/if(HAL_UART_Receive (&huart1 ,(uint8_t *)pucByte,1,0x01) != HAL_OK )//添加接收一位代码return FALSE ;elsereturn TRUE;
}/* Create an interrupt handler for the transmit buffer empty interrupt* (or an equivalent) for your target processor. This function should then* call pxMBFrameCBTransmitterEmpty( ) which tells the protocol stack that* a new character can be sent. The protocol stack will then call * xMBPortSerialPutByte( ) to send the character.*/
//static
void prvvUARTTxReadyISR( void ) //删去前面的static,方便在串口中断使用
{pxMBFrameCBTransmitterEmpty( );
}/* Create an interrupt handler for the receive interrupt for your target* processor. This function should then call pxMBFrameCBByteReceived( ). The* protocol stack will then call xMBPortSerialGetByte( ) to retrieve the* character.*/
//static
void prvvUARTRxISR( void ) //删去前面的static,方便在串口中断使用
{pxMBFrameCBByteReceived( );
}
- 现在需要添加USART1的中断处理函数。让我查看是否有中断处理文件:
让我查看中断处理文件:
现在我需要修改中断处理文件,将USART2改为USART1:
/** ******************************************************************************* @file GPIO/GPIO_IOToggle/Src/stm32f1xx_it.c* @author MCD Application Team* @version V1.5.0* @date 14-April-2017* @brief Main Interrupt Service Routines.* This file provides template for all exceptions handler and* peripherals interrupt service routine.******************************************************************************* @attention** <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2>** Redistribution and use in source and binary forms, with or without modification,* are permitted provided that the following conditions are met:* 1. Redistributions of source code must retain the above copyright notice,* this list of conditions and the following disclaimer.* 2. Redistributions in binary form must reproduce the above copyright notice,* this list of conditions and the following disclaimer in the documentation* and/or other materials provided with the distribution.* 3. Neither the name of STMicroelectronics nor the names of its contributors* may be used to endorse or promote products derived from this software* without specific prior written permission.** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.********************************************************************************//* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32f1xx_it.h"/** @addtogroup STM32F1xx_HAL_Examples* @{*//** @addtogroup GPIO_IOToggle* @{*/
extern TIM_HandleTypeDef htim4;
extern UART_HandleTypeDef huart1;
/* USER CODE BEGIN EV */
extern void prvvUARTTxReadyISR(void);
extern void prvvUARTRxISR(void);
extern void prvvTIMERExpiredISR( void );
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*//* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*//******************************************************************************/
/* Cortex-M3 Processor Exceptions Handlers */
/******************************************************************************//*** @brief This function handles NMI exception.* @param None* @retval None*/
void NMI_Handler(void)
{
}/*** @brief This function handles Hard Fault exception.* @param None* @retval None*/
void HardFault_Handler(void)
{/* Go to infinite loop when Hard Fault exception occurs */while (1){}
}/*** @brief This function handles Memory Manage exception.* @param None* @retval None*/
void MemManage_Handler(void)
{/* Go to infinite loop when Memory Manage exception occurs */while (1){}
}/*** @brief This function handles Bus Fault exception.* @param None* @retval None*/
void BusFault_Handler(void)
{/* Go to infinite loop when Bus Fault exception occurs */while (1){}
}/*** @brief This function handles Usage Fault exception.* @param None* @retval None*/
void UsageFault_Handler(void)
{/* Go to infinite loop when Usage Fault exception occurs */while (1){}
}/*** @brief This function handles SVCall exception.* @param None* @retval None*/
void SVC_Handler(void)
{
}/*** @brief This function handles Debug Monitor exception.* @param None* @retval None*/
void DebugMon_Handler(void)
{
}/*** @brief This function handles PendSVC exception.* @param None* @retval None*/
void PendSV_Handler(void)
{
}/*** @brief This function handles SysTick Handler.* @param None* @retval None*/
void SysTick_Handler(void)
{HAL_IncTick();
}
/*** @brief This function handles TIM4 global interrupt.*/
void TIM4_IRQHandler(void)
{/* USER CODE BEGIN TIM4_IRQn 0 *//* USER CODE END TIM4_IRQn 0 */HAL_TIM_IRQHandler(&htim4);/* USER CODE BEGIN TIM4_IRQn 1 *//* USER CODE END TIM4_IRQn 1 */
}/*** @brief This function handles USART1 global interrupt.*/
void USART1_IRQHandler(void)
{/* USER CODE BEGIN USART1_IRQn 0 *//* USER CODE END USART1_IRQn 0 */HAL_UART_IRQHandler(&huart1);/* USER CODE BEGIN USART1_IRQn 1 */if(__HAL_UART_GET_IT_SOURCE(&huart1, UART_IT_RXNE)!= RESET) {prvvUARTRxISR();//接收中断}if(__HAL_UART_GET_IT_SOURCE(&huart1, UART_IT_TXE)!= RESET) {prvvUARTTxReadyISR();//发送中断}HAL_NVIC_ClearPendingIRQ(USART1_IRQn);HAL_UART_IRQHandler(&huart1);/* USER CODE END USART1_IRQn 1 */
}/* USER CODE BEGIN 1 */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) //定时器中断回调函数,用于连接porttimer.c文件的函数
{/* NOTE : This function Should not be modified, when the callback is needed,the __HAL_TIM_PeriodElapsedCallback could be implemented in the user file*/prvvTIMERExpiredISR( );
}
/******************************************************************************/
/* STM32F1xx Peripherals Interrupt Handlers */
/* Add here the Interrupt Handler for the used peripheral(s) (PPP), for the */
/* available peripheral interrupt handler's name please refer to the startup */
/* file (startup_stm32f1xx.s). */
/******************************************************************************//*** @brief This function handles PPP interrupt request.* @param None* @retval None*/
/*void PPP_IRQHandler(void)
{
}*//*** @}*//*** @}*//************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
现在让我检查一下是否还有其他地方需要修改。让我查看main.c文件中的Modbus初始化部分:
我需要修改main.c文件中的注释,将串口2改为串口1:
/* 串口2初始化在portserial.c中 */->/* 串口1初始化在portserial.c中 */
现在让我检查一下是否还有其他需要修改的地方。让我查看一下是否有其他文件引用了串口2:
这些是HAL库文件,不需要修改。让我检查一下是否有其他用户文件引用了串口2:
很好!现在让我检查一下是否有任何编译错误。让我查看一下GPIO初始化文件,确保PC9的时钟被正确启用:
我需要修改GPIO初始化文件,将PA1改为PC9,并启用GPIOC时钟:
/* USER CODE BEGIN Header */
/********************************************************************************* @file gpio.c* @brief This file provides code for the configuration* of all used GPIO pins.******************************************************************************* @attention** Copyright (c) 2025 STMicroelectronics.* All rights reserved.** This software is licensed under terms that can be found in the LICENSE file* in the root directory of this software component.* If no LICENSE file comes with this software, it is provided AS-IS.********************************************************************************/
/* USER CODE END Header *//* Includes ------------------------------------------------------------------*/
#include "gpio.h"/* USER CODE BEGIN 0 *//* USER CODE END 0 *//*----------------------------------------------------------------------------*/
/* Configure GPIO */
/*----------------------------------------------------------------------------*/
/* USER CODE BEGIN 1 *//* USER CODE END 1 *//** Configure pins as* Analog* Input* Output* EVENT_OUT* EXTI
*/
void MX_GPIO_Init(void)
{GPIO_InitTypeDef GPIO_InitStruct = {0};/* GPIO Ports Clock Enable */__HAL_RCC_GPIOD_CLK_ENABLE();__HAL_RCC_GPIOA_CLK_ENABLE();__HAL_RCC_GPIOC_CLK_ENABLE();/*Configure GPIO pin Output Level */HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9, GPIO_PIN_RESET);/*Configure GPIO pin : PC9 */GPIO_InitStruct.Pin = GPIO_PIN_9;GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;GPIO_InitStruct.Pull = GPIO_NOPULL;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);}/* USER CODE BEGIN 2 *//* USER CODE END 2 */
总结
我已经成功将您的STM32F103工程从串口2改为串口1,并修改了相应的引脚配置。主要修改包括:
串口配置修改:
- 串口2 (PA2/PA3) → 串口1 (PA9/PA10)
- 485控制引脚 PA1 → PC9
修改的文件:
PRO2/Core/Inc/usart.h
- 更新了所有串口相关的宏定义和引脚配置PRO2/Core/Src/usart.c
- 修改了串口初始化函数和MSP配置PRO2/Drivers/BSP/STM32MB/port/portserial.c
- 更新了Modbus串口端口层PRO2/Core/Src/stm32f1xx_it.c
- 修改了中断处理函数PRO2/Core/Src/main.c
- 更新了注释PRO2/Core/Src/gpio.c
- 添加了GPIOC时钟使能并配置PC9引脚
关键修改点:
- 所有
USART2
改为USART1
- 所有
huart2
改为huart1
- 引脚从
PA2/PA3
改为PA9/PA10
- 485控制引脚从
PA1
改为PC9
- 启用了
GPIOC
时钟以支持PC9引脚
现在您的工程已经配置为使用串口1(PA9/PA10)和485控制引脚PC9。请确保硬件连接正确,然后重新编译工程进行测试。