forked from qalex/blackboard-STC15
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBH1750.c
50 lines (37 loc) · 827 Bytes
/
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
/*
* BH1750.c
*
* Created on: Jan 5, 2016
* Author: alex
*/
#include "sys.h"
#include "i2c.h"
#define BH1750_ADDR 0x23
void init_BH1750() {
u8 ack;
I2CInit();
// Send start condition
I2CStart();
// Send slave address
ack = I2CSend(BH1750_ADDR << 1);
// ack == 1 => NAK
// ack == 0 => ACK
// send "Continuously H-Resolution Mode"
ack = I2CSend(0x10);
I2CStop();
}
u16 read_BH1750() {
u16 val = 0;
I2CStart();
// Send slave address with Read bit set
I2CSend(BH1750_ADDR << 1 | 1);
val = I2CRead() << 8; // high byte
I2CAck(); // Send ack
val |= I2CRead(); // low byte
// Send nak for last byte to indicate
// End of transmission
I2CNak();
// Send stop condition
I2CStop();
return val / 1.2; // adjust value
}