蓝桥杯-基于STM32G432RBT6的LCD进阶(LCD界面切换以及高亮显示界面)

devtools/2024/11/15 7:31:46/

目录

一、页面切换内容详解

1.逻辑解释

2.代码详解

code.c(内含详细讲解)

code.h

main.c

3.效果图片展示

​编辑

二、页面选项高亮内容详解

1.逻辑解释

2.读入数据

FIRST.第一种高亮类型

code.c(内含代码详解)

code.h

main.c

SECOND.第二种高亮类型

3.效果展示

开源代码

一、页面切换内容详解

1.逻辑解释

  1. 首先通过 LCD_Clear(White) 函数将 LCD 屏幕清除为白色。
  2. lcd_page 的值增加 1。
  3. 然后进行判断,如果 lcd_page 达到 3,则将其重置为 0,实现页面的循环切换。

当检测到按键一被按下(Key_down==1)时:

  1. count 的值增加 1。

这样,通过按键二可以切换页面,通过按键一可以使 count 值增加。

举例来说:

  1. 初始时,lcd_page 为 0,count 为 0。
  2. 按下按键二一次,lcd_page 变为 1。
  3. 再按下按键二一次,lcd_page 变为 2。
  4. 接着按下按键二一次,lcd_page 变为 0,完成一次页面循环。
  5. 同时在这个过程中,每按下一次按键一,count 值就增加 1。

2.代码详解

code.c(内含详细讲解)
#include "headfile.h"void led_show(uint8_t led,uint8_t mode)
{
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);if(mode)
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_8<<(led-1),GPIO_PIN_RESET);else
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_8<<(led -1),GPIO_PIN_RESET);HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);}uint8_t Key_Scan(void)
{uint8_t Key_val=0;if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0)==GPIO_PIN_RESET){Key_val=1;}if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1)==GPIO_PIN_RESET){Key_val=2;}if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_2)==GPIO_PIN_RESET){Key_val=3;}if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0)==GPIO_PIN_RESET){Key_val=4;}return Key_val;
}
extern uint8_t key_rval;
uint8_t Key_up,Key_down,key_old;
uint8_t count;
uint8_t  lcd_page;//定义一个变量存储
void Key_Proc(void)	
{key_rval=Key_Scan();Key_down=key_rval&(key_rval^key_old);Key_up=~key_rval&(key_rval^key_old);key_old=key_rval;if(Key_down==2)//按键二切换页面{ LCD_Clear(White);//清屏操作,不然会有残影lcd_page++;//页面加一if(lcd_page==3) lcd_page=0;//以为没有lcd_page==3,所以回到0}if(Key_down==1)//按键一数值加一{count++;//如果变量 Key_down 的值等于 1,则将变量 count 的值增加 1。
//这是一个简单的条件判断和操作,用于根据特定条件来改变某个变量的值。}}char buf[21];
void Lcd_proc(void)
{if(lcd_page==0){LCD_DisplayStringLine(Line1,(uint8_t*)"       jinke!       ");//这行代码是将字符串 " jinke! " 强制转换为指向 uint8_t 类型数据的指针。}if(lcd_page==1){sprintf(buf,"      count:%d      ",count);//"count:%d" 是格式字符串,其中 %d 表示要插入一个整数变量 count 。//执行完这行代码后,buf 中就会存储一个包含 "count:" 和 count 的具体数值的字符串。LCD_DisplayStringLine(Line4,(uint8_t*)buf);//“LCD_DisplayStringLine(Line4, (uint8_t*)buf)” 表示在特定的行(Line4)上显示字符串,//其中字符串的内容来自于指向 uint8_t 类型的指针 buf 所指向的缓冲区。}if(lcd_page==2){LCD_DisplayStringLine(Line4,(uint8_t*)"       BAYBAY!      ");}
}
code.h
#ifndef _code_h
#define _code_h
#include "stm32g4xx.h"                  // Device headervoid led_show(uint8_t led,uint8_t mode);
void key_scan(void);
void Lcd_proc(void);
void Key_Proc(void);
#endif
main.c

/* USER CODE BEGIN Header */
/********************************************************************************* @file           : main.c* @brief          : Main program body******************************************************************************* @attention** Copyright (c) 2024 STMicroelectronics.* All rights reserved.** This software is licensed under terms that can be found in the LICENSE file* in the root directory of this software component.* If no LICENSE file comes with this software, it is provided AS-IS.********************************************************************************/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "gpio.h"/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "headfile.h"
/* USER CODE END Includes *//* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD *//* USER CODE END PTD *//* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD *//* USER CODE END PD *//* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM *//* USER CODE END PM *//* Private variables ---------------------------------------------------------*//* USER CODE BEGIN PV */uint8_t key_rval;
/* USER CODE END PV *//* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP *//* USER CODE END PFP *//* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 *//* USER CODE END 0 *//*** @brief  The application entry point.* @retval int*/
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 */LCD_Init();LCD_Clear(White);LCD_SetBackColor(White);LCD_SetTextColor(Black);/* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */while (1){ Key_Proc();Lcd_proc();	/* USER CODE END WHILE *//* USER CODE BEGIN 3 */}/* USER CODE END 3 */
}/*** @brief System Clock Configuration* @retval None*/
void SystemClock_Config(void)
{RCC_OscInitTypeDef RCC_OscInitStruct = {0};RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};/** Configure the main internal regulator output voltage*/HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);/** Initializes the RCC Oscillators according to the specified parameters* in the RCC_OscInitTypeDef structure.*/RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;RCC_OscInitStruct.HSEState = RCC_HSE_ON;RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV3;RCC_OscInitStruct.PLL.PLLN = 20;RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK){Error_Handler();}/** Initializes the CPU, AHB and APB buses clocks*/RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK){Error_Handler();}
}/* USER CODE BEGIN 4 *//* USER CODE END 4 *//*** @brief  This function is executed in case of error occurrence.* @retval None*/
void Error_Handler(void)
{/* USER CODE BEGIN Error_Handler_Debug *//* User can add his own implementation to report the HAL error return state */__disable_irq();while (1){}/* USER CODE END Error_Handler_Debug */
}#ifdef  USE_FULL_ASSERT
/*** @brief  Reports the name of the source file and the source line number*         where the assert_param error has occurred.* @param  file: pointer to the source file name* @param  line: assert_param error line source number* @retval None*/
void assert_failed(uint8_t *file, uint32_t line)
{/* USER CODE BEGIN 6 *//* User can add his own implementation to report the file name and line number,ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) *//* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

3.效果图片展示

二、页面选项高亮内容详解

1.逻辑解释

line_flag定义变量存储

  • 当页面为 1 时,根据 line_flag 的不同设置文本颜色,并显示与 count1count2count3 相关的字符串。

2.读入数据

FIRST.第一种高亮类型

code.c(内含代码详解)
#include "headfile.h"void led_show(uint8_t led,uint8_t mode)
{
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);if(mode)
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_8<<(led-1),GPIO_PIN_RESET);else
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_8<<(led -1),GPIO_PIN_RESET);HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);}
uint8_t Key_Scan(void)
{uint8_t Key_val=0;if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0)==GPIO_PIN_RESET){Key_val=1;}if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1)==GPIO_PIN_RESET){Key_val=2;}if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_2)==GPIO_PIN_RESET){Key_val=3;}if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0)==GPIO_PIN_RESET){Key_val=4;}return Key_val;
}
extern uint8_t key_rval;
uint8_t Key_up,Key_down,key_old;
uint8_t count1,count2,count3;
uint8_t  lcd_page,line_flag;//定义一个变量存储
void Key_Proc(void)	
{key_rval=Key_Scan();Key_down=key_rval&(key_rval^key_old);Key_up=~key_rval&(key_rval^key_old);key_old=key_rval;if(Key_down==1)//按键一切换页面{ LCD_Clear(White);//清屏操作,不然会有残影LCD_SetBackColor(White);LCD_SetTextColor(Black);//需要加此函数lcd_page++;//页面加一if(lcd_page==3) lcd_page=0;//以为没有lcd_page==3,所以回到0}if(Key_down==2&&lcd_page==1)//只在第二个页面起作用{line_flag++; if(line_flag==3)line_flag =0;//还可以写成if(++line_flag==3)line_flag =0;	}if(Key_down==3&&lcd_page==1)//只在第二个页面起作用{if(line_flag==0)count1++; if(line_flag==1)count2++;if(line_flag==2)count3++;}if(Key_down==4&&lcd_page==1)//只在第二个页面起作用{if(line_flag==0)count1--; if(line_flag==1)count2--;if(line_flag==2)count3--;}
}
char buf[21];
void Lcd_proc(void)
{if(lcd_page==0){LCD_DisplayStringLine(Line1,(uint8_t*)"       jinke!       ");//这行代码是将字符串 " jinke! " 强制转换为指向 uint8_t 类型数据的指针。}if(lcd_page==1)	{   if(line_flag==0) LCD_SetTextColor(Blue);//可替换成LCD_SetBackColor(Blue);else LCD_SetTextColor(Black);sprintf(buf,"    count1:%03d     ",count1);//"count:%d" 是格式字符串,其中 %d 表示要插入一个整数变量 count 。//执行完这行代码后,buf 中就会存储一个包含 "count:" 和 count 的具体数值的字符串。LCD_DisplayStringLine(Line3,(uint8_t*)buf);//“LCD_DisplayStringLine(Line4, (uint8_t*)buf)” 表示在特定的行(Line4)上显示字符串,//其中字符串的内容来自于指向 uint8_t 类型的指针 buf 所指向的缓冲区。if(line_flag==1) LCD_SetTextColor(Blue);else LCD_SetTextColor(Black);sprintf(buf,"    count2:%03d     ",count2);LCD_DisplayStringLine(Line4,(uint8_t*)buf);if(line_flag==2) LCD_SetTextColor(Blue);else LCD_SetTextColor(Black);sprintf(buf,"    count3:%03d     ",count3);LCD_DisplayStringLine(Line5,(uint8_t*)buf);		  }if(lcd_page==2){LCD_DisplayStringLine(Line4,(uint8_t*)"       BAYBAY!      ");}
}
code.h
#ifndef _code_h
#define _code_h
#include "stm32g4xx.h"                  // Device headervoid led_show(uint8_t led,uint8_t mode);
void key_scan(void);
void Lcd_proc(void);
void Key_Proc(void);
#endif
main.c
/* USER CODE BEGIN Header */
/********************************************************************************* @file           : main.c* @brief          : Main program body******************************************************************************* @attention** Copyright (c) 2024 STMicroelectronics.* All rights reserved.** This software is licensed under terms that can be found in the LICENSE file* in the root directory of this software component.* If no LICENSE file comes with this software, it is provided AS-IS.********************************************************************************/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "gpio.h"/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "headfile.h"
/* USER CODE END Includes *//* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD *//* USER CODE END PTD *//* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD *//* USER CODE END PD *//* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM *//* USER CODE END PM *//* Private variables ---------------------------------------------------------*//* USER CODE BEGIN PV */uint8_t key_rval;
/* USER CODE END PV *//* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP *//* USER CODE END PFP *//* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 *//* USER CODE END 0 *//*** @brief  The application entry point.* @retval int*/
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 */LCD_Init();LCD_Clear(White);LCD_SetBackColor(White);LCD_SetTextColor(Black);/* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */while (1){ Key_Proc();Lcd_proc();	/* USER CODE END WHILE *//* USER CODE BEGIN 3 */}/* USER CODE END 3 */
}/*** @brief System Clock Configuration* @retval None*/
void SystemClock_Config(void)
{RCC_OscInitTypeDef RCC_OscInitStruct = {0};RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};/** Configure the main internal regulator output voltage*/HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);/** Initializes the RCC Oscillators according to the specified parameters* in the RCC_OscInitTypeDef structure.*/RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;RCC_OscInitStruct.HSEState = RCC_HSE_ON;RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV3;RCC_OscInitStruct.PLL.PLLN = 20;RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK){Error_Handler();}/** Initializes the CPU, AHB and APB buses clocks*/RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK){Error_Handler();}
}/* USER CODE BEGIN 4 *//* USER CODE END 4 *//*** @brief  This function is executed in case of error occurrence.* @retval None*/
void Error_Handler(void)
{/* USER CODE BEGIN Error_Handler_Debug *//* User can add his own implementation to report the HAL error return state */__disable_irq();while (1){}/* USER CODE END Error_Handler_Debug */
}#ifdef  USE_FULL_ASSERT
/*** @brief  Reports the name of the source file and the source line number*         where the assert_param error has occurred.* @param  file: pointer to the source file name* @param  line: assert_param error line source number* @retval None*/
void assert_failed(uint8_t *file, uint32_t line)
{/* USER CODE BEGIN 6 *//* User can add his own implementation to report the file name and line number,ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) *//* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

SECOND.第二种高亮类型

只需要将LCD_SetTextColor(Blue)设置为LCD_SetBackColor

3.效果展示

key1换页

key2换行

key3加数

key4减数

演示


开源代码

通过网盘分享的文件:新建文件夹.zip
链接: https://pan.baidu.com/s/159nyhzLd4xkYirNAKgf00w?pwd=0820 提取码: 0820


http://www.ppmy.cn/devtools/114858.html

相关文章

flutter中常见的跨组件通讯方式

在 Flutter 开发中&#xff0c;事件管理和组件间通信是非常重要的。EventBus 和 NotificationListener 是两种常用的模式&#xff0c;它们各自有不同的使用场景和优势劣势。本文将对这两者进行比较分析&#xff0c;并提供代码示例。 在 Flutter 开发中&#xff0c;事件管理和组…

【JS】垃圾回收机制与内存泄漏

垃圾回收机制与内存泄漏 内存泄漏是指在程序运行过程中&#xff0c;某些不再需要使用的内存没有被正确释放&#xff0c;导致这些内存资源无法再被系统重新使用。随着程序的持续运行&#xff0c;内存泄漏会不断消耗可用内存&#xff0c;最终可能导致内存不足、系统变慢&#xf…

MySQL的登陆错误:ERROR 1049 (42000): Unknown database ‘root‘

MySQL的登陆错误&#xff1a;ERROR 1049 (42000): Unknown database ‘root’ 安装MySQL的时候&#xff0c;到网上查的命令行登陆MySQL的方法都是mysql -u root -p password mysql -r root -p 123456但是奇怪的是这条命令我输进去死活都不对&#xff0c;它都会要求再输入一遍…

Java servlet《网吧机房管理系统浅析》

网吧机房管理系统在网吧运营中起着至关重要的作用。 对于用户而言&#xff0c;该系统提供了便捷的登录方式&#xff0c;通过用户名和密码可准确显示所在网吧机房号&#xff0c;便于快速定位。同时&#xff0c;合理的机房分配功能确保用户获得良好上网体验。遇到问题时&#xff…

【PHP代码审计】PHP常见配置解析

&#x1f31d;博客主页&#xff1a;菜鸟小羊 &#x1f496;专栏&#xff1a;Linux探索之旅 | 网络安全的神秘世界 | 专接本 | 每天学会一个渗透测试工具 php.ini配置文件 php函数禁用 disable_functions该选项可以设置哪些php函数是禁止使用的&#xff0c;重启生效&#xff0…

Vue自定义指令以及项目中封装过的自定义指令

自定义指令 Vue 自定义指令是 Vue.js 框架中一个非常强大的功能&#xff0c;它允许你注册一些全局或局部的自定义 DOM 操作指令&#xff0c;以便在模板中复用。自定义指令通过 Vue.directive() 方法进行全局注册&#xff0c;或者在组件的 directives 选项中局部注册。 自定义…

【数据结构】快速排序详解(递归版本)

目录 0. 前言 1. 快速排序的基本思想 2. 快速排序的不同版本的实现 2.1 hoare版本 2.1.1 单趟排序动图演示 2.1.2 递归展开多个子区间进行单趟排序 2.1.3 代码的具体实现 2.1.3.1 霍尔法单趟排序代码 2.3.1.2 霍尔法递归代码 2.2 挖坑法 2.2.1 单趟排序方法动图演示…

力扣刷题--73. 矩阵置零【中等】

题目描述 给定一个 m x n 的矩阵&#xff0c;如果一个元素为 0 &#xff0c;则将其所在行和列的所有元素都设为 0 。请使用 原地 算法。 算法分析 标签&#xff1a;标记数组 额外定义一个标记二维数组&#xff0c;用于存储matrix0存储的位置在哪里&#xff0c;如果是matrix…