123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- #include <stdio.h>
- #include "ohos_init.h"
- #include "iot_errno.h"
- #include "iot_gpio.h"
- #include "iot_gpio_ex.h"
- #include "iot_i2c.h"
- #include <math.h>
- #include <string.h>
- #include <unistd.h>
- #include <stdint.h>
- #include "ohos_init.h"
- #include <stdlib.h>
- #include <cmsis_os2.h>
- #include <stdbool.h>
- #define WIFI_IOT_IO_FUNC_GPIO_0_I2C1_SDA 6
- #define WIFI_IOT_IO_FUNC_GPIO_1_I2C1_SCL 6
- #define WIFI_IOT_I2C_IDX_1 1
- #define BH1750_ADDR 0x23
- int BoardInit(void);
- int E53SC1ReadData(float *data);
- static void GpioInit(void)
- {
- printf("GPIO init...\r\n");
- IoTGpioInit(0);
- IoTGpioSetFunc(0, WIFI_IOT_IO_FUNC_GPIO_0_I2C1_SDA); // GPIO_0复用为I2C1_SDA
- IoTGpioInit(1);
- IoTGpioSetFunc(1, WIFI_IOT_IO_FUNC_GPIO_1_I2C1_SCL); // GPIO_1复用为I2C1_SCL
- IoTI2cInit(WIFI_IOT_I2C_IDX_1, 400000); /* baudrate: 400kbps */
- printf("GPIO init...OK\r\n");
- }
- static int InitBH1750(void)
- {
- int ret;
- uint8_t send_data[1] = {0x01};
- ret = IoTI2cWrite(WIFI_IOT_I2C_IDX_1, (BH1750_ADDR << 1) | 0x00, send_data, 1);
- if (ret != 0) {
- printf("===== Error: I2C write ret = 0x%x! =====\r\n", ret);
- return -1;
- }
- return 0;
- }
- static int StartBH1750(void)
- {
- int ret;
- uint8_t send_data[1] = {0x10};
- ret = IoTI2cWrite(WIFI_IOT_I2C_IDX_1, (BH1750_ADDR << 1) | 0x00, send_data, 1);
- if (ret != 0) {
- printf("===== Error: I2C write ret = 0x%x! =====\r\n", ret);
- return -1;
- }
- return 0;
- }
- int BoardInit(void)
- {
- int ret;
- GpioInit();
- ret = InitBH1750();
- // if (ret != 0) {
- // return -1;
- // }
- return 0;
- }
- int BH1750ReadData(float* data)
- {
- int ret;
- int result;
- ret = StartBH1750(); // 启动传感器采集数据
- if (ret != 0) {
- printf("Start BH1750 failed!\r\n");
- return -1;
- }
- usleep(180000);
- uint8_t recv_data[2] = {0};
- ret = IoTI2cRead(WIFI_IOT_I2C_IDX_1, (BH1750_ADDR << 1) | 0x01, recv_data, 2); // 读取传感器数据
- if (ret != 0) {
- return -1;
- }
- printf("recevie data is :%x,%x\r\n",recv_data[0],recv_data[1]);
- *data = (float)(((recv_data[0] << 8) + recv_data[1]) / 1.2); //合成数据,即光照数据
- return 0;
- }
- static void LEDPWM(void)
- {
- int ret;
- int temp;
- float Lux;
- ret = BoardInit();
- if (ret != 0) {
- printf("Board Init failed!\r\n");
- return;
- }
- osDelay(300);
- temp = BH1750ReadData(&Lux);
- if (temp != 0) {
- printf("Read Data failed!\r\n");
- return;
- }
- printf("Lux data:%.2f\r\n", Lux);
- }
- APP_FEATURE_INIT(LEDPWM);
|