HarmonyOS小熊派 | HarmonyOS基础外设开发--ADC采样

BearPi-HM_Nano开发板基础外设开发——ADC采样

本示例将演示如何在BearPi-HM_Nano开发板上通过按下按键改变GPIO口的电压,并使用ADC读取GPIO的电压值,

BearPi-HM_Nano

ADC API分析

本案例主要使用了以下API完成ADC采样的功能

IoTAdcRead()

/**
 * @brief Reads a piece of sampled data from a specified ADC channel based on the input parameters.
 *
 *
 *
 * @param channel Indicates the ADC channel index.
 * @param data Indicates the pointer to the address for storing the read data.
 * @param equModel Indicates the equation model.
 * @param curBais Indicates the analog power control mode.
 * @param rstCnt Indicates the count of the time from reset to conversion start.
 *               One count is equal to 334 ns. The value must range from 0 to 0xFF0.
 * @return Returns {@link IOT_SUCCESS} if the PWM signal output is stopped;
 * returns {@link IOT_FAILURE} otherwise. For details about other return values, see the chip description.
 * @since 1.0
 * @version 1.0
 */
unsigned int IoTAdcRead(unsigned int channel, unsigned short *data, IotAdcEquModelSel equModel,
                     IotAdcCurBais curBais, unsigned short rstCnt);

描述:

根据输入参数从指定的ADC通道读取一段采样数据

参数:

名字 描述
channel 表示ADC通道.
data 表示指向存储读取数据的地址的指针
equModel 表示平均算法的次数
curBais 表示模拟功率控制模式
rstCnt 指示从重置到转换开始的时间计数。一次计数等于334纳秒。值的范围必须从0到0xFF

硬件设计

本案例将使用板载用户按键F1来模拟GPIO口电压的变化。通过查看芯片手册可知GPIO_11对应的是 ADC Channel 5 ,所以需要编写软件去读取ADC Channel 5的电压,程序设计时先将GPIO_11上拉,使GPIO_11的电压一直处于高电平,当按键按下时GPIO_11接地,此时GPIO_11的电压变为 0 V。

软件设计

主要代码分析

该函数通过使用IoTAdcRead()函数来读取 ADC_CHANNEL_5 的数值存储在data中, IOT_ADC_EQU_MODEL_8 表示8次平均算法模式,IOT_ADC_CUR_BAIS_DEFAULT 表示默认的自动识别模式,最后通过 data * 1.8 * 4 / 4096.0 计算出实际的电压值。

yuchuan_basic_adc.c

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

#include "ohos_init.h"
#include "cmsis_os2.h"
#include "iot_gpio.h"
#include "iot_gpio_ex.h"
#include "iot_adc.h"
#include "iot_errno.h"


#define TASK_STACK_SIZE 1024 * 4
#define TASK_PRIORITY 24
#define BUTTON_F1_GPIO 11
#define ADC_CHANNEL_5 5

static float getVoltage(void)
{
    unsigned int ret;
    unsigned short data;

    //ADC通道采样数据读取电压只
    //unsigned int IoTAdcRead(unsigned int channel, unsigned short *data, IotAdcEquModelSel equModel,
    //                 IotAdcCurBais curBais, unsigned short rstCnt);
    ret = IoTAdcRead(ADC_CHANNEL_5, &data, IOT_ADC_EQU_MODEL_8, IOT_ADC_CUR_BAIS_DEFAULT, 0Xff);
    if (ret != IOT_SUCCESS)
    {
        /* code */
        printf("ADC Read Failed!!!\n");
    }
    
    return (float)data * 1.8 * 4 / 4096.0;
}

void yuchuanThreadADC(void *argument)
{
    (void)argument;
    float voltage;

    //上拉,让按键为按下GPIO_11 保持高电平状态
    IoTGpioSetPull(BUTTON_F1_GPIO,IOT_GPIO_PULL_UP);
    while (1)
    {
        /* code */
        printf("***************************************\r\n");
        printf("************Yuchuan ADC Example********\r\n");
        printf("***************************************\r\n");

        //获取Button F1 的电压之
        voltage = getVoltage();
        printf("YuchuanThread get voltage is : %.3fV\n",voltage);

        //延时1s
        usleep(1000000);
    }
    
}

static void yuchuanBasicADCExample(void)
{
    osThreadAttr_t threadAttr;
    threadAttr.attr_bits = 0U;
    threadAttr.cb_mem = NULL;
    threadAttr.cb_size = 0U;
    threadAttr.stack_mem = NULL;
    threadAttr.stack_size = TASK_STACK_SIZE;
    threadAttr.priority = TASK_PRIORITY;

    threadAttr.name = "YuchuanThreadADC";
    if (osThreadNew((osThreadFunc_t)yuchuanThreadADC,NULL,&threadAttr) == NULL)
    {
        /* code */
        printf("Falied to Create YuchuanThreadADC!!!\n");
    }
    

}

APP_FEATURE_INIT(yuchuanBasicADCExample);

编译调试

//applications/sample/BearPi/BearPi-HM_Nano/sample/B4_YuchuanBasicADC

static_library("yuchuanBasicADC"){
    sources = [
        "yuchuan_basic_adc.c",
    ]

    include_dirs = [
        "//utils/native/lite/include",
        "//kernel/liteos_m/kal/cmsis",
        "//base/iot_hardware/peripheral/interfaces/kits",
    ]
}

修改 BUILD.gn 文件

修改applications\BearPi\BearPi-HM_Nano\sample 路径下 BUILD.gn 文件,指定 yuchuanBasicADC 参与编译。

# 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("sample") {
    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",
        #"D8_iot_cloud_oc_smoke:cloud_oc_smoke",
        #"D9_iot_cloud_oc_light:cloud_oc_light",
        #"D10_iot_cloud_oc_manhole_cover:cloud_oc_manhole_cover",
        #"D11_iot_cloud_oc_infrared:cloud_oc_infrared",
        #"D12_iot_cloud_oc_agriculture:cloud_oc_agriculture",
        #"D13_iot_cloud_oc_gps:cloud_oc_gps",
        #"A1_YuchuanThread:yuchuanThread",
        #"A2_YuchuanTimer:yuchuanTimer",
        #"A4_YuchuanMutex:yuchuanMutex",
        #"A5_YuchuanSemaphore:yuchuanSemaphore",
        #"A3_YuchuanEvent:yuchuanEvent",
        #"A6_YuchuanMessage:yuchuanMessageQueue",
        #"B3_YuchuanBasicPWM:yuchuanPWM",
        "B4_YuchuanBasicADC:yuchuanBasicADC",
    ]
}

运行结果

示例代码编译烧录代码后,按下开发板的RESET按键,通过串口助手查看日志,当F1按键未按下时采集到的电压为3.3V左右,当按键按下时,电压变为0.2V左右。

ready to OS start
sdk ver:Hi3861V100R001C00SPC025 2020-09-03 18:10:00
formatting spiffs...
FileSystem mount ok.
wifi init success!

hiview init success.00 00:00:00 0 132 D 0/HIVIEW: log limit init success.
00 00:00:00 0 132 I 1/SAMGR: Bootstrap core services(count:3).
00 00:00:00 0 132 I 1/SAMGR: Init service:0x4b0128 TaskPool:0xe4b3c
00 00:00:00 0 132 I 1/SAMGR: Init service:0x4b0134 T***************************************
************Yuchuan ADC Example********
***************************************
YuchuanThread get voltage is : 3.326V
askPool:0xe4b5c
00 00:00:00 0 132 I 1/SAMGR: Init service:0x4b0600 TaskPool:0xe4b7c
00 00:00:00 0 164 I 1/SAMGR: Init service 0x4b0134 <time: 0ms> success!
00 00:00:00 0 64 I 1/SAMGR: Init service 0x4b0128 <time: 0ms> success!
00 00:00:00 0 8 I 1/SAMGR: Init service 0x4b0600 <time: 0ms> success!
00 00:00:00 0 8 I 1/SAMGR: Initialized all core system services!
00 00:00:00 0 64 I 1/SAMGR: Bootstrap system and application services(count:0).
00 00:00:00 0 64 I 1/SAMGR: Initialized all system and application services!
00 00:00:00 0 64 I 1/SAMGR: Bootstrap dynamic registered services(count:0).
***************************************
************Yuchuan ADC Example********
***************************************
YuchuanThread get voltage is : 3.331V
***************************************
************Yuchuan ADC Example********
***************************************
YuchuanThread get voltage is : 3.329V
***************************************
************Yuchuan ADC Example********
***************************************
YuchuanThread get voltage is : 3.331V
***************************************
************Yuchuan ADC Example********
***************************************
YuchuanThread get voltage is : 3.333V
***************************************
************Yuchuan ADC Example********
***************************************
YuchuanThread get voltage is : 3.329V
***************************************
************Yuchuan ADC Example********
***************************************
YuchuanThread get voltage is : 3.331V
***************************************
************Yuchuan ADC Example********
***************************************
YuchuanThread get voltage is : 0.248V
***************************************
************Yuchuan ADC Example********
***************************************
YuchuanThread get voltage is : 0.246V
***************************************
************Yuchuan ADC Example********
***************************************
YuchuanThread get voltage is : 0.250V
***************************************
************Yuchuan ADC Example********
***************************************
YuchuanThread get voltage is : 0.248V
***************************************
************Yuchuan ADC Example********
***************************************
YuchuanThread get voltage is : 3.329V
***************************************
************Yuchuan ADC Example********
***************************************
YuchuanThread get voltage is : 3.329V
***************************************

(完)