HarmonyOS小熊派 | HarmonyOS传感器驱动开发--E53_SC1读取光照强度

举报
Yuchuan 发表于 2021/07/20 16:44:44 2021/07/20
【摘要】 HarmonyOS传感器驱动开发--E53_SC1读取光照强度

BearPi-HM_Nano开发板传感器驱动开发——E53_SC1读取光照强度

本示例将演示如何在BearPi-HM_Nano开发板上使用E53_SC1读取温度 、湿度、光照强度,当光照强度过低时,开启补光灯补光。

BearPi-HM_Nano

E53_SC1 API分析

本案例主要使用了以下API完成温度 、湿度、光照强度读取

1、InitE53SC1()

/***************************************************************
* 函数名称: InitE53SC1
* 说    明: 初始化E53_SC1
* 参    数: 无
* 返 回 值: 无
***************************************************************/
void InitE53SC1(void)

描述:

初始化E53_SC1

2、ReadDataE53SC1()

/***************************************************************
* 函数名称: ReadDataE53SC1
* 说    明: 测量光照强度
* 参    数: 无
* 返 回 值: 光照强度
***************************************************************/
float ReadDataE53SC1(void)

描述: 读取光照强度

3、SetLightStatus()

/***************************************************************
* 函数名称: SetLightStatus
* 说    明: 灯状态设置
* 参    数: status,ENUM枚举的数据
*									OFF,光灯
*									ON,开灯
* 返 回 值: 无
***************************************************************/
void SetLightStatus(E53_SC1_Status_ENUM status)

描述:

控制补光灯开关 参数:

名字 描述
status ON开,OFF关闭.

硬件设计

本案例将用到 E53_SC1 智慧路灯扩展板与 BearPi-HM_Nano 开发板,其中E53_SC1扩展板原理图如下,光照强度传感器BH1750是通过I2C来驱动,灯是通过GPIO_7来控制。

E53_SC1 智慧路灯扩展板与 BearPi-HM_Nano 开发板安装

软件设计

主要代码分析

首先调用 InitE53SC1() 函数初始化E53_SC1所接的引脚的功能,然后循环调用 ReadDataE53SC1()  函数读取光照强度并通过串口打印出来,当光照强度过低时,开启灯

yuchuan_e53_sc1.c

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

#include "ohos_init.h"
#include "cmsis_os2.h"
#include "e53_sc1.h"

#define TASK_CB_SIZE 0U
#define TASK_STACK_SIZE 1024 * 4
#define TASK_PRIORITY 25

// 控制E53接口的函数
static void YuchuanE53SC1Task(void)
{
    float lux;
    // 初始化E53接口
    InitE53SC1();
    // 每间隔一段时间答应获取到的光照强度数据
    while (1)
    {
        printf("======================================================\r\n");
        printf("*************YuchuanHuaying E53_SC1_Example***********\r\n");
        printf("======================================================\r\n");

        // 读取E53接口BH1750光照传感器的数据
        lux = ReadDataE53SC1();
        printf("YuchuanHuaying BH1750 Get Lux Data : %.2f\r\n", lux);
        if (lux < 50)
        {
            // 设置灯的状态为开,开灯
            SetLightStatus(ON);
        }
        else
        {
            // 设置灯的状态为关,关灯
            SetLightStatus(OFF);
        }
        usleep(3000000);
    }
}

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

    threadAttr.name = "YuchuanE53SC1Task";
    if (osThreadNew((osThreadFunc_t)YuchuanE53SC1Task, NULL, &threadAttr) == NULL)
    {
        /* code */
    }
}

APP_FEATURE_INIT(YuchuanE53SC1Entry);

src/e53_sc1.c

/*
 * 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.
 */

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

#include "e53_sc1.h"
#include "iot_gpio.h"
#include "iot_gpio_ex.h"
#include "iot_i2c.h"
#include "cmsis_os2.h"

#define IOT_IO_LIGHT_GPIO_7 7
#define IOT_IO_I2C1_SDA_GPIO_0 0
#define IOT_IO_I2C1_SCL_GPIO_1 1
#define IOT_IO_I2C_IDX_1 1

/***************************************************************
* 函数名称: IoInitE53SC1
* 说    明: E53_SC1_GPIO初始化
* 参    数: 无
* 返 回 值: 无
***************************************************************/
static void IoInitE53SC1(void)
{
    // 初始化扩展板LED灯的GPIO
    IoTGpioInit(IOT_IO_LIGHT_GPIO_7);
    // 设置GPIO为输出方向
    IoTGpioSetDir(IOT_IO_LIGHT_GPIO_7, IOT_GPIO_DIR_OUT);
    // 设置GPIO的复用功能为普通GPIO
    IoTGpioSetFunc(IOT_IO_LIGHT_GPIO_7, IOT_GPIO_FUNC_GPIO_7_GPIO);

    // 初始化扩展板的I2C_SDA GPIO
    IoTGpioInit(IOT_IO_I2C1_SDA_GPIO_0);
    // 设置GPIO引脚为输出方向
    IoTGpioSetDir(IOT_IO_I2C1_SDA_GPIO_0, IOT_GPIO_DIR_OUT);
    // 设置GPIO引脚的复用功能
    IoTGpioSetFunc(IOT_IO_I2C1_SDA_GPIO_0, IOT_GPIO_FUNC_GPIO_0_I2C1_SDA);

    // 初始化扩展板的I2C_SCL GPIO
    IoTGpioInit(IOT_IO_I2C1_SCL_GPIO_1);
    // 设置GPIO引脚为输出方向
    IoTGpioSetDir(IOT_IO_I2C1_SCL_GPIO_1, IOT_GPIO_DIR_OUT);
    // 设置GPIO引脚的复用功能
    IoTGpioSetFunc(IOT_IO_I2C1_SCL_GPIO_1, IOT_GPIO_FUNC_GPIO_1_I2C1_SCL);

    // 初始化I2C
    /* baudrate: 400kbps */
    IoTI2cInit(IOT_IO_I2C_IDX_1, 400000);
}

/***************************************************************
* 函数名称: InitBH1750
* 说    明: 写命令初始化BH1750
* 参    数: 无
* 返 回 值: 无
***************************************************************/
static void InitBH1750(void)
{
    uint8_t send_data[1] = {0x01};
    // 通过I2C写数据
    // unsigned int IoTI2cWrite(unsigned int id, unsigned short deviceAddr, const unsigned char *data, unsigned int dataLen);
    IoTI2cWrite(IOT_IO_I2C_IDX_1, (BH1750_ADDR << 1) | 0x00, send_data, 1);
}

/***************************************************************
* 函数名称: InitE53SC1
* 说    明: 初始化E53_SC1
* 参    数: 无
* 返 回 值: 无
***************************************************************/
void InitE53SC1(void)
{
    // 初始化 E53_SC1 接口的GPIO引脚
    IoInitE53SC1();
    // 初始化 BH1750传感器
    InitBH1750();
}

/***************************************************************
* 函数名称: StartBH1750
* 说    明: 启动BH1750
* 参    数: 无
* 返 回 值: 无
***************************************************************/
static void StartBH1750(void)
{
    uint8_t send_data[1] = {0x10};
    // unsigned int IoTI2cWrite(unsigned int id, unsigned short deviceAddr, const unsigned char *data, unsigned int dataLen);
    IoTI2cWrite(IOT_IO_I2C_IDX_1, (BH1750_ADDR << 1) | 0x00, send_data, 1);
}

/***************************************************************
* 函数名称: ReadDataE53SC1
* 说    明: 测量光照强度
* 参    数: 无
* 返 回 值: 光照强度
***************************************************************/
float ReadDataE53SC1(void)
{
    int ret;
    // 启动传感器采集数据
    StartBH1750();
    // 延时2s
    usleep(2000000);

    uint8_t recv_data[2] = {0};
    // 读取传感器数据
    // unsigned int IoTI2cRead(unsigned int id, unsigned short deviceAddr, unsigned char *data, unsigned int dataLen);
    IoTI2cRead(IOT_IO_I2C_IDX_1, (BH1750_ADDR << 1) | 0x01, recv_data, 2);
    // //合成数据,即光照数据
    ret = (recv_data[0] << 8) + recv_data[1];
    return (float)(ret / 1.2);
}

/***************************************************************
* 函数名称: SetLightStatus
* 说    明: 灯状态设置
* 参    数: status,ENUM枚举的数据
*									OFF,光灯
*									ON,开灯
* 返 回 值: 无
***************************************************************/
void SetLightStatus(E53_SC1_Status_ENUM status)
{
    if (status == ON)
    {
        // 设置GPIO7 的引脚为高电平输出,点亮LED灯
        // unsigned int IoTGpioSetOutputVal(unsigned int id, IotGpioValue val);
        IoTGpioSetOutputVal(IOT_IO_LIGHT_GPIO_7, IOT_GPIO_VALUE1);
    }
    if (status == OFF)
    {
        // 设置GPIO 的引脚为低电平输出,熄灭LED灯
        IoTGpioSetOutputVal(IOT_IO_LIGHT_GPIO_7, IOT_GPIO_VALUE0);
    }
}

include/e53_sc1.h

/*
 * 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.
 */

#ifndef __E53_SC1_H__
#define __E53_SC1_H__

#define BH1750_ADDR 0x23

typedef enum
{
    OFF = 0,
    ON
}E53_SC1_Status_ENUM;

// 初始化E53_SC1 接口
void InitE53SC1(void);
// 获取光照传感器光照强度的数据
float ReadDataE53SC1(void);

#endif /* __E53_SC1_H__ */

编译调试

//applications/sample/BearPi/BearPi-HM_Nano/sample/C3_YuchuanE53SC1

static_library("yuchuanE53SC1"){
    sources = [
        "yuchuan_e53_sc1.c",
        "src/e53_sc1.c",
    ]

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

修改 BUILD.gn 文件

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

运行结果

示例代码编译烧录代码后,按下开发板的RESET按键,通过串口助手查看日志,会打印光照强度信息。用手遮住扩展板,补光灯会自动开启。

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

hiview init success.======================================================
*************YuchuanHuaying E53_SC1_Example***********
======================================================
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:0x4b0a48 TaskPool:0xe4b3c
00 00:00:00 0 132 I 1/SAMGR: Init service:0x4b0a54 TaskPool:0xe4b5c
00 00:00:00 0 132 I 1/SAMGR: Init service:0x4b0f20 TaskPool:0xe4b7c
00 00:00:00 0 164 I 1/SAMGR: Init service 0x4b0a54 <time: 0ms> success!
00 00:00:00 0 64 I 1/SAMGR: Init service 0x4b0a48 <time: 0ms> success!
00 00:00:00 0 8 I 1/SAMGR: Init service 0x4b0f20 <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).
YuchuanHuaying BH1750 Get Lux Data : 16.67
======================================================
*************YuchuanHuaying E53_SC1_Example***********
======================================================
YuchuanHuaying BH1750 Get Lux Data : 20.83
======================================================
*************YuchuanHuaying E53_SC1_Example***********
======================================================
YuchuanHuaying BH1750 Get Lux Data : 20.00
======================================================
*************YuchuanHuaying E53_SC1_Example***********
======================================================
YuchuanHuaying BH1750 Get Lux Data : 20.83
======================================================
*************YuchuanHuaying E53_SC1_Example***********
======================================================
YuchuanHuaying BH1750 Get Lux Data : 20.83
======================================================
*************YuchuanHuaying E53_SC1_Example***********
======================================================
YuchuanHuaying BH1750 Get Lux Data : 20.83
======================================================
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。