8000 Thu Feb 23 14:35:24 CST 2017 · esp32vn/esp32-snippets@a8242b9 · GitHub
[go: up one dir, main page]

Skip to content

Commit a8242b9

Browse files
author
kolban
committed
Thu Feb 23 14:35:24 CST 2017
1 parent 2edf093 commit a8242b9

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

hardware/ambient_light/bh1750fvi.c

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#include <driver/i2c.h>
2+
#include <esp_log.h>
3+
#include <freertos/FreeRTOS.h>
4+
#include <freertos/task.h>
5+
#include <math.h>
6+
7+
#include "sdkconfig.h"
8+
9+
#define PIN_SDA 21
10+
#define PIN_CLK 22
11+
#define I2C_ADDRESS 0x23
12+
13+
// No active state
14+
#define BH1750_POWER_DOWN 0x00
15+
16+
// Wating for measurment command
17+
#define BH1750_POWER_ON 0x01
18+
19+
// Reset data register value - not accepted in POWER_DOWN mode
20+
#define BH1750_RESET 0x07
21+
22+
// Start measurement at 1lx resolution. Measurement time is approx 120ms.
23+
#define BH1750_CONTINUOUS_HIGH_RES_MODE 0x10
24+
25+
// Start measurement at 0.5lx resolution. Measurement time is approx 120ms.
26+
#define BH1750_CONTINUOUS_HIGH_RES_MODE_2 0x11
27+
28+
// Start measurement at 4lx resolution. Measurement time is approx 16ms.
29+
#define BH1750_CONTINUOUS_LOW_RES_MODE 0x13
30+
31+
// Start measurement at 1lx resolution. Measurement time is approx 120ms.
32+
// Device is automatically set to Power Down after measurement.
33+
#define BH1750_ONE_TIME_HIGH_RES_MODE 0x20
34+
35+
// Start measurement at 0.5lx resolution. Measurement time is approx 120ms.
36+
// Device is automatically set to Power Down after measurement.
37+
#define BH1750_ONE_TIME_HIGH_RES_MODE_2 0x21
38+
39+
// Start measurement at 1lx resolution. Measurement time is approx 120ms.
40+
// Device is automatically set to Power Down after measurement.
41+
#define BH1750_ONE_TIME_LOW_RES_MODE 0x23
42+
43+
static char tag[] = "bh1750fvi";
44+
45+
#undef ESP_ERROR_CHECK
46+
#define ESP_ERROR_CHECK(x) do { esp_err_t rc = (x); if (rc != ESP_OK) { ESP_LOGE("err", "esp_err_t = %d", rc); assert(0 && #x);} } while(0);
47+
48+
void task_bh1750fvi(void *ignore) {
49+
ESP_LOGD(tag, ">> bh1750fvi");
50+
i2c_config_t conf;
51+
conf.mode = I2C_MODE_MASTER;
52+
conf.sda_io_num = PIN_SDA;
53+
conf.scl_io_num = PIN_CLK;
54+
conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
55+
conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
56+
conf.master.clk_speed = 100000;
57+
ESP_ERROR_CHECK(i2c_param_config(I2C_NUM_0, &conf));
58+
ESP_ERROR_CHECK(i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, 0, 0, 0));
59+
60+
uint8_t data[2];
61+
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
62+
i2c_master_start(cmd);
63+
i2c_master_write_byte(cmd, (I2C_ADDRESS << 1) | I2C_MASTER_WRITE, 1);
64+
i2c_master_write_byte(cmd, BH1750_CONTINUOUS_HIGH_RES_MODE, 1);
65+
i2c_master_stop(cmd);
66+
i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000/portTICK_PERIOD_MS);
67+
i2c_cmd_link_delete(cmd);
68+
69+
while(1) {
70+
cmd = i2c_cmd_link_create();
71+
ESP_ERROR_CHECK(i2c_master_start(cmd));
72+
ESP_ERROR_CHECK(i2c_master_write_byte(cmd, (I2C_ADDRESS << 1) | I2C_MASTER_READ, 1));
73+
i2c_master_read_byte(cmd, data, 0);
74+
i2c_master_read_byte(cmd, data+1, 1);
75+
ESP_ERROR_CHECK(i2c_master_stop(cmd));
76+
ESP_ERROR_CHECK(i2c_master_cmd_begin(I2C_NUM_0, cmd, 100/portTICK_PERIOD_MS));
77+
i2c_cmd_link_delete(cmd);
78+
int value = ((data[0] << 8) | data[1])/1.2;
79+
ESP_LOGD(tag, "lx value: %d", value);
80+
vTaskDelay(200/portTICK_PERIOD_MS);
81+
}
82+
vTaskDelete(NULL);
83+
} // task_hmc5883l

0 commit comments

Comments
 (0)
0