HarmonyOS小熊派 | B2_basic_button案例学习

举报
Yuchuan 发表于 2021/04/03 22:57:06 2021/04/03
【摘要】 使用GPIO输入功能去读取按键状态

开发板基础外设开发——GPIO输入

本示例将演示如何在BearPi-HM_Nano开发板上使用GPIO输入功能去读取按键状态

GPIO API分析

本案例主要使用了以下几个API完成GPIO输出功能

1、GpioInit()

头文件相对路径:

base\iot_hardware\interfaces\kits\wifiiot_lite\wifiiot_gpio.h

导入头文件:

#include "wifiiot_gpio.h"
/**
 * @brief Initializes the GPIO device.
 *
 * @return Returns {@link WIFI_IOT_SUCCESS} if the operation is successful;
 * returns an error code defined in {@link wifiiot_errno.h} otherwise.
 * @since 1.0
 * @version 1.0
 */
unsigned int GpioInit(void);

描述:

初始化GPIO外设

2、IoSetFunc()

设置GPIO_2的复用功能为普通GPIO

头文件相对路径:

base\iot_hardware\interfaces\kits\wifiiot_lite\wifiiot_gpio_ex.h

导入头文件:

#include "wifiiot_gpio_ex.h"
/**
 * @brief Sets the multiplexing function for a GPIO pin.
 *
 * @param id Indicates the GPIO pin.
 * @param val Indicates the I/O multiplexing function. For example,
 * if the value of <b>id</b> is {@link WIFI_IOT_IO_NAME_GPIO_0},
 * the value type of <b>val</b> is {@link WifiIotIoFuncGpio0}.
 * If the value of <b>id</b> is {@link WIFI_IOT_IO_NAME_GPIO_1},
 * the value type of <b>val</b> is {@link WifiIotIoFuncGpio1}.
 * The same rule applies to other values.
 * @return Returns {@link WIFI_IOT_SUCCESS} if the operation is successful;
 * returns an error code defined in {@link wifiiot_errno.h} otherwise.
 * @since 1.0
 * @version 1.0
 */
unsigned int IoSetFunc(WifiIotIoName id, unsigned char val);

描述:

设置GPIO引脚复用功能

参数:

名字 描述
id 表示GPIO引脚号.
val 表示GPIO复用功能

3、GpioSetDir()

设置GPIO_2为输出模式

头文件相对路径:

base\iot_hardware\interfaces\kits\wifiiot_lite\wifiiot_gpio.h

导入头文件:

#include "wifiiot_gpio.h"
/**
 * @brief Sets the direction for a GPIO pin.
 *
 * @param id Indicates the GPIO pin ID.
 * @param dir Indicates the GPIO input/output direction.
 * @return Returns {@link WIFI_IOT_SUCCESS} if the operation is successful;
 * returns an error code defined in {@link wifiiot_errno.h} otherwise.
 * @since 1.0
 * @version 1.0
 */
unsigned int GpioSetDir(WifiIotGpioIdx id, WifiIotGpioDir dir);

描述:

设置GPIO输出方向

参数:

名字 描述
id 表示GPIO引脚号.
dir 表示GPIO输出方向.

4、IoSetPull()

设备GPIO的上下拉方式

头文件相对路径:

base\iot_hardware\interfaces\kits\wifiiot_lite\wifiiot_gpio_ex.h

导入头文件:

#include "wifiiot_gpio_ex.h"
/**
 * @brief Sets the pull for a GPIO pin.
 *
 * @param id Indicates the GPIO pin.
 * @param val Indicates the pull-up or pull-down to set.
 * @return Returns {@link WIFI_IOT_SUCCESS} if the operation is successful;
 * returns an error code defined in {@link wifiiot_errno.h} otherwise.
 * @since 1.0
 * @version 1.0
 */
unsigned int IoSetPull(WifiIotIoName id, WifiIotIoPull val);

描述:

设备GPIO的上下拉方式

参数:

名字 描述
id 表示GPIO引脚号.
val 表示要设置的上拉或下拉.

5、GpioRegisterIsrFunc()

启用GPIO引脚的中断功能

头文件相对路径:

base\iot_hardware\interfaces\kits\wifiiot_lite\wifiiot_gpio.h

导入头文件:

#include "wifiiot_gpio.h"
/**
 * @brief Enables the interrupt function for a GPIO pin.
 *
 * This function can be used to set the interrupt type, interrupt polarity,
 * and interrupt callback for a GPIO pin.
 *
 * @param id Indicates the GPIO pin ID.
 * @param intType Indicates the interrupt type.
 * @param intPolarity Indicates the interrupt polarity.
 * @param func Indicates the interrupt callback function.
 * @param arg Indicates the pointer to the argument used in the interrupt callback function.
 * @return Returns {@link WIFI_IOT_SUCCESS} if the operation is successful;
 * returns an error code defined in {@link wifiiot_errno.h} otherwise.
 * @since 1.0
 * @version 1.0
 */
unsigned int GpioRegisterIsrFunc(WifiIotGpioIdx id, WifiIotGpioIntType intType, WifiIotGpioIntPolarity intPolarity,
                                 GpioIsrCallbackFunc func, char *arg);

描述:

启用GPIO引脚的中断功能。这个函数可以用来为GPIO pin设置中断类型、中断极性和中断回调。

参数:

名字 描述
id 表示GPIO引脚号.
intType 表示中断类型.
intPolarity 表示中断极性.
func 表示中断回调函数.
arg 表示中断回调函数中使用的参数的指针.

6、GpioSetOutputVal()

设置GPIO_2输出电平

头文件相对路径:

base\iot_hardware\interfaces\kits\wifiiot_lite\wifiiot_gpio.h

导入头文件:

#include "wifiiot_gpio.h"
/**
 * @brief Sets the output level value for a GPIO pin.
 *
 * @param id Indicates the GPIO pin ID.
 * @param val Indicates the output level value.
 * @return Returns {@link WIFI_IOT_SUCCESS} if the operation is successful;
 * returns an error code defined in {@link wifiiot_errno.h} otherwise.
 * @since 1.0
 * @version 1.0
 */
unsigned int GpioSetOutputVal(WifiIotGpioIdx id, WifiIotGpioValue val);

硬件设计

本案例将使用板载的两个用户按键来验证GPIO的输入功能,在BearPi-HM_Nano开发板上用户按键的连接电路图如下图所示,按键F1的检测引脚与主控芯片的GPIO_11连接,按键F2的检测引脚与主控芯片的GPIO_12连接,所以需要编写软件去读取GPIO_11和GPIO_12的电平值,判断按键是否被按下。

软件设计

主要代码分析

这部分代码主要分析按键触发中断的功能代码,这里以按键F1为例,按键F1的检测引脚与主控芯片的GPIO_11连接,首先通过调用IoSetFunc()和GpioSetDir()将GPIO_11设置为普通GPIO的输出模式。从前面原理图可知,当按键按下时,GPIO_11会被下拉到地,所以这里要使用IoSetPull()将GPIO_11引脚设置为上拉,这样才能产生电平的跳变。最后通过GpioRegisterIsrFunc()将中断类型设置为边沿触发,且为下降沿触发,当按键被按下时,GPIO_11会从高电平转为低电平,产生一个下降,这个时候就会触发中断并回调F1_Pressed函数。在F1_Pressed函数中实现点亮LED灯操作。

button_example.c

/*
 * 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.
 */

#include <stdio.h>
#include <unistd.h>

#include "ohos_init.h"
#include "cmsis_os2.h"
#include "wifiiot_gpio.h"
#include "wifiiot_gpio_ex.h"

static void F1_Pressed(char *arg)
{
    (void)arg;
    GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_2, 1);
}
static void F2_Pressed(char *arg)
{
    (void)arg;
    GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_2, 0);
}
static void ButtonExampleEntry(void)
{
    GpioInit();

    //初始化LED灯
    IoSetFunc(WIFI_IOT_IO_NAME_GPIO_2, WIFI_IOT_IO_FUNC_GPIO_2_GPIO);

    GpioSetDir(WIFI_IOT_IO_NAME_GPIO_2, WIFI_IOT_GPIO_DIR_OUT);

    //初始化F1按键,设置为下降沿触发中断
    IoSetFunc(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_IO_FUNC_GPIO_11_GPIO);

    GpioSetDir(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_GPIO_DIR_IN);
    IoSetPull(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_IO_PULL_UP);
    GpioRegisterIsrFunc(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_INT_TYPE_EDGE, WIFI_IOT_GPIO_EDGE_FALL_LEVEL_LOW, F1_Pressed, NULL);

    //初始化F2按键,设置为下降沿触发中断
    IoSetFunc(WIFI_IOT_IO_NAME_GPIO_12, WIFI_IOT_IO_FUNC_GPIO_12_GPIO);

    GpioSetDir(WIFI_IOT_IO_NAME_GPIO_12, WIFI_IOT_GPIO_DIR_IN);
    IoSetPull(WIFI_IOT_IO_NAME_GPIO_12, WIFI_IOT_IO_PULL_UP);
    GpioRegisterIsrFunc(WIFI_IOT_IO_NAME_GPIO_12, WIFI_IOT_INT_TYPE_EDGE, WIFI_IOT_GPIO_EDGE_FALL_LEVEL_LOW, F2_Pressed, NULL);
}

APP_FEATURE_INIT(ButtonExampleEntry);

applications\BearPi\BearPi-HM_Nano\sample\B2_basic_button\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.

static_library("button_example") {
    sources = [
        "button_example.c"
    ]

    include_dirs = [
        "//utils/native/lite/include",
        "//kernel/liteos_m/components/cmsis/2.0",
        "//base/iot_hardware/interfaces/kits/wifiiot_lite",
    ]
}

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",
    ]
}
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。