1. AdcRead()
3.实验步骤
步骤一:
hi3861_hdu_iot_application/src/vendor/hisilicon/hispark_pegasus/demo/lth1550_demo文件
夹复制到hi3861_hdu_iot_application/src/applications/sample/wifi-iot/app/目录下。
步骤二:修改applications/sample/wifi-iot/app/目录下的BUILD.gn,在features字段中添加
lth1550_demo:lth1550_control。注:第一个lth1550_demo指的是需要编译的工程目录,第二个
lth1550_control指的是applications/sample/wifi-iot/app/ lth1550_demo/BUILD.gn文件中的静态
库,名称为lth1550_control。
import("//build/lite/config/component/lite_component.gni")
lite_component("app") {
features = [ "lth1550_demo:lth1550_control", ]
}
步骤三:原理:ADC即模拟-数字转换器,可以将连续变化的模拟信号转换为数字信号,进而使用数字电
路进行处理,称之为数字信号处理。
从硬件原理图可以分析出左红外对管对应的是GPIO12,右红外对管对应的是GPIO07,通过ADC通道
从传感器读回数据,核心代码如方下所示:
/* ADC的相关API接口 Related API interfaces of ADC */
#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "iot_gpio_ex.h"
#include "iot_gpio.h"
#include "iot_adc.h"
#include "hi_adc.h"
#include "iot_watchdog.h"
#include "iot_errno.h"
void Lth1550Init(void)
{
/* 红外对管对应的GPIO,左:ADC0_GPIO12,右:ADC3_GPIO07 */
/* GPIO corresponding to infrared tube, left: ADC0_ GPIO12, right: ADC3_
IO07 */
IoTGpioInit(IOT_IO_NAME_GPIO_7);
/* 设置GPIO07的管脚复用关系为GPIO */
/* Set the pin reuse relationship of GPIO07 to GPIO */
IoSetFunc(IOT_IO_NAME_GPIO_7, IOT_IO_FUNC_GPIO_7_GPIO);
IoTGpioSetDir(IOT_IO_NAME_GPIO_7, IOT_GPIO_DIR_IN);
IoTGpioInit(IOT_IO_NAME_GPIO_12);
IoSetFunc(IOT_IO_NAME_GPIO_12, IOT_IO_FUNC_GPIO_12_GPIO);
IoTGpioSetDir(IOT_IO_NAME_GPIO_12, IOT_GPIO_DIR_IN);
}
void GetInfraredData(IotAdcChannelIndex idx)
{
unsigned short data = 0;
int ret = 0;
/* ADC_Channal_6 自动识别模式 CNcomment:4次平均算法模式 CNend 0xff */
/* ADC_ Channal_ 6 Automatic recognition mode CNcomment: 4 times average
gorithm mode CNend 0xff */
ret = AdcRead(idx, &data, IOT_ADC_EQU_MODEL_4, IOT_ADC_CUR_BAIS_DEFAULT,
ff);
if (ret != IOT_SUCCESS) {
printf("hi_adc_read failed\n");
}
if (IOT_ADC_CHANNEL_0 == idx) {
printf("Left ADC value is %d \r\n", data);
} else if (IOT_ADC_CHANNEL_3 == idx) {
printf("Right ADC value is %d \r\n", data);
}
}
static void adcTask(void)
{
Lth1550Init();步骤四:点击DevEco Device Tool工具“Rebuild”按键,具体编译步骤参考“1.4章节 运行第一个程序
helloworld”
步骤五:点击DevEco Device Tool工具“Upload”按键,等待提示(出现Connecting,please reset
device...),手动进行开发板复位(按下开发板reset键),将程序烧录到开发板中。具体烧录步骤
参考“1.4章节 运行第一个程序helloworld”。
while (1) {
GetInfraredData(IOT_ADC_CHANNEL_3);
GetInfraredData(IOT_ADC_CHANNEL_0);
usleep(20); // wait 20 us
}
}
void ADCExampleEntry(void)
{
osThreadAttr_t attr;
IoTWatchDogDisable();
attr.name = "adcTask";
attr.attr_bits = 0U;
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
attr.stack_size = 5 * 1024; // 堆栈大小5*1024,stack size 5*1024
attr.priority = osPriorityNormal;
if (osThreadNew((osThreadFunc_t)adcTask, NULL, &attr) == NULL) {
printf("[LSM6DSTask] Failed to create LSM6DSTask!\n");
}
}
APP_FEATURE_INIT(ADCExampleEntry);
步骤四:点击DevEco Device Tool工具“Rebuild”按键,具体编译步骤参考“1.4章节 运行第一个程序
helloworld”
步骤五:点击DevEco Device Tool工具“Upload”按键,等待提示(出现Connecting,please reset
device...),手动进行开发板复位(按下开发板reset键),将程序烧录到开发板中。具体烧录步骤
参考“1.4章节 运行第一个程序helloworld”。
二.弯曲传感器学习
弯曲传感器也称为弯曲传感器,是一种低成本、易于使用的传感器,用于测量偏转或弯曲量。
由于被纳入 Nintendo Power Glove,它在 20 世纪 90 年代广受欢迎。从那时起,人们就一直将其用作测量关节运动的测角仪、门传感器、检测墙壁的保险杠开关以及机器人抓手上的压力传感器。
读取柔性传感器的最简单方法是将其与静态电阻结合起来形成分压器,该分压器产生可由微控制器的模数转换器读取的可变电压。
需要注意的是,您测量的输出电压是下拉电阻两端的电压降,而不是柔性传感器两端的电压降。
我们可以使用这个方程来计算输出电压(Vo)。
在此配置中,输出电压随着弯曲半径的增加而降低。
例如,使用 5V 电源和 47K 下拉电阻,当传感器平坦(0°)时,电阻相对较低(约 25k)。这会产生以下输出电压: