问题描述:用按键控制屏幕上的数字增加,同一种写法,按键二、三均能实现,但按键一不按下数字自增
代码:
/////////////////////////////////////////*********************** 重点一:按键****************////////////////////
u8 key_Process(void)
{
if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_0) == 0) //PB0=0代表按键被按下
{
HAL_Delay(10); //一定要写防抖
if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_0) == 0)
{
while(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_0) == 0) //等待抬起
return 1;
}
}
if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_1) == 0) //PB1=0代表按键被按下
{
HAL_Delay(10); //一定要写防抖
if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_1 )== 0)
{
while(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_1) == 0) //等待抬起
return 2;
}
}
if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_2) == 0) //PB2=0代表按键被按下
{
HAL_Delay(10); //一定要写防抖
if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_2 )== 0)
{
while(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_2) == 0) //等待抬起
return 3;
}
}
if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == 0) //PA0=0代表按键被按下
{
HAL_Delay(10); //一定要写防抖
if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == 0)
{
while(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == 0) //等待抬起
return 4;
}
}
}
/////////////////////////////////////////*********************** 重点一:按键****************////////////////////
/* 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();
MX_TIM2_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
LCD_Init();
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
LCD_Clear(Black);
LCD_SetBackColor(Black);
LCD_SetTextColor(White);
int key_value = 0;
int page_index = 1; //定义页面索引,等于一时为页面1,等于二时为页面2
int B1 = -1;
int B2 = -1;
int B3 = -1;
char buf[32] = { 0 };
while (1)
{
key_value = key_Process(); //key_Process传的值是按键
if(page_index == 1) //假如当前页面为一页面
{
LCD_DisplayStringLine(Line1 ,(unsigned char *)" PSD ");
if(B1 == -1)
{
LCD_DisplayStringLine(Line4 ,(unsigned char *)" B1:@ ");
}
else
{
sprintf(buf, " B1:%d ", B1);
LCD_DisplayStringLine(Line4 ,(unsigned char *)buf);
}
if(B2 == -1)
{
LCD_DisplayStringLine(Line5 ,(unsigned char *)" B2:@ ");
}
else
{
sprintf(buf, " B2:%d ", B2);
LCD_DisplayStringLine(Line5 ,(unsigned char *)buf);
}
if(B3 == -1)
{
LCD_DisplayStringLine(Line6 ,(unsigned char *)" B3:@ ");
}
else
{
sprintf(buf, " B3:%d ", B3);
LCD_DisplayStringLine(Line6 ,(unsigned char *)buf);
}
////////////////////////////////////////密码逻辑//////////////////////////////////////////////
if(key_value == 1) //按键二被按下
{
B1++;
if(B1 == 10)
B1 = 0;
HAL_Delay(100);
}
if(key_value == 2) //按键二被按下
{
B2++;
if(B2 == 10)
B2 = 0;
HAL_Delay(100);
}
if(key_value == 3) //按键三被按下
{
B3++;
if(B3 == 10)
B3 = 0;
}
if(key_value == 4) //按键四被按下
//按键四被按下时,表示密码验证,如果密码正确,贼跳转页面
{
page_index = 2; //在这里切换成页面二
}
}
else //显示第二个页面
{
LCD_DisplayStringLine(Line1 ,(unsigned char *)" STA ");
LCD_DisplayStringLine(Line4 ,(unsigned char *)" F:2000Hz ");
LCD_DisplayStringLine(Line5 ,(unsigned char *)" D:10% ");
LCD_DisplayStringLine(Line6 ,(unsigned char *)" ");
}