|
為什么我的按鍵翻頁的程序他好像卡死了,翻不了頁?
#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "Key_TurnPage.h"
#include "OLED.h"
#include <stdio.h>
uint32_t HAL_Init(void); //定義用于接收按鍵鍵碼的變量
// 系統(tǒng)初始化
void System_Init(void) {
OLED_Init(); // 初始化OLED
Key_Init(); // 初始化按鍵
OLED_Clear(); // 清屏
}
const char* pageContent[] = {
"Page 1: Main",
"Page 2: Temp",
"Page 3: Humi"
};
// 顯示當(dāng)前頁
void ShowCurrentPage(void) {
OLED_Clear();
OLED_ShowString(0, 0, (char*)pageContent[currentPage]);
OLED_ShowString(0, 0, (char*)pageContent[currentPage]);
// 添加頁碼顯示(例如:1/10)
char pageStr[16];
sprintf(pageStr, "Page:%d/%d", currentPage+1, totalPages);
OLED_ShowString(0, 2, pageStr);
}
//主程序
int main(void)
{
/*模塊初始化*/
System_Init();
ShowCurrentPage(); // 顯示初始頁
while (1)
{
PageAction action = Key_Scan();
switch (action)
{
case PAGE_UP: // 執(zhí)行向上翻頁操作
printf("Page Up: %d\n", currentPage);
break;
case PAGE_DOWN: // 執(zhí)行向下翻頁操作
printf("Page Down: %d\n", currentPage);
break;
case PAGE_NONE: // 無按鍵動作
break;
}
// 其他任務(wù)...
// 按鍵掃描(假設(shè)在按鍵驅(qū)動中實(shí)現(xiàn))
Key_Scan();
// 處理向上翻頁
if (keyUpState == KEY_STATE_PRESSED) {
keyUpState = KEY_STATE_RELEASED; // 清除按鍵狀態(tài)
if (currentPage > 0) {
currentPage--;
ShowCurrentPage();
}
}
// 處理向下翻頁
if (keyDownState == KEY_STATE_PRESSED) {
keyDownState = KEY_STATE_RELEASED; // 清除按鍵狀態(tài)
if (currentPage < totalPages - 1) {
currentPage++;
ShowCurrentPage();
}
}
// 處理按鍵連發(fā)(長按快速翻頁)
if (keyUpState == KEY_STATE_REPEAT) {
if (currentPage > 0) {
currentPage--;
ShowCurrentPage();
Delay_ms(100); // 連發(fā)速度控制
}
}
if (keyDownState == KEY_STATE_REPEAT) {
if (currentPage < totalPages - 1) {
currentPage++;
ShowCurrentPage();
Delay_ms(100); // 連發(fā)速度控制
}
}
Delay_ms(10); // 主循環(huán)延時(shí)
}
}
#include "stm32f10x.h"
// 按鍵定義
#define KEY_UP_PIN GPIO_Pin_4
#define KEY_UP_PORT GPIOA
#define KEY_DOWN_PIN GPIO_Pin_6
#define KEY_DOWN_PORT GPIOA
// 按鍵狀態(tài)
typedef enum {
KEY_STATE_RELEASED, //按鍵釋放/未按下狀態(tài)
KEY_STATE_PRESS_DETECTED, //檢測到按鍵按下(初始按下狀態(tài))
KEY_STATE_PRESSED, //按鍵持續(xù)按下狀態(tài)
KEY_STATE_REPEAT //按鍵保持觸發(fā)連發(fā)狀態(tài)
} KeyState;
// 翻頁控制
typedef enum {
PAGE_UP, //向上翻頁
PAGE_DOWN, //向下翻頁
PAGE_NONE //無翻頁動作
} PageAction;
// 全局變量
static KeyState keyUpState = KEY_STATE_RELEASED; //向上按鍵的當(dāng)前狀態(tài)
static KeyState keyDownState = KEY_STATE_RELEASED; //向下按鍵的當(dāng)前狀態(tài)
static uint32_t keyPressTimer = 0; //按鍵按下計(jì)時(shí)器(用于長按檢測)
static uint8_t currentPage = 0; //當(dāng)前顯示頁面的索引(從0開始)
static uint8_t totalPages = 10; // 假設(shè)總共有10頁
// 按鍵初始化
void Key_Init(void) {
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = KEY_UP_PIN | KEY_DOWN_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; // 上拉輸入
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
volatile uint32_t sysTickCount = 0;
uint32_t GetTick(void)
{
return sysTickCount; // 返回當(dāng)前滴答值
}
// 按鍵掃描函數(shù)(非阻塞式)
PageAction Key_Scan(void) {
static uint32_t lastScanTime = 0;
//uint32_t currentTime = HAL_GetTick();
uint32_t currentTime = GetTick();
// 每10ms掃描一次按鍵
if (currentTime - lastScanTime < 10) {
return PAGE_NONE;
}
lastScanTime = currentTime;
// 檢測KEY_UP
if (GPIO_ReadInputDataBit(KEY_UP_PORT, KEY_UP_PIN) == Bit_RESET) { // 按鍵按下
switch (keyUpState) {
case KEY_STATE_RELEASED:
keyUpState = KEY_STATE_PRESS_DETECTED;
keyPressTimer = currentTime;
break;
case KEY_STATE_PRESS_DETECTED:
if (currentTime - keyPressTimer > 20) { // 消抖
keyUpState = KEY_STATE_PRESSED;
currentPage = (currentPage == 0) ? totalPages - 1 : currentPage - 1;
return PAGE_UP;
}
break;
case KEY_STATE_PRESSED:
if (currentTime - keyPressTimer > 500) { // 長按500ms后開始自動重復(fù)
keyUpState = KEY_STATE_REPEAT;
keyPressTimer = currentTime;
}
break;
case KEY_STATE_REPEAT:
if (currentTime - keyPressTimer > 100) { // 每100ms重復(fù)一次
keyPressTimer = currentTime;
currentPage = (currentPage == 0) ? totalPages - 1 : currentPage - 1;
return PAGE_UP;
}
break;
}
} else { // 按鍵釋放
keyUpState = KEY_STATE_RELEASED;
}
// 檢測KEY_DOWN
if (GPIO_ReadInputDataBit(KEY_DOWN_PORT, KEY_DOWN_PIN) == Bit_RESET) {
switch (keyDownState) {
case KEY_STATE_RELEASED:
keyDownState = KEY_STATE_PRESS_DETECTED;
keyPressTimer = currentTime;
break;
case KEY_STATE_PRESS_DETECTED:
if (currentTime - keyPressTimer > 20) { // 消抖
keyDownState = KEY_STATE_PRESSED;
currentPage = (currentPage + 1) % totalPages;
return PAGE_DOWN;
}
break;
case KEY_STATE_PRESSED:
if (currentTime - keyPressTimer > 500) { // 長按500ms后開始自動重復(fù)
keyDownState = KEY_STATE_REPEAT;
keyPressTimer = currentTime;
}
break;
case KEY_STATE_REPEAT:
if (currentTime - keyPressTimer > 100) { // 每100ms重復(fù)一次
keyPressTimer = currentTime;
currentPage = (currentPage + 1) % totalPages;
return PAGE_DOWN;
}
break;
}
} else {
keyDownState = KEY_STATE_RELEASED;
}
return PAGE_NONE;
}
|
|