欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 维修 > Hi3861 OpenHarmony嵌入式应用入门--ADC

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

2025/4/25 18:23:08 来源:https://blog.csdn.net/andylauren/article/details/139773731  浏览:    关键词: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)  // 延时时间1sstatic 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进行烧录。

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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词