鸿蒙HiSpark Wi-Fi IoT开发套件试用04(点亮LED灯)
返回>>鸿蒙HiSpark Wi-Fi IoT开发套件试用笔记 目录
hi3861相关资料、硬件原理图等:https://harmonyos.51cto.com/posts/1158,这个很重要,建议大家下载了好好研究。开源硬件的学习都是从点灯开始,点灯的贴子可以参考连老师的Hi3861 GPIO操作 点灯和按键实验:https://harmonyos.51cto.com/posts/1236,把代码分写可能更适合初学者,顺便推荐连老师的这句代码可以跟踪执行情况,超级实用呀:
printf("%s %d \r\n",__FILE__,__LINE__);
点亮主板的板载LED灯的步骤如下:
1. 在\applications\sample\wifi-iot\app下新建文件夹“led_demo”,在led_demo文件夹下新建BUILD.gn和led_demo.c 两个文件,具体目录结构如下:
.
└── applications
└── sample
└── wifi-iot
└── app
│── led_demo
│ │── BUILD.gn
│ └── led_demo.c
└── BUILD.gn
2. \applications\sample\wifi-iot\app\led_demo下的BUILD.gn代码如下:
static_library("led_demo") {
sources = [
"led_demo.c"
]
include_dirs = [
"//utils/native/lite/include",
"//kernel/liteos_m/components/cmsis/2.0",
"//base/iot_hardware/interfaces/kits/wifiiot_lite"
]
}
3. \applications\sample\wifi-iot\app\led_demo下的led_demo.c代码如下:
#include <unistd.h>
#include "stdio.h"
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "wifiiot_gpio.h"
#include "wifiiot_gpio_ex.h"
#include <hi_types_base.h>
#include <hi_i2c.h>
#include <hi_early_debug.h>
#include <hi_stdlib.h>
void *LedTask(const char *arg)
{
(void)arg;
while (1)
{
GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_9, 0);
usleep(300000);
GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_9, 1);
usleep(800000);
}
return NULL;
}
void led_demo(void)
{
osThreadAttr_t attr;
//复用引脚为 GPIO
IoSetFunc(WIFI_IOT_IO_NAME_GPIO_9, WIFI_IOT_IO_FUNC_GPIO_9_GPIO);
//设置为输出
GpioSetDir(WIFI_IOT_IO_NAME_GPIO_9, WIFI_IOT_GPIO_DIR_OUT);
attr.name = "LedTask";
attr.attr_bits = 0U;
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
attr.stack_size = 512;
attr.priority = 26;
if (osThreadNew((osThreadFunc_t)LedTask, NULL, &attr) == NULL)
{
printf("[LedExample] Falied to create LedTask!\n");
}
}
SYS_RUN(led_demo);
保存,烧录重启后可以看到主板上LED灯的闪烁。
4. 不要忘修改\applications\sample\wifi-iot\app下的BUILD.gn代码
import("//build/lite/config/component/lite_component.gni")
lite_component("app") {
features = [
"led_demo:led_demo",
]
}
提示:此案例为单主板案例,主板不要放到底板上。