目录
一、页面切换内容详解
1.逻辑解释
- 首先通过
LCD_Clear(White)
函数将 LCD 屏幕清除为白色。lcd_page
的值增加 1。- 然后进行判断,如果
lcd_page
达到 3,则将其重置为 0,实现页面的循环切换。当检测到按键一被按下(
Key_down==1
)时:
count
的值增加 1。这样,通过按键二可以切换页面,通过按键一可以使
count
值增加。举例来说:
- 初始时,
lcd_page
为 0,count
为 0。- 按下按键二一次,
lcd_page
变为 1。- 再按下按键二一次,
lcd_page
变为 2。- 接着按下按键二一次,
lcd_page
变为 0,完成一次页面循环。- 同时在这个过程中,每按下一次按键一,
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
的不同设置文本颜色,并显示与count1
、count2
、count3
相关的字符串。
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