wordpress建站linuxseo手機關鍵詞網(wǎng)址
目錄
一、頁面切換內(nèi)容詳解
1.邏輯解釋
2.代碼詳解
code.c(內(nèi)含詳細講解)
code.h
main.c
3.效果圖片展示
?編輯
二、頁面選項高亮內(nèi)容詳解
1.邏輯解釋
2.讀入數(shù)據(jù)
FIRST.第一種高亮類型
code.c(內(nèi)含代碼詳解)
code.h
main.c
SECOND.第二種高亮類型
3.效果展示
開源代碼
一、頁面切換內(nèi)容詳解
1.邏輯解釋
- 首先通過?
LCD_Clear(White)
?函數(shù)將 LCD 屏幕清除為白色。lcd_page
?的值增加 1。- 然后進行判斷,如果?
lcd_page
?達到 3,則將其重置為 0,實現(xiàn)頁面的循環(huán)切換。當檢測到按鍵一被按下(
Key_down==1
)時:
count
?的值增加 1。這樣,通過按鍵二可以切換頁面,通過按鍵一可以使?
count
?值增加。舉例來說:
- 初始時,
lcd_page
?為 0,count
?為 0。- 按下按鍵二一次,
lcd_page
?變?yōu)?1。- 再按下按鍵二一次,
lcd_page
?變?yōu)?2。- 接著按下按鍵二一次,
lcd_page
?變?yōu)?0,完成一次頁面循環(huán)。- 同時在這個過程中,每按下一次按鍵一,
count
?值就增加 1。
2.代碼詳解
code.c(內(nèi)含詳細講解)
#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);//清屏操作,不然會有殘影l(fā)cd_page++;//頁面加一if(lcd_page==3) lcd_page=0;//以為沒有l(wèi)cd_page==3,所以回到0}if(Key_down==1)//按鍵一數(shù)值加一{count++;//如果變量 Key_down 的值等于 1,則將變量 count 的值增加 1。
//這是一個簡單的條件判斷和操作,用于根據(jù)特定條件來改變某個變量的值。}}char buf[21];
void Lcd_proc(void)
{if(lcd_page==0){LCD_DisplayStringLine(Line1,(uint8_t*)" jinke! ");//這行代碼是將字符串 " jinke! " 強制轉(zhuǎn)換為指向 uint8_t 類型數(shù)據(jù)的指針。}if(lcd_page==1){sprintf(buf," count:%d ",count);//"count:%d" 是格式字符串,其中 %d 表示要插入一個整數(shù)變量 count 。//執(zhí)行完這行代碼后,buf 中就會存儲一個包含 "count:" 和 count 的具體數(shù)值的字符串。LCD_DisplayStringLine(Line4,(uint8_t*)buf);//“LCD_DisplayStringLine(Line4, (uint8_t*)buf)” 表示在特定的行(Line4)上顯示字符串,//其中字符串的內(nèi)容來自于指向 uint8_t 類型的指針 buf 所指向的緩沖區(qū)。}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.效果圖片展示
二、頁面選項高亮內(nèi)容詳解
1.邏輯解釋
line_flag定義變量存儲
- 當頁面為 1 時,根據(jù)?
line_flag
?的不同設置文本顏色,并顯示與?count1
、count2
、count3
?相關的字符串。
2.讀入數(shù)據(jù)
FIRST.第一種高亮類型
code.c(內(nèi)含代碼詳解)
#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);//需要加此函數(shù)lcd_page++;//頁面加一if(lcd_page==3) lcd_page=0;//以為沒有l(wèi)cd_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! " 強制轉(zhuǎn)換為指向 uint8_t 類型數(shù)據(jù)的指針。}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 表示要插入一個整數(shù)變量 count 。//執(zhí)行完這行代碼后,buf 中就會存儲一個包含 "count:" 和 count 的具體數(shù)值的字符串。LCD_DisplayStringLine(Line3,(uint8_t*)buf);//“LCD_DisplayStringLine(Line4, (uint8_t*)buf)” 表示在特定的行(Line4)上顯示字符串,//其中字符串的內(nèi)容來自于指向 uint8_t 類型的指針 buf 所指向的緩沖區(qū)。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加數(shù)
key4減數(shù)
演示
開源代碼
通過網(wǎng)盤分享的文件:新建文件夾.zip
鏈接: https://pan.baidu.com/s/159nyhzLd4xkYirNAKgf00w?pwd=0820 提取碼: 0820