笔者跟着B站铁头山羊视频学习 STM32-HAL库 开发教程。下面对HAL库有关UART课程知识和应用做一个梳理。
省流:
uint8_t byteNumber = 0x5a;uint8_t byteArray[] = {0,1,2,3,4,5};char ch = 'a';char *str = "Hello word";HAL_UART_Transmit(&huart1,&byteNumber,1,HAL_MAX_DELAY); // 发送一个字节HAL_UART_Transmit(&huart1,byteArray,6,HAL_MAX_DELAY); // 发送一个多个字节HAL_UART_Transmit(&huart1,(uint8_t *)&ch,1,HAL_MAX_DELAY); // 发送一个字符HAL_UART_Transmit(&huart1,(uint8_t *)str,strlen(str),HAL_MAX_DELAY); // 发送一个字符串
发送数据给 串口外设。
uint8_t Data_Receive;HAL_UART_Receive(&huart1,&Data_Receive,1,HAL_MAX_DELAY); if(Data_Receive == 0)
{HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_RESET);
}
将串口接收到的数据保存到自定义的变量中,再判断。
1、串口UART基本原理
常用8位无校验的数据帧格式:1 个起始位、1 个停止位、8个数据位。
2、示例
2.1 简单数据传输
(1) GPIO引脚配置
将PA9发送引脚设置为复用推挽输出,系统自动配置。
将PA9发送引脚设置为输入模式。手动配置上拉电阻使能,以防这个引脚意外断开,像按键一样。
(2)UART参数配置
选择异步模式(Asynchronus),对下面的参数设置(Parameter Setting)进行配置。
(3)程序
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();/* USER CODE BEGIN 2 *//* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */while (1){HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET); // 点亮LED,C13置0,开漏输出低电压HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_SET); // 点亮LED,C9置1,推挽输出高电压HAL_Delay(500);HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET); // 熄灭LED,C13置1,开漏输出高阻态HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_RESET); // 熄灭LED,C9置0,推挽输出低电压HAL_Delay(500);/* USER CODE END WHILE *//* USER CODE BEGIN 3 */}/* USER CODE END 3 */
}
2.2 通过串口发送命令点亮LED灯
(1) UART参数配置
(2)程序
int main(void)
{/* USER CODE BEGIN 1 */uint8_t byteNumber = 0x5a;uint8_t byteArray[] = {0,1,2,3,4,5};char ch = 'a';char *str = "Hello word";HAL_UART_Transmit(&huart1,&byteNumber,1,HAL_MAX_DELAY);HAL_UART_Transmit(&huart1,byteArray,6,HAL_MAX_DELAY);HAL_UART_Transmit(&huart1,(uint8_t *)&ch,1,HAL_MAX_DELAY);HAL_UART_Transmit(&huart1,(uint8_t *)str,strlen(str),HAL_MAX_DELAY);/* 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_USART1_UART_Init();/* USER CODE BEGIN 2 *//* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */while (1){char *Data_Receive;HAL_UART_Receive(&huart1,(uint8_t *)Data_Receive,sizeof(Data_Receive),HAL_MAX_DELAY); // 接收UART数据保存到Data_Receiveif( strncmp(Data_Receive,"on",2)==0) // 判断{HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_RESET); // 点灯}else if( strncmp(Data_Receive,"off",3)==0){HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_SET);}/* USER CODE END WHILE *//* USER CODE BEGIN 3 */}/* USER CODE END 3 */
}