Harmony OS 设备开发学习记录(九)-- 通过ADC值区分按键(中断方式)

Harmony OS 设备开发学习记录(九)-- 通过ADC值区分按键(中断方式)

基于hispark wifi套件采用harmony os 2.0全量代码

一、看原理图确定硬件电路

oled板上的两个按键
在这里插入图片描述

二、在源码中建立demo文件

在app下建立adcdemo文件夹并创建BUILD.gn和adc_botton_int.c文件
在这里插入图片描述

三、编写代码

在adcdemo/adc_botton_int.c中写入

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

#include "ohos_init.h"
#include "cmsis_os2.h"

#include "hi_gpio.h"
#include "hi_io.h"
#include "hi_adc.h"
#include "hi_errno.h"

#define MSGQUE_COUNT 1
#define MSG_SIZE 1

#define ACT_DOWN 0
#define ACT_UP 1

#define KEY2_BUTTON 2
#define KEY3_BUTTON 3
#define KEY4_BUTTON 4
#define NOKEY_BUTTON 0

static osMessageQueueId_t button_msgq;

void button_status_callback(uint8_t button_id, uint8_t button_action){
    uint32_t rst;
    rst = osMessageQueuePut(button_msgq, &button_id, 0, 0);
    if (rst != osOK)
    {
        printf("button_status_callback error! \n");
        return;
    }
    
    printf("button_status_callback %d\n", rst);
}


static void key234_isr_handler(char *arg)
{
    hi_u16 data;
    static uint8_t btn_record;
    // static enum button_id btn_record;
    hi_adc_read(HI_ADC_CHANNEL_2, &data, HI_ADC_EQU_MODEL_4, HI_ADC_CUR_BAIS_DEFAULT, 0);
    if ((5<(int)data) && ((int)data<228)){
        button_status_callback(KEY2_BUTTON, ACT_DOWN);
        btn_record = KEY2_BUTTON;
    }
    else if ((228<(int)data) && ((int)data<455)){
        button_status_callback(KEY3_BUTTON, ACT_DOWN);
        btn_record = KEY3_BUTTON;
    }
    else if ((455<(int)data) && ((int)data<682)){
        button_status_callback(KEY4_BUTTON, ACT_DOWN);
        btn_record = KEY4_BUTTON;
    }

    if ((int)data > 1422)
    {
        btn_record = NOKEY_BUTTON;
        button_status_callback(btn_record, ACT_UP);
        hi_gpio_register_isr_function(HI_GPIO_IDX_5, HI_INT_TYPE_EDGE, HI_GPIO_EDGE_FALL_LEVEL_LOW, key234_isr_handler, NULL);
    }
    else
    {
        hi_gpio_register_isr_function(HI_GPIO_IDX_5, HI_INT_TYPE_EDGE, HI_GPIO_EDGE_RISE_LEVEL_HIGH, key234_isr_handler, NULL);
    }
}

static void ButtonThreadTask(void *arg){
    (void)arg;
    key234_isr_handler(arg);
    uint8_t value = 0;

    osMessageQueueId_t msgq;
    msgq = osMessageQueueNew(MSGQUE_COUNT, MSG_SIZE, NULL);
    if (msgq == NULL){
        printf("The key234_isr_handler falied to create button queue!\n");
        return;
    } else {
        button_msgq = msgq;
    }

    while (1)
    {
        if (osMessageQueueGet(button_msgq, &value, 0, osWaitForever) == osOK){
            printf("success! get button value %d\n", value);
        }
    }
}


static void AdcGpioEntry(void){
    printf("ADC Test!\n");
    osThreadAttr_t attr;
    

    hi_gpio_init();
    hi_io_set_func(HI_GPIO_IDX_5, HI_IO_FUNC_GPIO_5_GPIO);
    hi_gpio_set_dir(HI_GPIO_IDX_5, HI_GPIO_DIR_IN);
    hi_io_set_pull(HI_GPIO_IDX_5, HI_IO_PULL_UP);

    attr.name = "ButtonThreadTask";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = 1024;
    attr.priority = 25;

    if (osThreadNew(ButtonThreadTask, NULL, &attr) == NULL) {
        printf("[LedExample] Falied to create LedTask!\n");
    }
}

SYS_RUN(AdcGpioEntry);

在adcdemo/BUILD.gn中写入

static_library("adcdemo") {
    sources = [
        "adc_botton_int.c"
    ]

    include_dirs = [
        "//utils/native/lite/include",
        "//kernel/liteos_m/components/cmsis/2.0",
        "//base/iot_hardware/peripheral/interfaces/kits",
        "//device/hisilicon/hispark_pegasus/sdk_liteos/include"
    ]
}

在上级目录的app/BUILD.gn中写入

import("//build/lite/config/component/lite_component.gni")

lite_component("app") {
    features = [
        "adcdemo",
    ]
}

四、在Linux下使用hb工具进行编译

root@DESKTOP-QAO2AOK:~/harmonyos/code-2.0-canary# hb set
[OHOS INFO] Input code path: .
OHOS Which product do you need?  wifiiot_hispark_pegasus
root@DESKTOP-QAO2AOK:~/harmonyos/code-2.0-canary# hb build

如果曾经设置过hb set就不需要再设置了,直接这样就可以了

root@DESKTOP-QAO2AOK:~/harmonyos/code-2.0-canary# hb build

看到success字样即为编译成功

五、将编译好的固件烧录到开发板

将linux中的源码文件夹中的out拷贝到Windows下替换原有out文件夹就可以了,但是要先删除原有out文件夹
打开vscode使用DevEco Device Tool打开源码文件夹
选择对应的开发板型号
这里选择的是hi3861
然后在项目设置中按照实际端口情况进行如下设置
在这里插入图片描述
保存项目并打开
在这里插入图片描述
点击upload进行烧录,烧录时需要根据提示按下开发板的rst键,稍等片刻,看到success代表烧录成功。
在这里插入图片描述
烧录完成后打开串口调试助手
在这里插入图片描述

按下rst键重启开发板,这时候分别按下核心板上的usr按键,和oled板上的s1按键与s2按键可以看到串口中会输出对应按下的按键,按键弹起时会输出0。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

EMb-Y

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值