1 GPIO 实验-主芯片 GPIO 输出实验
1 实验目的
本实验通过控制主芯片 GPIO 9 输出高低电平,实现对交通灯 LED3 红灯亮灭的控制。
2 实验要求
1.软件要求:VSCode,hi3861_hdu_iot_application(如果采用 zip 包下载,SDK 名称为 hi3861_hdu_iot_application-master,如果采用 git 下载 SDK 名称为 hi3861_hdu_iot_application,此处以采用 git 下载为例);
2.硬件要求:HISPARK_HI3861_IOT_VER.C 开发套件;
3.硬件配置:将 MOTOR_EN 拨码开关由 ON 拨到 OFF;硬件搭建如下图所示:
3 接口说明
3.1 IoTGpioInit()
3.2 IoSetFunc()
3..3 IoTGpioSetDir()
3.4 IoTGpioSetOutputVal()
4 实验流程
步骤一:hi3861_hdu_iot_application/src/vendor/hisilicon/hispark_pegasus/demo/led_demo 文件夹复制到 hi3861_hdu_iot_application/src/applications/sample/wifi-iot/app/目录下。
步骤二:修改 applications/sample/wifi-iot/app/目录下的 BUILD.gn,在 features 字段中添加 led_demo:led_control。注:第一个 led_demo 指的是需要编译的工程目录,第二个 led_control 指的是 applications/sample/wifi-iot/app/led_demo/BUILD.gn 文件中的静态库,名称为 led_control。
import("//build/lite/config/component/lite_component.gni")lite_component("app") { features = [ "led_demo:led_control", ]}
复制代码
步骤三:LED 亮灭原理:通过 GPIO 口输出高低电平控制 LED 亮灭。
从硬件原理图可以分析出 LED3 红灯对应的管脚是 GPIO09,因此我们需要通过控制 GPIO09 的高低电平输出,来控制 LED3 的亮灭。
1 /*2 * Copyright (c) 2022 HiSilicon (Shanghai) Technologies CO., LIMITED.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 at6 *7 * http://www.apache.org/licenses/LICENSE-2.08 *9 * Unless required by applicable law or agreed to in writing, software10 * 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 and13 * limitations under the License.14 */15 16 #include <stdio.h>17 #include <unistd.h>18 19 #include "iot_gpio_ex.h"20 #include "ohos_init.h"21 #include "cmsis_os2.h"22 #include "iot_gpio.h"23 24 #define LED_LOOP 1025 #define DELYA_MS 100026 27 static void LedExampleEntry(void)28 {29 // LED3的GPIO初始化 GPIO initialization of LED330 IoTGpioInit(IOT_IO_NAME_GPIO_9);31 // 设置GPIO9的管脚复用关系为GPIO Set the pin reuse relationship of GPIO9 to GPIO32 IoSetFunc(IOT_IO_NAME_GPIO_9, IOT_IO_FUNC_GPIO_9_GPIO);33 // GPIO方向设置为输出 GPIO direction set to output34 IoTGpioSetDir(IOT_IO_NAME_GPIO_9, IOT_GPIO_DIR_OUT);35 36 for (int i = 0; i < LED_LOOP; i++) {37 /*38 * 设置GPIO09输出高电平点亮红色交通灯LED339 * Set GPIO09 output high level to turn on red traffic light LED 340 */41 IoTGpioSetOutputVal(IOT_IO_NAME_GPIO_9, IOT_GPIO_VALUE1);42 TaskMsleep(DELYA_MS);43 /*44 * 设置GPIO09输出低电平熄灭红色交通灯LED345 * Set GPIO09 output low level to turn off red traffic light LED 346 */47 IoTGpioSetOutputVal(IOT_IO_NAME_GPIO_9, IOT_GPIO_VALUE0);48 TaskMsleep(DELYA_MS);49 }50 }51 52 static void LedControlTask(void)53 {54 osThreadAttr_t attr;55 56 attr.name = "LedCntrolDemo";57 attr.attr_bits = 0U;58 attr.cb_mem = NULL;59 attr.cb_size = 0U;60 attr.stack_mem = NULL;61 attr.stack_size = 1024; // 堆栈大小为1024 stack size 102462 attr.priority = osPriorityNormal;63 if (osThreadNew((osThreadFunc_t)LedExampleEntry, NULL, &attr) == NULL) {64 printf("[LedExample] Failed to create LedTask!\n");65 }66 }67 68 APP_FEATURE_INIT(LedControlTask);
复制代码
5 实验结果
软件烧录成功后,按一下开发板的 RESET 按键复位开发板,可以看到红灯先亮起然后熄灭,循环 10 次后红灯灯熄灭,说明我们使用 GPIO 控制 LED 灯的实验成功。LED 的实验效果图如下图所示。
评论