First Commit
[owSlave2.git] / common / I2C / TSL256x.c
1 #define F_CPU 8000000UL
2 #include <avr/io.h>
3 #include <avr/interrupt.h>
4 #include <util/delay.h>
5 #include <avr/wdt.h>
6 #include <avr/sleep.h>
7 #include "USI_TWI_Master.h"
8
9 void TSL256x_init() {
10         I2c_StartCondition();
11         I2c_WriteByte(0b01010010);  // 0x0101001 1001001 0111001
12         I2c_WriteByte(0x80);
13         I2c_WriteByte(0x03);
14         I2c_StopCondition();
15         I2c_StartCondition();
16         I2c_WriteByte(0b01010010);  // 0x0101001 1001001 0111001
17         I2c_WriteByte(0x81);
18         I2c_WriteByte(0x12);
19         I2c_StopCondition();
20         _delay_ms(500);
21 }
22
23 void TSL256x_setup(uint8_t conf) {
24         I2c_StartCondition();
25         I2c_WriteByte(0b01010010);  // 0x0101001 1001001 0111001
26         I2c_WriteByte(0x81);
27         I2c_WriteByte(conf);
28         I2c_StopCondition();
29 }
30
31 uint16_t TSL256x_Ch(uint8_t ch){
32         uint16_t res;
33         I2c_StartCondition();
34         I2c_WriteByte(0b01010010);  // 0x0101001 1001001 0111001
35         //I2c_WriteByte(0xAD);
36         I2c_WriteByte(0xAC+(ch<<1));
37         I2c_StopCondition();
38         I2c_StartCondition();
39         I2c_WriteByte(0b01010011); 
40         res=I2c_ReadByte(ACK);
41         res|=((uint16_t)I2c_ReadByte(NO_ACK))<<8;
42         I2c_StopCondition();
43         return res;
44 }
45
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
54
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
79
80 uint32_t CalculateLux(uint8_t iGain, uint8_t tInt, uint16_t ch0, uint16_t ch1)
81 {
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
86         uint32_t chScale;
87         uint32_t channel1;
88         uint32_t channel0;
89         switch (tInt)
90         {
91                 case 0: // 13.7 msec
92                 chScale = CHSCALE_TINT0;
93                 break;
94                 case 1: // 101 msec
95                 chScale = CHSCALE_TINT1;
96                 break;
97                 default: // assume no scaling
98                 chScale = (1 << CH_SCALE);
99                 break;
100         }
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
109         uint32_t ratio1 = 0;
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 ?
114         uint16_t b, m;
115                 if ((ratio >= 0) && (ratio <= K1T))
116                 {b=B1T; m=M1T;}
117                 else if (ratio <= K2T)
118                 {b=B2T; m=M2T;}
119                 else if (ratio <= K3T)
120                 {b=B3T; m=M3T;}
121                 else if (ratio <= K4T)
122                 {b=B4T; m=M4T;}
123                 else if (ratio <= K5T)
124                 {b=B5T; m=M5T;}
125                 else if (ratio <= K6T)
126                 {b=B6T; m=M6T;}
127                 else if (ratio <= K7T)
128                 {b=B7T; m=M7T;}
129                 else if (ratio > K8T)
130                 {b=B8T; m=M8T;}
131         uint32_t temp;
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;
139         return(lux);
140 }
141
142
143
144
145         
146