• 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管脚输入电平状态

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

    1. # Copyright (c) 2023 Beijing HuaQing YuanJian Education Technology Co., Ltd
    2. # Licensed under the Apache License, Version 2.0 (the "License");
    3. # you may not use this file except in compliance with the License.
    4. # You may obtain a copy of the License at
    5. #
    6. # http://www.apache.org/licenses/LICENSE-2.0
    7. #
    8. # Unless required by applicable law or agreed to in writing, software
    9. # distributed under the License is distributed on an "AS IS" BASIS,
    10. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11. # See the License for the specific language governing permissions and
    12. # limitations under the License.
    13. import("//build/lite/config/component/lite_component.gni")
    14. lite_component("demo") {
    15. features = [
    16. #"base_00_helloworld:base_helloworld_example",
    17. #"base_01_led:base_led_example",
    18. "base_02_loopkey:base_loopkey_example",
    19. ]
    20. }

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

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

    1. # Copyright (c) 2023 Beijing HuaQing YuanJian Education Technology Co., Ltd
    2. # Licensed under the Apache License, Version 2.0 (the "License");
    3. # you may not use this file except in compliance with the License.
    4. # You may obtain a copy of the License at
    5. #
    6. # http://www.apache.org/licenses/LICENSE-2.0
    7. #
    8. # Unless required by applicable law or agreed to in writing, software
    9. # distributed under the License is distributed on an "AS IS" BASIS,
    10. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11. # See the License for the specific language governing permissions and
    12. # limitations under the License.
    13. static_library("base_loopkey_example") {
    14. sources = [
    15. "base_loopkey_example.c",
    16. ]
    17. include_dirs = [
    18. "//utils/native/lite/include",
    19. "//kernel/liteos_m/kal/cmsis",
    20. "//base/iot_hardware/peripheral/interfaces/kits",
    21. "//vendor/hqyj/fs_hi3861/common/bsp/include"
    22. ]
    23. }
    1. /*
    2. * Copyright (c) 2023 Beijing HuaQing YuanJian Education Technology Co., Ltd
    3. * Licensed under the Apache License, Version 2.0 (the "License");
    4. * you may not use this file except in compliance with the License.
    5. * You may obtain a copy of the License at
    6. *
    7. * http://www.apache.org/licenses/LICENSE-2.0
    8. *
    9. * Unless required by applicable law or agreed to in writing, software
    10. * distributed under the License is distributed on an "AS IS" BASIS,
    11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12. * See the License for the specific language governing permissions and
    13. * limitations under the License.
    14. */
    15. #include <stdio.h>
    16. #include <unistd.h>
    17. #include "cmsis_os2.h"
    18. #include "hi_gpio.h"
    19. #include "hi_io.h"
    20. #include "ohos_init.h"
    21. #define KEY1 HI_IO_NAME_GPIO_6 // 对应按键key1
    22. #define KEY2 HI_IO_NAME_GPIO_5 // 对应按键key2
    23. osThreadId_t Task1_ID = 0; // 任务1 ID
    24. hi_gpio_value val1, val1_last, val2, val2_last; // GPIO的状态值
    25. #define TASK_STACK_SIZE 1024
    26. #define TASK_DELAY_TIME (200 * 1000)
    27. /**
    28. * @description: 任务1
    29. * @param {*}
    30. * @return {*}
    31. */
    32. void Task1(void)
    33. {
    34. printf("enter Task 1.......\r\n");
    35. hi_gpio_init(); // GPIO初始化
    36. hi_io_set_pull(KEY1, HI_IO_PULL_UP); // 设置GPIO上拉
    37. hi_io_set_func(KEY1, HI_IO_FUNC_GPIO_6_GPIO); // 设置IO16为GPIO功能
    38. hi_gpio_set_dir(KEY1, HI_GPIO_DIR_IN); // 设置GPIO为输入模式
    39. hi_io_set_pull(KEY2, HI_IO_PULL_UP); // 设置GPIO上拉
    40. hi_io_set_func(KEY2, HI_IO_FUNC_GPIO_5_GPIO); // 设置IO15为GPIO功能
    41. hi_gpio_set_dir(KEY2, HI_GPIO_DIR_IN); // 设置GPIO为输入模式
    42. while (1) {
    43. hi_gpio_get_input_val(KEY1, &val1); // 获取GPIO引脚的状态
    44. if (val1 != val1_last) {
    45. // 当GPIO状态改变的时候, 打印输出
    46. printf("key1Value: %s\r\n", val1 ? "HI_GPIO_VALUE_1" : "HI_GPIO_VALUE_0");
    47. val1_last = val1;
    48. }
    49. hi_gpio_get_input_val(KEY2, &val2); // 获取GPIO引脚的状态
    50. if (val2 != val2_last) {
    51. // 当GPIO状态改变的时候, 打印输出
    52. printf("key2Value: %s\r\n", val2 ? "HI_GPIO_VALUE_1" : "HI_GPIO_VALUE_0");
    53. val2_last = val2;
    54. }
    55. // 200ms一检测
    56. usleep(TASK_DELAY_TIME); // 200ms sleep
    57. }
    58. }
    59. /**
    60. * @description: 初始化并创建任务
    61. * @param {*}
    62. * @return {*}
    63. */
    64. static void base_key_demo(void)
    65. {
    66. printf("[demo] Enter base_key_demo()!\r\n");
    67. osThreadAttr_t taskOptions;
    68. taskOptions.name = "Task1"; // 任务的名字
    69. taskOptions.attr_bits = 0; // 属性位
    70. taskOptions.cb_mem = NULL; // 堆空间地址
    71. taskOptions.cb_size = 0; // 堆空间大小
    72. taskOptions.stack_mem = NULL; // 栈空间地址
    73. taskOptions.stack_size = TASK_STACK_SIZE; // 栈空间大小 单位:字节
    74. taskOptions.priority = osPriorityNormal; // 任务的优先级:wq
    75. Task1_ID = osThreadNew((osThreadFunc_t)Task1, NULL, &taskOptions); // 创建任务1
    76. if (Task1_ID != NULL) {
    77. printf("ID = %d, Create Task1_ID is OK!\r\n", Task1_ID);
    78. }
    79. }
    80. SYS_RUN(base_key_demo);

    目录结构

    1. │ config.json
    2. ├─common
    3. │ └─bsp
    4. │ ├─include
    5. │ └─src
    6. ├─demo
    7. │ │ BUILD.gn
    8. │ │
    9. │ ├─base_00_helloworld
    10. │ │ base_helloworld_example.c
    11. │ │ BUILD.gn
    12. │ │
    13. │ ├─base_01_led
    14. │ │ base_led_example.c
    15. │ │ BUILD.gn
    16. │ │
    17. │ └─base_02_loopkey
    18. │ base_loopkey_example.c
    19. │ BUILD.gn
    20. └─doc
    21. │ HarmonyOS开发板实验指导书 v2.1.pdf
    22. │ 华清远见 FS_Hi3861开发指导.md
    23. │ 华清远见 FS_Hi3861新手入门手册.md
    24. ├─board
    25. │ FS-Hi3861-V4.2.pdf
    26. │ FS-Hi3861QDB-V3.2.pdf
    27. │ hi-12f_kit_v1.1.0A7E6BCB9%A6-20211025.pdf
    28. │ hi-12f_v1.1.2-A7E6BCB9%A6-20211202.pdf
    29. │ nodemcu-hi-07s_12f-kit_v1.1-20210913.pdf
    30. │ RTplay2.01_2024-06-14.pdf
    31. └─figures

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

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

  • 相关阅读:
    金融信息化研究所与YashanDB等单位启动金融多主数据库应用行动计划
    自已定义一个Java异常——子定义异常,和异常遇到的面试题。
    跳跃游戏(动态规划)
    【Spring Boot】Spring Bootd的介绍、项目的创建
    简述TCP三次握手,四次挥手
    零基础速通AI建筑效果图!七个步骤,20个万能模板让你成功逆袭
    string的接口测试与使用
    批量将所有文件的磁盘路径名称提取到 txt 记事本文件中
    Mysql —— 多行/聚合/分组函数 打字练习
    项目组合管理在金融科技领域的应用实践︱中银金融PMO负责人张伟
  • 原文地址:https://blog.csdn.net/andylauren/article/details/139771090