当前位置: 首页 > article >正文

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

目录

一、页面切换内容详解

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 header

void 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 header

void 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.kler.cn/a/307304.html

相关文章:

  • 【C++】string类(附题)
  • 微信小程序的主体文件和页面文件介绍
  • 【计算机网络】TCP网络程序
  • UVC 输出视频格式修改和windows下数据分析
  • 论文阅读《BEVFormer v2》
  • 微信小程序=》基础=》常见问题=》性能总结
  • 【AIGC】CFG:基于扩散模型分类器差异引导
  • JavaScript 函数 function
  • 用 nextjs 创建 Node+React Demo
  • WebGL入门(048):OES_draw_buffers_indexed 简介、使用方法、示例代码
  • Python---爬虫
  • Leetcode-轮转数组
  • 复现OpenVLA:开源的视觉-语言-动作模型及原理详解
  • 【Go开发】Go语言结构体,与java类不一样的定义方式
  • 推荐|基于springBoot智能推荐的卫生健康系统设计与实现(源码+论文+数据库)
  • 【附源码】用Python开发一个音乐下载工具,并打包EXE文件,所有音乐都能搜索下载!
  • el-table 的单元格 + 图表 + 排序
  • 动手学深度学习(pytorch土堆)-03常见的Transforms
  • 图论篇--代码随想录算法训练营第五十六天打卡| 108. 冗余连接,109. 冗余连接II
  • 【SQL】百题计划:SQL排序Order by的使用。
  • Flutter Error: Type ‘UnmodifiableUint8ListView‘ not found
  • 刷题DAY36
  • 初中生物--5.单细胞生物
  • VuePress搭建文档网站/个人博客(详细配置)主题配置-导航栏配置
  • 【开源免费】基于SpringBoot+Vue.JS企业客户管理系统(JAVA毕业设计)
  • Linux命令:文本处理工具sed详解