HarmonyOS 小熊派

您所在的位置:网站首页 鸿蒙控制台音乐软件怎么换 HarmonyOS 小熊派

HarmonyOS 小熊派

2024-06-06 11:44| 来源: 网络整理| 查看: 265

一、点亮LED灯及LED灯闪烁解决方案

• 如何编写点亮LED灯程序• 如何编译烧录点亮LED灯程序• 如何调试点亮LED灯程序

二、目录

1. 添加点亮LED灯源码文件2. 编写点亮LED灯业务代码3. 编写编译构建文件BUILD.gn4. 调试LED灯程序5. 总结

三、添加点亮LED源码文件

1、新增LedExsampleFlash文件夹在applications/BearPi/BearPi-HM_Nano/sample路径下新建一个LedExsampleFlash目录,用于存放业务源码文件。2、新增LedExampleFlash.c文件在applications/BearPi/BearPi-HM_Nano/sample/LedExsampleFlash路径下新建一个applications/BearPi/BearPi-HM_Nano/sample/LedExsampleFlash/LedExampleFlash.c文件,该文件为业务源码文件。3、新增BUILD.gn文件在applications/BearPi/BearPi-HM_Nano/sample/LedExsampleFlash路径下新建一个BUILD.gn文件,该文件为业务源码编译脚本。

四、添加点亮LED灯源码

添加点亮LED灯源码

#include #include "ohos_init.h" #include "wifiiot_gpio.h" #include "wifiiot_gpio_ex.h" void ledExampleFlash(void) { //初始化GPIO引脚 GpioInit(); //设置GPIO_2的复用功能为普通的GPIO IoSetFunc(WIFI_IOT_IO_NAME_GPIO_2,WIFI_IOT_IO_FUNC_GPIO_2_GPIO); //设置GPIO_2为输出模式 GpioSetDir(WIFI_IOT_GPIO_IDX_2,WIFI_IOT_GPIO_DIR_OUT); //设置GPIO_2输出高电平点亮LED灯 GpioSetOutputVal(WIFI_IOT_GPIO_IDX_2,WIFI_IOT_GPIO_VALUE1); } APP_FEATURE_INIT(ledExampleFlash);

五、编写业务编译构建文件BUILD.gn

编写用于将业务构建成静态库的BUILD.gn文件在applications/BearPi/BearPi-HM_Nano/sample/LedExsampleFlash下的BUILD.gn文件中添加如下代码。

static_library("ledFlash") { sources = [ "LedExampleFlash.c", ] include_dirs = [ "//utils/native/lite/include", "//base/iot_hardware/interfaces/kits/wifiiot_lite", ] }

⚫ static_library中指定业务模块的编译结果,为静态库文件libledFlash.a,开发者根据实际情况完成填写。⚫ sources中指定静态库.a所依赖的.c文件及其路径,若路径中包含"//"则表示绝对路径(此处为代码根路径),若不包含"//"则表示相对路径。⚫ include_dirs中指定source所需要依赖的.h文件路径。

六、编写模块编译构建文件BUILD.gn

编写模块BUILD.gn文件,指定需参与构建的特性模块。在./applications/BearPi/BearPi-HM_Nano/sample下的BUILD.gn文件中添加如下代码。 

# Copyright (c) 2020 Nanjing Xiaoxiongpai Intelligent Technology Co., Ltd. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import("//build/lite/config/component/lite_component.gni") lite_component("app") { features = [ #"A1_kernal_thread:thread_example", #"A2_kernel_timer:timer_example", #"A3_kernel_event:event_example", #"A4_kernel_mutex:mutex_example", #"A5_kernel_semaphore:semaphore_example", #"A6_kernel_message:message_example", #"B1_basic_led_blink:led_example", #"B2_basic_button:button_example", #"B3_basic_pwm_led:pwm_example", #"B4_basic_adc:adc_example", #"B5_basic_i2c_nfc:i2c_example", #"B6_basic_uart:uart_example", #"C1_e53_sf1_mq2:e53_sf1_example", #"C2_e53_ia1_temp_humi_pls:e53_ia1_example", #"C3_e53_sc1_pls:e53_sc1_example", #"C4_e53_sc2_axis:e53_sc2_example", #"C5_e53_is1_infrared:e53_is1_example", #"D1_iot_wifi_ap:wifi_ap", #"D2_iot_wifi_sta_connect:wifi_sta_connect", #"D3_iot_udp_client:udp_client", #"D4_iot_tcp_server:tcp_server", #"D5_iot_mqtt:iot_mqtt", #"D6_iot_cloud_oc:oc_mqtt", #"D7_iot_cloud_onenet:onenet_mqtt", "LedExsampleFlash:ledFlash", ] }

⚫ LedExsampleFlash 是相对路径,指向applications/BearPi/BearPi-HM_Nano/sample/LedExsampleFlash/BUILD.gn⚫ LedExsampleFlash是目标,指向./applications\BearPi\BearPi-HM_Nano\sample\LedExsampleFlash\BUILD.gn中的static_library("ledFlash")。

七、调试LED程序,使LED灯闪烁

添加LED灯闪烁源码

#include #include #include "ohos_init.h" #include "wifiiot_gpio.h" #include "wifiiot_gpio_ex.h" void ledExampleFlash(void) { //初始化GPIO引脚 GpioInit(); //设置GPIO_2的复用功能为普通的GPIO IoSetFunc(WIFI_IOT_IO_NAME_GPIO_2,WIFI_IOT_IO_FUNC_GPIO_2_GPIO); //设置GPIO_2为输出模式 GpioSetDir(WIFI_IOT_GPIO_IDX_2,WIFI_IOT_GPIO_DIR_OUT); for (size_t i = 0; i < 10; i++) { /* code */ //设置GPIO_2输出高电平点亮LED灯 GpioSetOutputVal(WIFI_IOT_GPIO_IDX_2,WIFI_IOT_GPIO_VALUE1); usleep(1000000); GpioSetOutputVal(WIFI_IOT_GPIO_IDX_2,WIFI_IOT_GPIO_VALUE0); usleep(3000000); } //设置GPIO_2输出高电平点亮LED灯 GpioSetOutputVal(WIFI_IOT_GPIO_IDX_2,WIFI_IOT_GPIO_VALUE1); usleep(5000000); GpioSetOutputVal(WIFI_IOT_GPIO_IDX_2,WIFI_IOT_GPIO_VALUE0); } APP_FEATURE_INIT(ledExampleFlash);

八、本次实验小结

• 1、掌握如何在一个工作目录下添加多个案例• 2、掌握如何点亮LED灯• 3、掌握如何让LED灯闪烁



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3