锐单电子商城 , 一站式电子元器件采购平台!
  • 电话:400-990-0325

Pegasus读取传感器AD的值

时间:2022-09-30 01:30:00 传感器ad值成了10402cb010传感器cb传感器

这篇文章用的是 MQ3传感器,FSR电阻式薄膜力传感器

VCC-->3.3

DO-->开发板

当检测到压力或酒精时,输出电压为0.3V左右。

Sensor.c

#include  #include  #include   #include "ohos_init.h" #include "cmsis_os2.h" #include "iot_gpio.h" #include "iot_errno.h"  #include "iot_adc.h" #include "hi_time.h"   #define GPIO11 11 #define GPIO12 12 #define TASK_STAK_SIZE    (1024*10)  void get_Sensor_value (void) {     float val_mq3,val_fsr;     unsigned short data_MQ3 = 0,data_fsr = 0;      unsigned int ret=0,qut=0;     // ADC_Channal_2(gpio5)  自动识别模式  CNcomment:四平均算法模式 CNend     ret = AdcRead(IOT_ADC_CHANNEL_5, &data_MQ3,                                IOT_ADC_EQU_MODEL_4, IOT_ADC_CUR_BAIS_DEFAULT, 0xff);    if (ret != HI_ERR_SUCCESS)      {         printf("ADC_5 Read Fail\n");     }     val_mq3 = data_MQ3 * 1.8 *4 / 4096; /* --- vlt * 1.8* 4 / 4096.0 ---将码字转换为电压 */    printf("MQ3_Value is %lf\r\n", val_mq3);     qut = AdcRead(IOT_ADC_CHANNEL_0, &data_fsr, IOT_ADC_EQU_MODEL_4, IOT_ADC_CUR_BAIS_DEFAULT,  0xF0);     if (qut != HI_ERR_SUCCESS)      {         printf("ADC_fsr Read Fail\n");     }     val_fsr = (float)(data_fsr * 1.8 *4 / 4096); /* --- vlt * 1.8* 4 / 4096.0 ---将码字转换为电压 */     printf("fsr_Value is %lf\r\n", val_fsr);    }   void SensorTask(void* parame) {     (void)parame;     printf("start test Sensor\r\n");     unsigned int time = 1000;     while (1) {         hi_sleep(time);        get_Sensor_value();             } }   static void SensorDemo(void) {     osThreadAttr_t attr;      attr.name = "SensorTask";     attr.attr_bits = 0U;     attr.cb_mem = NULL;     attr.cb_size = 0U;     attr.stack_mem = NULL;     attr.stack_size = TASK_STAK_SIZE;     attr.priority = osPriorityNormal;      if (osThreadNew(SensorTask, NULL, &attr) == NULL) {         printf("[SensorDemo] Falied to create SensorTask!\n");     } }  APP_FEATURE_INIT(SensorDemo);  

build.gn

static_library("Sensor_demo") {   sources = [     "Sensor.c",     "hal_iot_adc.c",   ]    include_dirs = [     "//utils/native/lite/include",     "//kernel/liteos_m/kal/cmsis",     "//base/iot_hardware/peripheral/interfaces/kits",   ] } 
锐单商城拥有海量元器件数据手册IC替代型号,打造电子元器件IC百科大全!

相关文章