HarmonyOS智慧设备开发-LED灯控制案例 原创

鸿蒙时代
发布于 2021-8-23 11:52
浏览
1收藏

一、技术相关

项目名称: led_kz_02
项目语言: C语言
开发板:Hi3861
工具:vscode

二、效果如下
HarmonyOS智慧设备开发-LED灯控制案例-鸿蒙开发者社区
HarmonyOS智慧设备开发-LED灯控制案例-鸿蒙开发者社区
三、开发过程

1.建立项目
在applications/sample/wifi-iot/app/下新建一个文件夹用于存放项目文件,在文件中创建一个led_kz_02.c文件用于存放业务代码,和一个BUILD.gn用于存放编译配置。

2.编写代码
控制原理:
在Hi361开发板中,GPIO09控制LED灯,GPIO05与user按钮连接,通过函数hi_gpio_register_isr_function监控user按钮中断并执行相应操作;
static void *UserButton(const char *arg){ //中断后执行的函数
USER_KZ_GPIO=!USER_KZ_GPIO;
}

static void *kzLed(const char *arg)
{
hi_gpio_register_isr_function(LED_KZ_GPIO,HI_INT_TYPE_EDGE,
HI_GPIO_EDGE_FALL_LEVEL_LOW,UserButton, NULL);
while (1)
{
IoTGpioSetOutputVal(LED_Out_GPIO, USER_KZ_GPIO);
}
return NULL;
}

3.运行代码
修改applications/sample/wifi-iot/app/BUILD.gn文件,使其编译新建的led_kz.c文件
import(“//build/lite/config/component/lite_component.gni”)
lite_component(“app”) {
features = [
“led_kz:led_kz”
]
}

然后进行编译烧录,按下res键运行项目后,按一次user,led灯会随之熄灭;再按一次便会亮起。

4.关键代码
led_kz.c
#include <stdio.h>
#include <unistd.h>
#include “ohos_init.h”
#include “cmsis_os2.h”
#include “hi_gpio.h”
#include “hi_io.h”

#define LED_TASK_STACK_SIZE 512
#define LED_TASK_PRIO 25
#define LED_Out_GPIO 9
#define LED_KZ_GPIO 5

int USER_KZ_GPIO = 0;
static void *UserButton(const char *arg){
USER_KZ_GPIO=!USER_KZ_GPIO;
}

static void *kzLed(const char *arg)
{
hi_gpio_register_isr_function(LED_KZ_GPIO, HI_INT_TYPE_EDGE, HI_GPIO_EDGE_FALL_LEVEL_LOW,
UserButton, NULL);
while (1)
{
IoTGpioSetOutputVal(LED_Out_GPIO, USER_KZ_GPIO);
}
return NULL;
}

static void LedExampleEntry(void)
{
osThreadAttr_t attr;

hi_gpio_init();
hi_io_set_func(LED_Out_GPIO, HI_IO_FUNC_GPIO_9_GPIO);
hi_gpio_set_dir(LED_Out_GPIO, HI_GPIO_DIR_OUT);
hi_io_set_func(LED_KZ_GPIO, HI_IO_FUNC_GPIO_5_GPIO);
hi_gpio_set_dir(LED_KZ_GPIO, HI_GPIO_DIR_IN);
hi_io_set_pull(LED_KZ_GPIO, HI_IO_PULL_UP);

attr.name = "kzLed";
attr.attr_bits = 0U;
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
attr.stack_size = LED_TASK_STACK_SIZE;
attr.priority = LED_TASK_PRIO;

if (osThreadNew((osThreadFunc_t)kzLed, NULL, &attr) == NULL) {
    printf("[LedExample] Falied to create LedTask!\n");
}

}

SYS_RUN(LedExampleEntry);

BUILD.gn
static_library(“led_kz”) {
sources = [
“led_kz.c”
]
include_dirs = [
“//utils/native/lite/include”,
“//kernel/liteos_m/components/cmsis/2.0”,
“//base/iot_hardware/peripheral/interfaces/kits”,
]
}

完整代码地址:https://gitee.com/jltfcloudcn/smart-devices/tree/master/led_kz_zd

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
分类
标签
HarmonyOS智慧设备开发-LED灯控制案例.docx 127.75K 36次下载
收藏 1
回复
举报
回复
    相关推荐