【iCore4 双核心板_ARM】例程二:读取ARM按键状态

时间:2022-07-19 16:12:35

实验原理:

  按键的一端与STM32 PB9相连,另外一端接地,且PB9外接一个1K电阻大小的上拉电阻,

初始化时把PB9设置成输入模式,当按键弹起时,PB9由于上拉电阻的作用呈高电平(3.3V);

当按键按下时,PB9直接被按键短接到GND,呈低电平,因此判断PB9的电平变化可得到按键状态。

实验现象:

  该按键每按下一次,LED改变一次状态。

源代码下载链接:

链接:http://pan.baidu.com/s/1mhPhFfy 密码:rrgr

核心源码:

int main(void)
{ /* USER CODE BEGIN 1 */
static int led_work_status = ;//三色LED工作状态
static int key_status = KEY_UP;//按键松开状态 /* 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 */
LED_RED_ON; //初始化LED状态
/* USER CODE END 2 */ /* Infinite loop */
/* USER CODE BEGIN WHILE */
//按键每按下一次切换一次LED状态
while ()
{
/* USER CODE END WHILE */ /* USER CODE BEGIN 3 */
if(ARM_KEY_STATE == KEY_UP)key_status = KEY_UP;
if(key_status == KEY_UP){
if(ARM_KEY_STATE == KEY_DOWN){
HAL_Delay(); //按键消抖
if(ARM_KEY_STATE == KEY_DOWN){
key_status = KEY_DOWN;
led_work_status += ;
if(led_work_status > )led_work_status = ;
//²Ù×÷LED
switch(led_work_status){
case :
LED_RED_ON;
LED_GREEN_OFF;
LED_BLUE_OFF;
break;
case :
LED_RED_OFF;
LED_GREEN_ON;
LED_BLUE_OFF;
break;
case :
LED_RED_OFF;
LED_GREEN_OFF;
LED_BLUE_ON;
break;
default:
break;
}
}
}
}
}
/* USER CODE END 3 */ }

iCore4链接:

【iCore4 双核心板_ARM】例程二:读取ARM按键状态