OpenHarmony坐牢开发04 —— 编写Led-Demo
这是老师交给我的文档:hispark_pegasus/demo/led_demo/README.md · OpenHarmony/vendor_hisilicon - Gitee.com
然而,这个实验的LED是由hi3861的引脚控制的,这个引脚叫GPIO10,LED灯在交通灯板子上,但是现在没有交通灯板子。我实验的任务就是编程控制Hi3861自身板子上的LED,控制这个LED的引脚叫GPIO9。
作为0基础的小白,尝试一下吧。
- 按照文档说的,下载源代码,放入到项目内
- 先编译一下试试
-
没想到竟然出错了
iot_gpio.h: No such file or directory
仔细排查之后觉得应该是项目结构优化更新了,于是试试上网找一下解决方法。
-
寻找一下地址
-
更改
./wifi-iot/app/led_demo/build.gn
static_library("ledDemo") { sources = [ "app_demo_led_control.c", "hal_iot_gpio_ex.c", ] include_dirs = [ "./", "//commonlibrary/utils_lite/include", "//kernel/liteos_m/kal/cmsis", "//base/iot_hardware/peripheral/interfaces/kits", ] }
-
接下来试着修改,好习惯,先把源代码存下来
//app_demo_led_control.c #include <stdio.h> #include <unistd.h> #include "ohos_init.h" #include "cmsis_os2.h" #include "iot_gpio.h" #include "iot_watchdog.h" #include "hi_io.h" #define IOT_GPIO_IDX_10 10 // for hispark_pegasus static void *LedCntrolDemo(const char *arg) { (void)arg; printf("LedTask start\r\n"); // 配置GPIO引脚号和输出值 IoTGpioSetOutputVal(IOT_GPIO_IDX_10, IOT_GPIO_VALUE1); return NULL; } static void LedControlTask(void) { osThreadAttr_t attr; // 初始化GPIO IoTGpioInit(IOT_GPIO_IDX_10); // 设置GPIO为输出方向 IoTGpioSetDir(IOT_GPIO_IDX_10, IOT_GPIO_DIR_OUT); attr.name = "LedCntrolDemo"; attr.attr_bits = 0U; attr.cb_mem = NULL; attr.cb_size = 0U; attr.stack_mem = NULL; attr.stack_size = 1024; /* 堆栈大小为1024 */ attr.priority = osPriorityNormal; // 报错 if (osThreadNew((osThreadFunc_t)LedCntrolDemo, NULL, &attr) == NULL) { printf("[LedExample] Failed to create LedTask!\n"); } } SYS_RUN(LedControlTask);
-
阅读
iot_gpio.h
和iot_gpio_ex.h
发现两个引脚的参数基本一致
-
于是尝试直接把所有的10都修改为9,,,
//app_demo_led_control.c #include <stdio.h> #include <unistd.h> #include "ohos_init.h" #include "cmsis_os2.h" #include "iot_gpio.h" #include "iot_watchdog.h" #include "hi_io.h" #define IOT_GPIO_IDX_9 9 // for hispark_pegasus static void *LedCntrolDemo(const char *arg) { (void)arg; printf("LedTask start\r\n"); // 配置GPIO引脚号和输出值 IoTGpioSetOutputVal(IOT_GPIO_IDX_9, IOT_GPIO_VALUE1); return NULL; } static void LedControlTask(void) { osThreadAttr_t attr; // 初始化GPIO IoTGpioInit(IOT_GPIO_IDX_9); // 设置GPIO为输出方向 IoTGpioSetDir(IOT_GPIO_IDX_9, IOT_GPIO_DIR_OUT); attr.name = "LedCntrolDemo"; attr.attr_bits = 0U; attr.cb_mem = NULL; attr.cb_size = 0U; attr.stack_mem = NULL; attr.stack_size = 1024; /* 堆栈大小为1024 */ attr.priority = osPriorityNormal; // 报错 if (osThreadNew((osThreadFunc_t)LedCntrolDemo, NULL, &attr) == NULL) { printf("[LedExample] Failed to create LedTask!\n"); } } SYS_RUN(LedControlTask);
-
之后经过老师提醒,重新阅读了一遍helloworld的步骤,发现现在用的是旧框架。
-
于是想直接借用HelloWorld的方法,利用first_project的配置,于是修改
app/my_first_app/build.gn
static_library("myapp") { sources = [ "hello_world.c", "hal_iot_gpio_ex.c", ] include_dirs = [ "//utils/native/lite/include", "./", "//commonlibrary/utils_lite/include", "//kernel/liteos_m/kal/cmsis", "//base/iot_hardware/peripheral/interfaces/kits", ] }
-
修改
helloworld.c
,注意输出值改为0,因为GPIO9接到了发光二极管LED的阴极,所以GPIO9置成零才能让LED亮。GPIO10接到了另外一个发光二极管LED的阳极,所以GPIO10置成1才能让LED亮。#include <stdio.h> #include <unistd.h> #include "ohos_init.h" #include "cmsis_os2.h" #include "iot_gpio.h" #include "iot_watchdog.h" #include "hi_io.h" #define IOT_GPIO_IDX_9 9 // for hispark_pegasus static void *LedCntrolDemo(const char *arg) { (void)arg; printf("LedTask start\r\n"); // 配置GPIO引脚号和输出值 IoTGpioSetOutputVal(IOT_GPIO_IDX_9, IOT_GPIO_VALUE0); return NULL; } void HelloWorld(void) { //printf("[DEMO] Hello world.20230107\n"); osThreadAttr_t attr; // 初始化GPIO IoTGpioInit(IOT_GPIO_IDX_9); // 设置GPIO为输出方向 IoTGpioSetDir(IOT_GPIO_IDX_9, IOT_GPIO_DIR_OUT); attr.name = "LedCntrolDemo"; attr.attr_bits = 0U; attr.cb_mem = NULL; attr.cb_size = 0U; attr.stack_mem = NULL; attr.stack_size = 1024; /* 堆栈大小为1024 */ attr.priority = osPriorityNormal; // 报错 if (osThreadNew((osThreadFunc_t)LedCntrolDemo, NULL, &attr) == NULL) { printf("[LedExample] Failed to create LedTask!\n"); } } SYS_RUN(HelloWorld);
-
运行结果
画圈处的灯亮了。
标签:OpenHarmony,NULL,Led,attr,04,IOT,GPIO,include,iot From: https://www.cnblogs.com/ZzTzZ/p/17031411.html