STM32CubeIDE(串口)

目录

一、轮询模式

[1.1 配置USART2为异步模式](#1.1 配置USART2为异步模式)

[1.2 500ms发送一次消息](#1.2 500ms发送一次消息)

[1.3 通信结果](#1.3 通信结果)

[1.4 串口控制LED](#1.4 串口控制LED)

二、中断收发

[2.1 开启中断](#2.1 开启中断)

[2.2 中断发送接收](#2.2 中断发送接收)

[2.2.1 中断发送只需要调用接口](#2.2.1 中断发送只需要调用接口)

[2.2.2 中断接收](#2.2.2 中断接收)

[2.3 实验结果](#2.3 实验结果)

三、DMA模式与收发不定长数据

[3.1 DMA通道配置](#3.1 DMA通道配置)

[3.2 DMA发送接收函数](#3.2 DMA发送接收函数)

[3.3 使用空闲中断接收不定长数据](#3.3 使用空闲中断接收不定长数据)


一、轮询模式

1.1 配置USART2为异步模式

1.2 500ms发送一次消息

1.3 通信结果

1.4 串口控制LED

cpp 复制代码
  uint8_t reciveDate[2];
  while (1)
  {
	  HAL_UART_Receive(&huart1, reciveDate, 2, HAL_MAX_DELAY);
	  HAL_UART_Transmit(&huart1, reciveDate, 2, 100);
	  GPIO_PinState state=GPIO_PIN_SET;
	  if(reciveDate[1]=='1')
	  {
		  state=GPIO_PIN_RESET;
	  }
	  if(reciveDate[0]=='R')
	  {
		  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, state);
	  }
	  else if(reciveDate[0]=='G')
	  {
		  HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, state);
	  }
  }
}

二、中断收发

2.1 开启中断

2.2 中断发送接收

2.2.1 中断发送只需要调用接口

cpp 复制代码
HAL_UART_Transmit_IT(&huart1, reciveDate, 2);

2.2.2 中断接收

①在程序起始开启中断

②重新定义stm32f1xx_hal_uart.c中的函数

cpp 复制代码
__weak void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
  /* Prevent unused argument(s) compilation warning */
  UNUSED(huart);
  /* NOTE: This function should not be modified, when the callback is needed,
           the HAL_UART_RxCpltCallback could be implemented in the user file
   */
}
cpp 复制代码
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
	HAL_UART_Transmit_IT(&huart1, reciveDate, 2);
	GPIO_PinState state = GPIO_PIN_SET;

	if (reciveDate[1] == '1')
	{
		state = GPIO_PIN_RESET;
	}
	if (reciveDate[0] == 'G')
	{
		HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, state);
	}
	HAL_UART_Receive_IT(&huart1, reciveDate, 2);//再次启动中断接收
}

2.3 实验结果

三、DMA模式与收发不定长数据

3.1 DMA通道配置

3.2 DMA发送接收函数

只需要将_IT修改为_DMA即可,DMA模式还是有中断参与其中

cpp 复制代码
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
	HAL_UART_Transmit_DMA(&huart1, reciveDate, 2);//dma发送
	GPIO_PinState state = GPIO_PIN_SET;

	if (reciveDate[1] == '1')
	{
		state = GPIO_PIN_RESET;
	}
	if (reciveDate[0] == 'G')
	{
		HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, state);
	}
	HAL_UART_Receive_DMA(&huart1, reciveDate, 2);//DMA接收
}

3.3 使用空闲中断接收不定长数据

只有当接收端不再有数据输入时才会触发空闲中断,重新定义

cpp 复制代码
__weak void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
{
  /* Prevent unused argument(s) compilation warning */
  UNUSED(huart);
  UNUSED(Size);

  /* NOTE : This function should not be modified, when the callback is needed,
            the HAL_UARTEx_RxEventCallback can be implemented in the user file.
   */
}
cpp 复制代码
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
{
	if(huart==&huart1)
	{
		HAL_UART_Transmit_DMA(&huart1, reciveDate, Size);//发�?�与接收相同的Size长度的字节数

		HAL_UARTEx_ReceiveToIdle_DMA(&huart1, reciveDate, sizeof(reciveDate));//接收不定长数�?
	}
}
相关推荐
Tronlong创龙4 小时前
SBC‑TLT153 嵌入式 Linux 应用开发,交叉编译与 GDB 调试实践(上)
嵌入式硬件·嵌入式开发·硬件开发·工业控制·工业开发板
巴渝小禹4 小时前
单片机死循环delay_us()计算方法
单片机·嵌入式硬件
Yiran_G4 小时前
LoRa串口模组和SoC模组怎么选?从MCU架构看无线通信方案设计
嵌入式硬件·物联网
HQB20265 小时前
CN6150|简芯维尔 6V/500mA 高速低噪声 CMOS‑LDO 线性稳压器
嵌入式硬件·硬件工程
科芯创展6 小时前
XZ3445 高集成升压/升降压DC-DC电源芯片技术解析
单片机·嵌入式硬件
艾芯微科技6 小时前
高充放电流锂电充放 SoC:ETA9885E10,便携储能、小移动电源 BOM 选型笔记
网络·单片机·嵌入式硬件·集成测试·51单片机
艾芯微科技7 小时前
钰泰 ETA6973PQ3Y|QFN3×3 2A 同步降压 Buck,I2C 可配置多档输出,动态调压 PMIC
网络·单片机·嵌入式硬件·fpga开发·集成测试·51单片机
Brilliantwxx7 小时前
【STM32】 通用定时器:输入捕获与输出比较详解 测脉宽、测频率
stm32·单片机·嵌入式硬件·mcu
意法半导体STM328 小时前
【官方原创】LAT1703 STM32C5 LL Cordic例程
stm32·单片机·嵌入式硬件·stm32c5·ll cordic