STM32CubeMX+串口通信控制led
基础配置见:https://www.cnblogs.com/jsit-dj-it/p/11967153.html
一、配置USART外设的工作参数
二、生成代码
在main.c编写业务逻辑代码
/* USER CODE BEGIN 4 */ void USER_UART_IRQHandler(UART_HandleTypeDef *huart) { /*__HAL_UART_GET_FLAG 获取 SR 寄存器标志位状态 UART_FLAG_RXNE 接受数据寄存器非空标志位 UART_FLAG_RXNE (0x1UL << USART_SR_RXNE_Pos) /*!< 0x00000020 */ UART_RX_STATE_READY=0x00自定义 */ //接收数据 if ((__HAL_UART_GET_FLAG(&huart1, UART_FLAG_RXNE) != RESET)) { //接收到一帧中的第一个字节 if (uart1RxState == UART_RX_STATE_READY) { uart1RxState = UART_RX_STATE_START; __HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE); uart1RxCounter = 0; uart1RxBuf[uart1RxCounter] = (uint8_t)(huart1.Instance->DR & (uint8_t)0x00FF); uart1RxCounter++; } else if (uart1RxState == UART_RX_STATE_START) { uart1RxBuf[uart1RxCounter] = (uint8_t)(huart1.Instance->DR & (uint8_t)0x00FF); uart1RxCounter++; } __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE); } if ((__HAL_UART_GET_FLAG(&huart1, UART_FLAG_IDLE) != RESET))//进入空闲中断 { __HAL_UART_DISABLE_IT(&huart1, UART_IT_IDLE); //关了空闲中断 __HAL_UART_DISABLE_IT(&huart1, UART_IT_RXNE); //关了接收完成中断 uart1RxState = UART_RX_STATE_DEAL; //状态表明一帧数据接收完成了,需要处理。处理完以后再把接收中断打开 } } /* USER CODE END 4 */
流水灯显示代码
if(uart1RxState == UART_RX_STATE_DEAL) { if(strstr((const char *)uart1RxBuf, stringMode1) != NULL) { printf("I'm in mode_1!\r\n"); ledMode = 1; } else if(strstr((const char *)uart1RxBuf, stringMode2) != NULL) { printf("I'm in mode_2!\r\n"); ledMode = 2; } else if(strstr((const char *)uart1RxBuf, stringStop) != NULL) { printf("I'm stop!\r\n"); ledMode = 0; } __HAL_UART_ENABLE_IT(&huart1, UART_IT_RXNE); uart1RxState = UART_RX_STATE_READY; uart1RxCounter = 0; memset(uart1RxBuf, 0, UART1_RCV_MAX); } switch (ledMode) { case 1: LED_State_Set(LED_value); LED_value = cror(LED_value, 1); HAL_Delay(1000); break; case 2: LED_State_Set(LED_value); LED_value = crol(LED_value, 1); HAL_Delay(1000); break; case 0: LED_value = 0xFF; LED_State_Set(LED_value); break; default: break; }
流水灯
/* USER CODE BEGIN 0 */ uint8_t cror(uint8_t numbers, uint8_t bits) { uint8_t right = numbers >> bits; uint8_t left = numbers << (8 - bits); uint8_t temp = left | right; return temp; } uint8_t crol(uint8_t numbers, uint8_t bits) { uint8_t right = numbers << bits; uint8_t left = numbers >> (8 - bits); uint8_t temp = left | right; return temp; } void LED_State_Set(uint8_t numbers) { HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, ((numbers & 0x01) >> 0) ? GPIO_PIN_SET : GPIO_PIN_RESET); HAL_GPIO_WritePin(LED2_GPIO_Port, LED2_Pin, ((numbers & 0x02) >> 1) ? GPIO_PIN_SET : GPIO_PIN_RESET); HAL_GPIO_WritePin(LED3_GPIO_Port, LED3_Pin, ((numbers & 0x04) >> 2) ? GPIO_PIN_SET : GPIO_PIN_RESET); HAL_GPIO_WritePin(LED4_GPIO_Port, LED4_Pin, ((numbers & 0x08) >> 3) ? GPIO_PIN_SET : GPIO_PIN_RESET); HAL_GPIO_WritePin(LED5_GPIO_Port, LED5_Pin, ((numbers & 0x10) >> 4) ? GPIO_PIN_SET : GPIO_PIN_RESET); HAL_GPIO_WritePin(LED6_GPIO_Port, LED6_Pin, ((numbers & 0x20) >> 5) ? GPIO_PIN_SET : GPIO_PIN_RESET); HAL_GPIO_WritePin(LED7_GPIO_Port, LED7_Pin, ((numbers & 0x40) >> 6) ? GPIO_PIN_SET : GPIO_PIN_RESET); HAL_GPIO_WritePin(LED8_GPIO_Port, LED8_Pin, ((numbers & 0x80) >> 7) ? GPIO_PIN_SET : GPIO_PIN_RESET); } /* USER CODE END 0 */