stm32 通過串列埠控制led燈亮滅 並通過lcd顯示led燈的亮滅狀態
阿新 • • 發佈:2019-02-14
#include "main.h" #include "stm32f0xx_hal.h" /* USER CODE BEGIN Includes */ #include "lcd.h" /* USER CODE END Includes */ /* Private variables ---------------------------------------------------------*/ UART_HandleTypeDef huart1; /* USER CODE BEGIN PV */ /* Private variables ---------------------------------------------------------*/ /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); static void MX_GPIO_Init(void); static void MX_USART1_UART_Init(void); /* USER CODE BEGIN PFP */ /* Private function prototypes -----------------------------------------------*/ /* USER CODE END PFP */ /* USER CODE BEGIN 0 */ /* USER CODE END 0 */ /** * @brief The application entry point. * * @retval None */ 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(); MX_USART1_UART_Init(); /* USER CODE BEGIN 2 */ Lcd_Init(); Lcd_Clear(WHITE); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ Gui_DrawFont_GBK16(0, 0, BLACK, WHITE, "LED1 OFF"); Gui_DrawFont_GBK16(0, 25, BLACK, WHITE, "LED2 OFF"); Gui_DrawFont_GBK16(0, 50, BLACK, WHITE, "LED3 OFF"); HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2, GPIO_PIN_SET); while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ uint8_t data = 0; HAL_UART_Receive_IT(&huart1, &data, 1); if(data == '1') { HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET); HAL_UART_Transmit(&huart1, &data, 1, 0); Gui_DrawFont_GBK16(0, 0, BLACK, WHITE, "LED1 ON"); //HAL_Delay(500); } else if(data == '2') { HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET); HAL_UART_Transmit(&huart1, &data, 1, 0); Gui_DrawFont_GBK16(0, 0, BLACK, WHITE, "LED1 OFF"); //HAL_Delay(500); } else if(data == '3') { HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_RESET); HAL_UART_Transmit(&huart1, &data, 1, 0); Gui_DrawFont_GBK16(0, 25, BLACK, WHITE, "LED2 ON"); //HAL_Delay(500); } else if(data == '4') { HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_SET); HAL_UART_Transmit(&huart1, &data, 1, 0); Gui_DrawFont_GBK16(0, 25, BLACK, WHITE, "LED2 OFF"); //HAL_Delay(500); } else if(data == '5') { HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2, GPIO_PIN_RESET); HAL_UART_Transmit(&huart1, &data, 1, 0); Gui_DrawFont_GBK16(0, 50, BLACK, WHITE, "LED3 ON"); //HAL_Delay(500); } else if(data == '6') { HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2, GPIO_PIN_SET); HAL_UART_Transmit(&huart1, &data, 1, 0); Gui_DrawFont_GBK16(0, 50, BLACK, WHITE, "LED3 OFF"); //HAL_Delay(500); } } /* USER CODE END 3 */ }