Hi3861 OpenHarmony嵌入式应用入门--ADC

本篇讲解使用ADC进行采样,并使用API将采样值转为电压。

电路原理图

通过hi-12f_v1.1.2-规格书-20211202.pdf 找到IO9对应的ADC通道

GPIO API

API名称

说明

hi_u32 hi_gpio_init(hi_void);

GPIO模块初始化

hi_u32 hi_gpio_set_dir(hi_gpio_idx id, hi_gpio_dir dir);

设置GPIO引脚方向,id参数用于指定引脚,dir参数用于指定输入或输出

hi_u32 hi_io_set_func(hi_io_name id, hi_u8 val);

配置某个IO的复用功能

hi_u32 hi_adc_read(hi_adc_channel_index channel, hi_u16 *data, hi_adc_equ_model_sel equ_model,

hi_adc_cur_bais cur_bais, hi_u16 delay_cnt);

从一个ADC通道读一个数据

hi_float hi_adc_convert_to_voltage(hi_u16 data);

将ADC读取到的码字转换为电压

修改D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\BUILD.gn文件

# Copyright (c) 2023 Beijing HuaQing YuanJian Education 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("demo") {
  features = [
    #"base_00_helloworld:base_helloworld_example",
    #"base_01_led:base_led_example",
    #"base_02_loopkey:base_loopkey_example",
    #"base_03_irqkey:base_irqkey_example",
    "base_04_adc:base_adc_example",
  ]
}

创建D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\base_04_adc文件夹

文件夹中创建D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\base_04_adc\base_adc_example.c文件D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\base_04_adc\BUILD.gn文件

# Copyright (c) 2023 Beijing HuaQing YuanJian Education 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("base_adc_example") {
    sources = [
        "base_adc_example.c",
    ]

    include_dirs = [
        "//utils/native/lite/include",
        "//kernel/liteos_m/kal/cmsis",
        "//base/iot_hardware/peripheral/interfaces/kits",
        "//vendor/hqyj/fs_hi3861/common/bsp/include"
    ]
}
/*
 * Copyright (C) 2023 HiHope Open Source Organization .
 * 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 "cmsis_os2.h"
#include "hi_adc.h"
#include "hi_errno.h"
#include "hi_gpio.h"
#include "hi_io.h"
#include "ohos_init.h"

#define STACK_SIZE (1024)
#define DELAY_US (1000 * 1000)  // 延时时间1s

static void AdcGpioTask(void)
{
    hi_u16 value;             // 存储ADC值
    while (true) {
        if (hi_adc_read(HI_ADC_CHANNEL_4, &value, HI_ADC_EQU_MODEL_4, HI_ADC_CUR_BAIS_DEFAULT, 0) != HI_ERR_SUCCESS) {
            printf("ADC read error!\n");
        } else {
            hi_float fv = hi_adc_convert_to_voltage(value);
            printf("ADC_VALUE = %u %fV\n", (unsigned int)value, fv);
        }
        usleep(DELAY_US); // 延时1s
    }
}

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

    hi_gpio_init();                                         // GPIO初始化
    hi_io_set_func(HI_GPIO_IDX_9, HI_IO_FUNC_GPIO_9_GPIO);  // 设置GPIO9为GPIO模式
    hi_gpio_set_dir(HI_GPIO_IDX_9, HI_GPIO_DIR_IN);         // 设置GPIO9为输入模式

    attr.name = "AdcGpioTask";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = STACK_SIZE;
    attr.priority = osPriorityNormal;

    if (osThreadNew(AdcGpioTask, NULL, &attr) == NULL) {
        printf("[ADCExample] Falied to create AdcGpioTask!\n");
    }
}
SYS_RUN(AdcGpioEntry);

目录结构

│  config.json
│
├─common
│  └─bsp
│      ├─include
│      └─src
├─demo
│  │  BUILD.gn
│  │
│  ├─base_00_helloworld
│  │      base_helloworld_example.c
│  │      BUILD.gn
│  │
│  ├─base_01_led
│  │      base_led_example.c
│  │      BUILD.gn
│  │
│  ├─base_02_loopkey
│  │      base_loopkey_example.c
│  │      BUILD.gn
│  │
│  ├─base_03_irqkey
│  │      base_irqkey_example.c
│  │      BUILD.gn
│  │
│  └─base_04_adc
│          base_adc_example.c
│          BUILD.gn
│
└─doc
    │  HarmonyOS开发板实验指导书 v2.1.pdf
    │  华清远见 FS_Hi3861开发指导.md
    │  华清远见 FS_Hi3861新手入门手册.md
    │
    ├─board
    │      FS-Hi3861-V4.2.pdf
    │      FS-Hi3861QDB-V3.2.pdf
    │      hi-12f_kit_v1.1.0规格书-20211025.pdf
    │      hi-12f_v1.1.2-规格书-20211202.pdf
    │      nodemcu-hi-07s_12f-kit_v1.1-20210913.pdf
    │      RTplay2.01_2024-06-14.pdf
    │
    └─figures

使用build,编译成功后,使用upload进行烧录。

运行效果如下,用螺丝刀旋转滑动变阻器会使采样值变化。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值