1 #define F_CPU 8000000UL
3 #include <avr/interrupt.h>
4 #include <util/delay.h>
7 #include "USI_TWI_Master.h"
11 I2c_WriteByte(0b01010010); // 0x0101001 1001001 0111001
16 I2c_WriteByte(0b01010010); // 0x0101001 1001001 0111001
23 void TSL256x_setup(uint8_t conf) {
25 I2c_WriteByte(0b01010010); // 0x0101001 1001001 0111001
31 uint16_t TSL256x_Ch(uint8_t ch){
34 I2c_WriteByte(0b01010010); // 0x0101001 1001001 0111001
35 //I2c_WriteByte(0xAD);
36 I2c_WriteByte(0xAC+(ch<<1));
39 I2c_WriteByte(0b01010011);
40 res=I2c_ReadByte(ACK);
41 res|=((uint16_t)I2c_ReadByte(NO_ACK))<<8;
46 #define LUX_SCALE 14 // scale by 2^14
47 #define RATIO_SCALE 9 // scale ratio by 2^9
48 //???????????????????????????????????????????????????
49 // Integration time scaling factors
50 //???????????????????????????????????????????????????
51 #define CH_SCALE 10 // scale channel values by 2^10
52 #define CHSCALE_TINT0 0x7517 // 322/11 * 2^CH_SCALE
53 #define CHSCALE_TINT1 0x0fe7 // 322/81 * 2^CH_SCALE
55 #define K1T 0x0040 // 0.125 * 2^RATIO_SCALE
56 #define B1T 0x01f2 // 0.0304 * 2^LUX_SCALE
57 #define M1T 0x01be // 0.0272 * 2^LUX_SCALE
58 #define K2T 0x0080 // 0.250 * 2^RATIO_SCALE
59 #define B2T 0x0214 // 0.0325 * 2^LUX_SCALE
60 #define M2T 0x02d1 // 0.0440 * 2^LUX_SCALE
61 #define K3T 0x00c0 // 0.375 * 2^RATIO_SCALE
62 #define B3T 0x023f // 0.0351 * 2^LUX_SCALE
63 #define M3T 0x037b // 0.0544 * 2^LUX_SCALE
64 #define K4T 0x0100 // 0.50 * 2^RATIO_SCALE
65 #define B4T 0x0270 // 0.0381 * 2^LUX_SCALE
66 #define M4T 0x03fe // 0.0624 * 2^LUX_SCALE
67 #define K5T 0x0138 // 0.61 * 2^RATIO_SCALE
68 #define B5T 0x016f // 0.0224 * 2^LUX_SCALE
69 #define M5T 0x01fc // 0.0310 * 2^LUX_SCALE
70 #define K6T 0x019a // 0.80 * 2^RATIO_SCALE
71 #define B6T 0x00d2 // 0.0128 * 2^LUX_SCALE
72 #define M6T 0x00fb // 0.0153 * 2^LUX_SCALE
73 #define K7T 0x029a // 1.3 * 2^RATIO_SCALE
74 #define B7T 0x0018 // 0.00146 * 2^LUX_SCALE
75 #define M7T 0x0012 // 0.00112 * 2^LUX_SCALE
76 #define K8T 0x029a // 1.3 * 2^RATIO_SCALE
77 #define B8T 0x0000 // 0.000 * 2^LUX_SCALE
78 #define M8T 0x0000 // 0.000 * 2^LUX_SCALE
80 uint32_t CalculateLux(uint8_t iGain, uint8_t tInt, uint16_t ch0, uint16_t ch1)
82 //????????????????????????????????????????????????????????????????????????
83 // first, scale the channel values depending on the gain and integration time
84 // 16X, 402mS is nominal.
85 // scale if integration time is NOT 402 msec
92 chScale = CHSCALE_TINT0;
95 chScale = CHSCALE_TINT1;
97 default: // assume no scaling
98 chScale = (1 << CH_SCALE);
101 // scale if gain is NOT 16X
102 if (!iGain) chScale = chScale << 4; // scale 1X to 16X
103 // scale the channel values
104 channel0 = (ch0 * chScale) >> CH_SCALE;
105 channel1 = (ch1 * chScale) >> CH_SCALE;
106 //????????????????????????????????????????????????????????????????????????
107 // find the ratio of the channel values (Channel1/Channel0)
108 // protect against divide by zero
110 if (channel0 != 0) ratio1 = (channel1 << (RATIO_SCALE+1)) / channel0;
111 // round the ratio value
112 uint32_t ratio = (ratio1 + 1) >> 1;
113 // is ratio <= eachBreak ?
115 if ((ratio >= 0) && (ratio <= K1T))
117 else if (ratio <= K2T)
119 else if (ratio <= K3T)
121 else if (ratio <= K4T)
123 else if (ratio <= K5T)
125 else if (ratio <= K6T)
127 else if (ratio <= K7T)
129 else if (ratio > K8T)
132 temp = ((channel0 * b) - (channel1 * m));
133 // do not allow negative lux value
134 if (temp < 0) temp = 0;
135 // round lsb (2^(LUX_SCALE?1))
136 temp += (1 << (LUX_SCALE-1));
137 // strip off fractional portion
138 uint32_t lux = temp ;//>> LUX_SCALE;