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));//接收不定长数�?
	}
}
相关推荐
国科安芯9 分钟前
AS32A601与ASM1042芯片在电力系统自动化监控中的应用效能分析
单片机·物联网·自动化
才鲸嵌入式5 小时前
C++相比于C语言增加了哪些概念?
c语言·c++·单片机·嵌入式·arm·面向对象·软件
dujunqiu9 小时前
S32DS上进行S32K328的时钟配置,LPUART时钟配置步骤详解
单片机·mcu
Peter_Deng.11 小时前
单片机 - STM32F407 ADC 模式详解:单次转换、连续转换、扫描模式、非扫描模式
stm32·单片机·嵌入式硬件
iFulling11 小时前
【单片机】51单片机练习代码
单片机·嵌入式硬件·51单片机
华普微HOPERF13 小时前
让温度“说话”,数字温度传感器如何智能感知温度?
科技·单片机·嵌入式硬件·物联网·智能家居
iCxhust13 小时前
PC16550 UART接收中断处理完整示例代码
c语言·开发语言·stm32·单片机·嵌入式硬件
NEWEVA__zzera2214 小时前
记录存储的使用
经验分享·单片机
iFulling15 小时前
【单片机】51单片机学习笔记
单片机·学习·51单片机
YONYON-R&D16 小时前
STM32L431中,低功耗模式下的仿真调试功能受到限制
单片机·嵌入式硬件