Hi3861 OpenHarmony嵌入式应用入门--中断按键

本篇讲解gpio的中断使用方式。

硬件原理图如下,与上一篇一样的电路

GPIO API

|------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------|
| API名称 | 说明 |
| hi_u32 hi_gpio_init(hi_void); | GPIO模块初始化 |
| hi_u32 hi_io_set_pull(hi_io_name id, hi_io_pull val); | 设置某个IO上下拉功能。 |
| hi_u32 hi_gpio_set_dir(hi_gpio_idx id, hi_gpio_dir dir); | 设置GPIO引脚方向,id参数用于指定引脚,dir参数用于指定输入或输出 |
| hi_u32 hi_gpio_get_input_val(hi_gpio_idx id, hi_gpio_value* val); | 获取某个IO管脚输入电平状态 |
| hi_u32 hi_gpio_register_isr_function(hi_gpio_idx id, hi_gpio_int_type int_type, hi_gpio_int_polarity int_polarity, gpio_isr_callback func, hi_void *arg); | 使能某个GPIO的中断功能 |

修改D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\BUILD.gn文件

复制代码
# Copyright (c) 2023 Beijing HuaQing YuanJian Education Technology Co., Ltd
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#    http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License. 

import("//build/lite/config/component/lite_component.gni")

lite_component("demo") {
  features = [
    #"base_00_helloworld:base_helloworld_example",
    #"base_01_led:base_led_example",
    #"base_02_loopkey:base_loopkey_example",
    "base_03_irqkey:base_irqkey_example",
  ]
}

创建D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\base_03_irqkey文件夹

文件夹中创建D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\base_03_irqkey\base_irqkey_example.c文件D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\base_03_irqkey\BUILD.gn文件

复制代码
# Copyright (c) 2023 Beijing HuaQing YuanJian Education Technology Co., Ltd
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License. 

static_library("base_irqkey_example") {
    sources = [
        "base_irqkey_example.c",
    ]

    include_dirs = [
        "//utils/native/lite/include",
        "//kernel/liteos_m/kal/cmsis",
        "//base/iot_hardware/peripheral/interfaces/kits",
        "//vendor/hqyj/fs_hi3861/common/bsp/include"
    ]
}

/*
 * Copyright (c) 2023 Beijing HuaQing YuanJian Education Technology Co., Ltd
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <unistd.h>

#include "cmsis_os2.h"
#include "hi_gpio.h"
#include "hi_io.h"
#include "ohos_init.h"

#define KEY1 HI_IO_NAME_GPIO_6  // 对应按键key1
#define KEY2 HI_IO_NAME_GPIO_5  // 对应按键key2

osThreadId_t Task1_ID = 0;    //  任务1 ID
hi_gpio_value val, val_last;  // GPIO的状态值
#define TASK_STACK_SIZE 1024
#define TASK_DELAY_TIME (200 * 1000)

/**
 * @brief 按键中断回调函数
 * @note   当按键按下的时候才会触发
 * @param  *arg:
 * @retval None
 */
hi_void key1_callback(void)
{
    printf("key1 down...\r\n");
    hi_gpio_get_input_val(KEY1, &val);  // 获取GPIO引脚的状态
    printf("key1_callback: %s\r\n", val ? "HI_GPIO_VALUE_1" : "HI_GPIO_VALUE_0");
}
hi_void key2_callback(void)
{
    printf("key2 down...\r\n");
    hi_gpio_get_input_val(KEY2, &val);  // 获取GPIO引脚的状态
    printf("key2_callback: %s\r\n", val ? "HI_GPIO_VALUE_1" : "HI_GPIO_VALUE_0");
}
/**
 * @description: 任务1
 * @param {*}
 * @return {*}
 */
void Task1(void)
{
    printf("enter Task 1.......\r\n");
    hi_gpio_init();                                             // GPIO初始化
    hi_io_set_pull(KEY1, HI_IO_PULL_UP);                        // 设置GPIO上拉
    hi_io_set_func(KEY1, HI_IO_FUNC_GPIO_6_GPIO);               // 设置IO14为GPIO功能
    hi_gpio_set_dir(KEY1, HI_GPIO_DIR_IN);                      // 设置GPIO为输入模式
    hi_gpio_register_isr_function(KEY1,                         // KEY按键引脚
                                  HI_INT_TYPE_EDGE,             // 下降沿检测
                                  HI_GPIO_EDGE_FALL_LEVEL_LOW,  // 低电平时触发
                                  &key1_callback,               // 触发后调用的回调函数
                                  NULL);                        // 回调函数的传参值
    hi_io_set_pull(KEY2, HI_IO_PULL_UP);                        // 设置GPIO上拉
    hi_io_set_func(KEY2, HI_IO_FUNC_GPIO_6_GPIO);               // 设置IO14为GPIO功能
    hi_gpio_set_dir(KEY2, HI_GPIO_DIR_IN);                      // 设置GPIO为输入模式
    hi_gpio_register_isr_function(KEY2,                         // KEY按键引脚
                                  HI_INT_TYPE_EDGE,             // 下降沿检测
                                  HI_GPIO_EDGE_FALL_LEVEL_LOW,  // 低电平时触发
                                  &key2_callback,               // 触发后调用的回调函数
                                  NULL);                        // 回调函数的传参值

    while (1) {
        // 200ms一检测
        usleep(TASK_DELAY_TIME);  // 200ms sleep
    }
}

/**
 * @description: 初始化并创建任务
 * @param {*}
 * @return {*}
 */
static void base_key_demo(void)
{
    printf("[demo] Enter base_key_demo()!\r\n");

    osThreadAttr_t taskOptions;
    taskOptions.name = "Task1";                // 任务的名字
    taskOptions.attr_bits = 0;                 // 属性位
    taskOptions.cb_mem = NULL;                 // 堆空间地址
    taskOptions.cb_size = 0;                   // 堆空间大小
    taskOptions.stack_mem = NULL;              // 栈空间地址
    taskOptions.stack_size = TASK_STACK_SIZE;  // 栈空间大小 单位:字节
    taskOptions.priority = osPriorityNormal;   // 任务的优先级:wq

    Task1_ID = osThreadNew((osThreadFunc_t)Task1, NULL, &taskOptions);  // 创建任务1
    if (Task1_ID != NULL) {
        printf("ID = %d, Create Task1_ID is OK!\r\n", Task1_ID);
    }
}
SYS_RUN(base_key_demo);

目录结构

复制代码
│  config.json
│
├─common
│  └─bsp
│      ├─include
│      └─src
├─demo
│  │  BUILD.gn
│  │
│  ├─base_00_helloworld
│  │      base_helloworld_example.c
│  │      BUILD.gn
│  │
│  ├─base_01_led
│  │      base_led_example.c
│  │      BUILD.gn
│  │
│  ├─base_02_loopkey
│  │      base_loopkey_example.c
│  │      BUILD.gn
│  │
│  └─base_03_irqkey
│          base_irqkey_example.c
│          BUILD.gn
│
└─doc
    │  HarmonyOS开发板实验指导书 v2.1.pdf
    │  华清远见 FS_Hi3861开发指导.md
    │  华清远见 FS_Hi3861新手入门手册.md
    │
    ├─board
    │      FS-Hi3861-V4.2.pdf
    │      FS-Hi3861QDB-V3.2.pdf
    │      hi-12f_kit_v1.1.0A7E6BCB9%A6-20211025.pdf
    │      hi-12f_v1.1.2-A7E6BCB9%A6-20211202.pdf
    │      nodemcu-hi-07s_12f-kit_v1.1-20210913.pdf
    │      RTplay2.01_2024-06-14.pdf
    │
    └─figures

使用build,编译成功后,使用upload进行烧录。

运行效果如下,按下按键会有对应按键状态输出。

相关推荐
振南的单片机世界1 小时前
看门狗硬核拆解:1.6秒不喂狗,硬件强制“掀桌子”
arm开发·stm32·单片机·嵌入式硬件
嵌入式阿蔡1 小时前
Linux_01:交叉编译与开发环境实战——从单片机跨到 Linux 的第一道坎
linux·运维·网络·单片机·嵌入式硬件·嵌入式实时数据库
NYFEA3 小时前
徕飞 FCA系列全面上新,一站式I/O扩展国产替代方案
单片机·嵌入式硬件
国科安芯4 小时前
ASL622S:把“信号失真“挡在门外的航天级运放
网络·单片机·嵌入式硬件·架构·状态模式·低轨卫星星座
XiHongShi20164 小时前
STM32H7 FLASH和SRAM标准例程
stm32·单片机·嵌入式硬件
恒锐丰科技韩生21 小时前
SS8844T|8.2‑40V 四通道独立半桥驱动 ETSSOP28 带散热焊盘 工业自动化机器人专用
嵌入式硬件·硬件工程
Hello_Damon_Nikola1 天前
INMP441从入门到精通
单片机·嵌入式硬件·物联网
恒锐丰小瑞1 天前
率能SS6810H 32V/1A/双通道H桥驱动芯片,PWM电流控制与混合衰减模式,用于POS打印机/安防相机/机器人
嵌入式硬件·数码相机·机器人·硬件工程
恒锐丰科技林技术员1 天前
SS8812T 双通道 H 桥电机驱动芯片应用解析
经验分享·嵌入式硬件·硬件工程
ye150127774551 天前
12V转3V-5V2A电位器无极调光调速WT7027
stm32·单片机·嵌入式硬件