• Hi3861 OpenHarmony嵌入式应用入门--点灯


    本篇实现对gpio的控制,通过控制输出进行gpio的点灯操作。

    硬件

    我们来操作IO2,控制绿色的灯。

    软件

    GPIO API

    API名称

    说明

    hi_u32 hi_gpio_deinit(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_set_ouput_val(hi_gpio_idx id, hi_gpio_value val);

    设置GPIO引脚的输出状态,id参数用于指定引脚,val参数用于指定高电平或低电平

    hi_u32 hi_io_set_func(hi_io_name id, hi_u8 val);

    设置引脚功能,id参数用于指定引脚,val用于指定引脚功能

    hi_u32 hi_gpio_deinit(hi_void);

    解除GPIO模块初始化

    修改D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\BUILD.gn文件,让工程编译led代码

    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_01_led:base_led_example",
    17. ]
    18. }

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

    文件夹中创建D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\base_01_led\base_led_example.c文件D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\base_01_led\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_led_example") {
    14. sources = [
    15. "base_led_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 LED HI_IO_NAME_GPIO_2 // 开发板三色灯其中一个引脚
    22. osThreadId_t Task1_ID = 0; // 任务1 ID
    23. hi_gpio_value val, val_last; // GPIO的状态值
    24. #define TASK_STACK_SIZE 1024
    25. #define TASK_DELAY_TIME (1000 * 1000)
    26. /**
    27. * @description: 任务1
    28. * @param {*}
    29. * @return {*}
    30. */
    31. void Task1(void)
    32. {
    33. printf("enter Task 1.......\r\n");
    34. hi_gpio_init(); // GPIO初始化
    35. hi_io_set_pull(LED, HI_IO_PULL_DOWN); // 设置GPIO下拉,保证上电时为灭灯状态
    36. hi_io_set_func(LED, HI_IO_FUNC_GPIO_2_GPIO); // 设置IO2为GPIO功能
    37. hi_gpio_set_dir(LED, HI_GPIO_DIR_OUT); // 设置GPIO为输入模式
    38. while (1) {
    39. hi_gpio_set_ouput_val(LED, HI_GPIO_VALUE0); // 设置GPIO引脚为低电平
    40. usleep(TASK_DELAY_TIME); // 1s sleep
    41. hi_gpio_set_ouput_val(LED, HI_GPIO_VALUE1); // 设置GPIO引脚为高电平
    42. usleep(TASK_DELAY_TIME); // 1s sleep
    43. }
    44. }
    45. /**
    46. * @description: 初始化并创建任务
    47. * @param {*}
    48. * @return {*}
    49. */
    50. static void base_led_demo(void)
    51. {
    52. printf("[demo] Enter base_led_demo()!\r\n");
    53. osThreadAttr_t taskOptions;
    54. taskOptions.name = "Task1"; // 任务的名字
    55. taskOptions.attr_bits = 0; // 属性位
    56. taskOptions.cb_mem = NULL; // 堆空间地址
    57. taskOptions.cb_size = 0; // 堆空间大小
    58. taskOptions.stack_mem = NULL; // 栈空间地址
    59. taskOptions.stack_size = TASK_STACK_SIZE; // 栈空间大小 单位:字节
    60. taskOptions.priority = osPriorityNormal; // 任务的优先级:wq
    61. Task1_ID = osThreadNew((osThreadFunc_t)Task1, NULL, &taskOptions); // 创建任务1
    62. if (Task1_ID != NULL) {
    63. printf("ID = %d, Create Task1_ID is OK!\r\n", Task1_ID);
    64. }
    65. }
    66. SYS_RUN(base_led_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. └─doc
    17. │ HarmonyOS开发板实验指导书 v2.1.pdf
    18. │ 华清远见 FS_Hi3861开发指导.md
    19. │ 华清远见 FS_Hi3861新手入门手册.md
    20. ├─board
    21. │ FS-Hi3861-V4.2.pdf
    22. │ FS-Hi3861QDB-V3.2.pdf
    23. │ hi-12f_kit_v1.1.0A7E6BCB9%A6-20211025.pdf
    24. │ hi-12f_v1.1.2-A7E6BCB9%A6-20211202.pdf
    25. │ nodemcu-hi-07s_12f-kit_v1.1-20210913.pdf
    26. │ RTplay2.01_2024-06-14.pdf
    27. └─figures

    使用build

    效果就是LED间隔1秒闪烁。

  • 相关阅读:
    《优化接口设计的思路》系列:第五篇—接口发生异常如何统一处理
    Ajax(一)
    207、室外远距离点对点无线网桥组网方案
    dubbo异步调用三种方式
    在SQL语句正确的情况下,使用druid库查询ES数据报错的可能原因!
    减少无线链接切换导致数据体验变差的技术简介
    【语义分割】2022-HRViT CVPR
    【系列教程】ChatGPT+ROS:打造智能无人机自主飞行的下一代解决方案✈️【一】将chatgpt集成到ROS中
    那些年遇到过的问题与解决方案
    想要精通算法和SQL的成长之路 - 划分字母区间
  • 原文地址:https://blog.csdn.net/andylauren/article/details/139745571