基于stm32G070CBT6的串口1和串口3的中断完成rs485的数据接收和发送,生成代码
时间: 2024-04-08 17:28:42 浏览: 185
以下是基于STM32G070CBT6的串口1和串口3的中断完成RS485数据接收和发送的代码示例。您可以在HAL库的基础上进行修改和扩展。
```c
#include "stm32g0xx.h"
#include "stm32g0xx_hal.h"
#define RS485_DE_GPIO_Port GPIOA
#define RS485_DE_Pin GPIO_PIN_8
#define RS485_RE_GPIO_Port GPIOA
#define RS485_RE_Pin GPIO_PIN_9
UART_HandleTypeDef huart1;
UART_HandleTypeDef huart3;
uint8_t rx1_data; // 接收缓冲区数据
uint8_t rx3_data; // 接收缓冲区数据
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
static void MX_USART3_UART_Init(void);
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if (huart->Instance == USART1)
{
// 处理USART1接收中断
// ...
// 您的代码逻辑
// ...
HAL_UART_Receive_IT(&huart1, &rx1_data, 1); // 启动下一次接收
}
else if (huart->Instance == USART3)
{
// 处理USART3接收中断
// ...
// 您的代码逻辑
// ...
HAL_UART_Receive_IT(&huart3, &rx3_data, 1); // 启动下一次接收
}
}
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
if (huart->Instance == USART1)
{
// 处理USART1发送中断
// ...
// 您的代码逻辑
// ...
HAL_GPIO_WritePin(RS485_RE_GPIO_Port, RS485_RE_Pin, GPIO_PIN_SET); // 切换为接收模式
}
else if (huart->Instance == USART3)
{
// 处理USART3发送中断
// ...
// 您的代码逻辑
// ...
HAL_GPIO_WritePin(RS485_RE_GPIO_Port, RS485_RE_Pin, GPIO_PIN_SET); // 切换为接收模式
}
}
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART1_UART_Init();
MX_USART3_UART_Init();
while (1)
{
// 主循环中的其他任务
// ...
// 您的代码逻辑
// ...
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure LSE Drive Capability
*/
HAL_PWR_EnableBkUpAccess();
__HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_MEDIUMHIGH);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_LSE;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.LSEState = RCC_LSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL4;
RCC_OscInitStruct.PLL.PLLDIV = RCC_PLL_DIV2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
| RCC_CLOCKTYPE_PCLK1;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
{
Error_Handler();
}
}
static void MX_USART1_UART_Init(void)
{
huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
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)
{
Error_Handler();
}
// 启动接收中断
HAL_UART_Receive_IT(&huart1, &rx1_data, 1);
}
static void MX_USART3_UART_Init(void)
{
huart3.Instance = USART3;
huart3.Init.BaudRate = 115200;
huart3.Init.WordLength = UART_WORDLENGTH_8B;
huart3.Init.StopBits = UART_STOPBITS_1;
huart3.Init.Parity = UART_PARITY_NONE;
huart3.Init.Mode = UART_MODE_TX_RX;
huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart3.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart3) != HAL_OK)
{
Error_Handler();
}
// 启动接收中断
HAL_UART_Receive_IT(&huart3, &rx3_data, 1);
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOA_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(RS485_RE_GPIO_Port, RS485_RE_Pin, GPIO_PIN_SET);
/*Configure GPIO pin : RS485_RE_Pin */
GPIO_InitStruct.Pin = RS485_RE_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(RS485_RE_GPIO_Port, &GPIO_InitStruct);
/*Configure GPIO pin : RS485_DE_Pin */
GPIO_InitStruct.Pin = RS485_DE_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(RS485_DE_GPIO_Port, &GPIO_InitStruct);
}
```
在这个示例代码中,我们使用了USART1和USART3作为RS485通信的两个串口,并在中断回调函数中处理接收和发送的逻辑。同时,我们使用了GPIO引脚来控制RS485芯片的发送使能(DE)和接收使能(RE)引脚。
请注意,这只是一个基本的框架,您需要根据实际需求进行修改和扩展,例如添加数据处理和错误处理等。另外,还需要根据您的具体硬件连接情况来配置引脚和串口参数。
希望这个示例能对您有所帮助!
阅读全文