-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathBH1750.c
422 lines (318 loc) · 11 KB
/
BH1750.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/* ************************************************************************
*
* functions for BH1750FVI (ambient light sensor, I2C bus)
*
* (c) 2024 by Markus Reschke
*
* ************************************************************************ */
/*
* hints:
* - pin assignment
* SCL I2C: SCL
* SDA I2C: SDA
* ADDR Gnd or Vcc
* - I2C clock modes: standard (100kHz) and fast (400kHz)
* - Vcc 3.3V
* I2C pull-up resistors to 3.3V should be ok for a 5V ATmega
*/
/* local includes */
#include "config.h" /* global configuration */
#ifdef HW_BH1750
/*
* local constants
*/
/* source management */
#define BH1750_C
/* operation modes */
#define MODE_MANUAL 0 /* manual mode */
#define MODE_AUTO 1 /* automatic mode */
/* I2C addreses */
#define BH1750_I2C_ADDR_0 0b00100011 /* ADDR=0 0x23 */
#define BH1750_I2C_ADDR_1 0b01011100 /* ADDR=1 0x5c */
/*
* instruction set
*/
/* power down */
#define BH1750_POWER_DOWN 0b00000000
/* power on (and wait for measurement command) */
#define BH1750_POWER_UP 0b00000001
/* reset data register (not in power down mode) */
#define BH1750_RESET 0b00000111
/* run measurement in continuous high resolution mode 1 (1 lx) */
#define BH1750_CONT_HIRES_1 0b00010000
/* run measurement in continuous high resolution mode 2 (0.5 lx) */
#define BH1750_CONT_HIRES_2 0b00010001
/* run measurement in continuous low resolution mode (4 lx) */
#define BH1750_CONT_LOWRES 0b00010011
/* run measurement in one-time high resolution mode 1 (1 lx) */
#define BH1750_SINGLE_HIRES_1 0b00100000
/* run measurement in one-time high resolution mode 2 (0.5 lx) */
#define BH1750_SINGLE_HIRES_2 0b00100001
/* run measurement in one-time low resolution mode (4 lx) */
#define BH1750_SINGLE_LOWRES 0b00100011
/* change measurement time, high bits (bits 7-5) */
#define BH1750_TIME_HIGH 0b01000000
/* change measurement time, low bits (bits 4-0) */
#define BH1750_TIME_LOW 0b01100000
/*
* include header files
*/
/* local includes */
#include "common.h" /* common header file */
#include "variables.h" /* global variables */
#include "functions.h" /* external functions */
/* ************************************************************************
* low level functions for I2C interface
* ************************************************************************ */
/*
* send command to BH1750
*
* requires:
* - Command
*
* returns:
* - 1 on success
* - 0 on any problem
*/
uint8_t BH1750_SendCommand(uint8_t Command)
{
uint8_t Flag = 0; /* return value */
uint8_t OldTimeout; /* old ACK timeout */
OldTimeout = I2C.Timeout; /* save old timeout */
if (I2C_Start(I2C_START) == I2C_OK) /* start */
{
I2C.Byte = BH1750_I2C_ADDR << 1; /* address (7 bits) & write (0) */
I2C.Timeout = 1; /* ACK timeout 10µs */
/* send address & write bit, expect ACK from BH1750 */
if (I2C_WriteByte(I2C_ADDRESS) == I2C_ACK) /* address BH1750 */
{
I2C.Byte = Command; /* set command */
/* send command, expect ACK from BH1750 */
if (I2C_WriteByte(I2C_DATA) == I2C_ACK) /* send data */
{
Flag = 1; /* signal success */
}
}
}
I2C_Stop(); /* stop */
I2C.Timeout = OldTimeout; /* restore old timeout */
return Flag;
}
/*
* read light intensity value from BH1750
*
* requires:
* - Value: pointer to raw value
*
* returns:
* - 1 on success
* - 0 on any problem
*/
uint8_t BH1750_ReadValue(uint16_t *Value)
{
uint8_t Flag = 0; /* return value */
uint8_t HighByte; /* high byte */
uint16_t RawValue; /* raw value */
uint8_t OldTimeout; /* old ACK timeout */
OldTimeout = I2C.Timeout; /* save old timeout */
if (I2C_Start(I2C_START) == I2C_OK) /* start */
{
/* address (7 bits) & read (1) */
I2C.Byte = (BH1750_I2C_ADDR << 1) | 0b00000001;
I2C.Timeout = 1; /* ACK timeout 10µs */
/* send address & read bit, expect ACK from BH1750 */
if (I2C_WriteByte(I2C_ADDRESS) == I2C_ACK) /* address BH1750 */
{
/* read high byte and ACK */
if (I2C_ReadByte(I2C_ACK) == I2C_OK) /* read first byte */
{
HighByte = I2C.Byte; /* save byte */
/* read low byte and NACK */
if (I2C_ReadByte(I2C_NACK) == I2C_OK) /* read second byte */
{
/* pre-process data */
RawValue = HighByte; /* copy high byte */
RawValue <<= 8; /* and shift to MSB */
RawValue |= I2C.Byte; /* copy low byte */
*Value = RawValue; /* save result */
Flag = 1; /* signal success */
}
}
}
}
I2C_Stop(); /* stop */
I2C.Timeout = OldTimeout; /* restore old timeout */
return Flag;
}
/* ************************************************************************
* high level functions
* ************************************************************************ */
/*
* measurement modes and times:
* - resolution mode resolution typ. time max. time
* high resolution 1 1 lx 120 ms 180 ms
* high resolution 2 0.5 lx 120 ms 180 ms
* low resolution 4 lx 16 ms 24 ms
* - modes
* continuously: stay in power on mode
* one-time: automaticly power down after measurement
*/
/*
* BH1750: read light intensity
*
* requires:
* - Value: pointer to light intensity in Lux
* - Scale: pointer to scale factor / decimal places
*
* returns:
* - 1 on success
* - 0 on any problem
*/
uint8_t BH1750_ReadLux(uint32_t *Value, uint8_t *Scale)
{
uint8_t Flag = 0; /* return value */
uint16_t RawValue; /* raw value */
uint32_t Temp; /* temporary value */
/*
* measure light intensity
*/
/* send command: measure in continuous high resolution mode 1 */
if (BH1750_SendCommand(BH1750_CONT_HIRES_1) == 1) /* done */
{
/* conversion time 120-180ms */
MilliSleep(180); /* wait 180ms */
/* read raw value */
if (BH1750_ReadValue(&RawValue) == 1) /* done */
{
Flag = 1; /* signal success */
}
}
/*
* convert raw value to Lux
*/
Temp = RawValue; /* copy raw value */
Temp *= 100; /* scale to 10^-2 */
/* apply accuracy factor (/1.2) */
Temp /= 12; /* /12 (scaled to 10^-1) */
*Value = Temp; /* save Lux value */
*Scale = 1; /* 1 decimal place (10^-1) */
return Flag;
}
/* ************************************************************************
* tool
* ************************************************************************ */
/*
* BH1750 tool
* - reads and displays light intensity (in Lux)
*/
void BH1750_Tool(void)
{
uint8_t Flag = 1; /* loop control */
uint8_t Test; /* key / feedback */
uint8_t Mode = MODE_MANUAL; /* operation mode */
uint16_t Timeout = 0; /* timeout for user feedback */
uint8_t Scale; /* Lux scale / decimal places */
uint32_t Value; /* Lux value */
/*
* display info
*/
LCD_Clear();
#ifdef UI_COLORED_TITLES
Display_ColoredEEString(BH1750_str, COLOR_TITLE); /* display: BH1750 */
#else
Display_EEString(BH1750_str); /* display: BH1750 */
#endif
LCD_CharPos(1, 2); /* move to line #2 */
Display_EEString(Start_str); /* display: Start */
/*
* power on BH1750
* - BH1750 enters power down mode after Vcc is applied
*/
/* send command: power up */
if (BH1750_SendCommand(BH1750_POWER_UP) == 0) /* can't power on */
{
/* inform user */
LCD_ClearLine2(); /* clear line #2 */
Display_EEString(Error_str); /* display: Error */
WaitKey(); /* wait for user feedback */
Flag = 0; /* don't proceed */
}
/*
* processing loop
*/
while (Flag)
{
/*
* user input
*/
/* wait for user input */
Test = TestKey(Timeout, CURSOR_BLINK | CHECK_KEY_TWICE | CHECK_BAT);
if (Test == KEY_LONG) /* long key press */
{
/* display mode in line #1 */
LCD_ClearLine(1); /* clear line #1 */
LCD_CharPos(1, 1); /* move to line #1 */
#ifdef UI_COLORED_TITLES
Display_ColoredEEString_Space(BH1750_str, COLOR_TITLE);
#else
Display_EEString_Space(BH1750_str);
#endif
/* change mode */
if (Mode == MODE_MANUAL) /* manual mode */
{
Mode = MODE_AUTO; /* set automatic mode */
Timeout = 1000; /* wait for max. 1s */
/* indicate auto mode */
Display_Char('*'); /* display: * */
}
else /* automatic mode */
{
Mode = MODE_MANUAL; /* set manual mode again */
Timeout = 0; /* wait for user */
}
MilliSleep(500); /* smooth UI */
}
else if (Test == KEY_TWICE) /* two short key presses */
{
Flag = 0; /* end loop */
}
LCD_ClearLine2(); /* clear line #2 */
/*
* read and show light intensity
*/
if (Flag) /* ok to proceed */
{
/* get light intensity from BH1750 (in Lux) */
Test = BH1750_ReadLux(&Value, &Scale);
if (Test) /* got value */
{
/* display Lux value */
Display_FullValue(Value, Scale, 'l');
Display_Char('x');
}
else /* some error */
{
Display_Minus(); /* display: - */
}
}
}
/*
* power down BH1750
* - ignore any errors
*/
BH1750_SendCommand(BH1750_POWER_DOWN); /* send command: power down */
}
/* ************************************************************************
* clean-up of local constants
* ************************************************************************ */
/* local constants */
#undef MODE_MANUAL
#undef MODE_AUTO
#undef BH1750_I2C_ADDR_0
#undef BH1750_I2C_ADDR_1
/* source management */
#undef BH1750_C
#endif
/* ************************************************************************
* EOF
* ************************************************************************ */