Subversion Repositories Code-Repo

Rev

Rev 148 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 148 Rev 154
Line 31... Line 31...
31
        DBG_PRINT_LUX("%c ", buffer[i]);
31
        DBG_PRINT_LUX("%c ", buffer[i]);
32
    }
32
    }
33
    DBG_PRINT_LUX("\r\n");
33
    DBG_PRINT_LUX("\r\n");
34
 
34
 
35
    // Set default integration time and gain
35
    // Set default integration time and gain
36
    LUX_SetTiming(tsl2561_data_p->integration);
36
    LUX_Set_Timing(tsl2561_data_p->integration);
37
    LUX_SetGain(tsl2561_data_p->gain);
37
    LUX_Set_Gain(tsl2561_data_p->gain);
38
 
38
 
39
    // Start the chip in power-down mode
39
    // Start the chip in power-down mode
40
    LUX_Disable();
40
    LUX_Disable();
41
}
41
}
42
 
42
 
43
void LUX_Enable()  {
43
void LUX_Enable()  {
44
    LUX_Write2Bytes(TSL2561_COMMAND_BIT | TSL2561_REGISTER_CONTROL, TSL2561_CONTROL_POWERON);
44
    LUX_Write_2_Bytes(TSL2561_COMMAND_BIT | TSL2561_REGISTER_CONTROL, TSL2561_CONTROL_POWERON);
45
}
45
}
46
 
46
 
47
void LUX_Disable() {
47
void LUX_Disable() {
48
    LUX_Write2Bytes(TSL2561_COMMAND_BIT | TSL2561_REGISTER_CONTROL, TSL2561_CONTROL_POWEROFF);
48
    LUX_Write_2_Bytes(TSL2561_COMMAND_BIT | TSL2561_REGISTER_CONTROL, TSL2561_CONTROL_POWEROFF);
49
}
49
}
50
 
50
 
51
void LUX_SetGain(tsl2561Gain_t gain) {
51
void LUX_Set_Gain(tsl2561Gain_t gain) {
52
    LUX_Enable();
52
    LUX_Enable();
53
    tsl2561_data_p->gain = gain;
53
    tsl2561_data_p->gain = gain;
54
    LUX_Write2Bytes(TSL2561_COMMAND_BIT | TSL2561_REGISTER_TIMING,
54
    LUX_Write_2_Bytes(TSL2561_COMMAND_BIT | TSL2561_REGISTER_TIMING,
55
            tsl2561_data_p->integration | tsl2561_data_p->gain);
55
            tsl2561_data_p->integration | tsl2561_data_p->gain);
56
    LUX_Disable();
56
    LUX_Disable();
57
}
57
}
58
 
58
 
59
void LUX_SetTiming(tsl2561IntegrationTime_t integration) {
59
void LUX_Set_Timing(tsl2561IntegrationTime_t integration) {
60
    LUX_Enable();
60
    LUX_Enable();
61
    tsl2561_data_p->integration = integration;
61
    tsl2561_data_p->integration = integration;
62
    LUX_Write2Bytes(TSL2561_COMMAND_BIT | TSL2561_REGISTER_TIMING,
62
    LUX_Write_2_Bytes(TSL2561_COMMAND_BIT | TSL2561_REGISTER_TIMING,
63
            tsl2561_data_p->integration | tsl2561_data_p->gain);
63
            tsl2561_data_p->integration | tsl2561_data_p->gain);
64
    LUX_Disable();
64
    LUX_Disable();
65
}
65
}
66
 
66
 
67
unsigned long LUX_CalculateLux(unsigned int ch0, unsigned int ch1) {
67
unsigned long LUX_Calculate_Lux(unsigned int ch0, unsigned int ch1) {
68
    unsigned long chScale, channel0, channel1, ratio1, ratio, temp, lux;
68
    unsigned long chScale, channel0, channel1, ratio1, ratio, temp, lux;
69
    unsigned int b, m;
69
    unsigned int b, m;
70
 
70
 
71
    switch (tsl2561_data_p->integration) {
71
    switch (tsl2561_data_p->integration) {
72
        case TSL2561_INTEGRATIONTIME_13MS:
72
        case TSL2561_INTEGRATIONTIME_13MS:
Line 147... Line 147...
147
    lux = temp >> TSL2561_LUX_LUXSCALE;
147
    lux = temp >> TSL2561_LUX_LUXSCALE;
148
 
148
 
149
    return lux;
149
    return lux;
150
}
150
}
151
 
151
 
152
unsigned long LUX_GetFullLuminosity() {
152
unsigned long LUX_Get_Full_Luminosity() {
153
    unsigned long x;
153
    unsigned long x;
154
 
154
 
155
    // Enable the device by setting the control bit to 0x03
155
    // Enable the device by setting the control bit to 0x03
156
    LUX_Enable();
156
    LUX_Enable();
157
 
157
 
Line 174... Line 174...
174
            Delay10KTCYx(255);
174
            Delay10KTCYx(255);
175
            Delay10KTCYx(145);
175
            Delay10KTCYx(145);
176
            break;
176
            break;
177
    }
177
    }
178
 
178
 
179
    x = LUX_Read2Bytes(TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN1_LOW);
179
    x = LUX_Read_2_Bytes(TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN1_LOW);
180
    x <<= 16;
180
    x <<= 16;
181
    x |= LUX_Read2Bytes(TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN0_LOW);
181
    x |= LUX_Read_2_Bytes(TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN0_LOW);
182
 
182
 
183
    LUX_Disable();
183
    LUX_Disable();
184
 
184
 
185
    return x;
185
    return x;
186
}
186
}
187
 
187
 
188
unsigned int LUX_GetLuminosity(unsigned char channel) {
188
unsigned int LUX_Get_Luminosity(unsigned char channel) {
189
    unsigned long x = LUX_GetFullLuminosity();
189
    unsigned long x = LUX_Get_Full_Luminosity();
190
 
190
 
191
    if (channel == 0) {
191
    if (channel == 0) {
192
        // Reads two byte value from channel 0 (visible + infrared)
192
        // Reads two byte value from channel 0 (visible + infrared)
193
        return (x & 0xFFFF);
193
        return (x & 0xFFFF);
194
    } else if (channel == 1) {
194
    } else if (channel == 1) {
Line 201... Line 201...
201
 
201
 
202
    // Unknown channel!
202
    // Unknown channel!
203
    return 0;
203
    return 0;
204
}
204
}
205
 
205
 
206
void LUX_Write2Bytes(unsigned char reg, unsigned char value) {
206
void LUX_Write_2_Bytes(unsigned char reg, unsigned char value) {
207
    unsigned char buffer[2], result;
207
    unsigned char buffer[2], result;
208
    buffer[0] = reg;
208
    buffer[0] = reg;
209
    buffer[1] = value;
209
    buffer[1] = value;
210
    I2C_Master_Send(tsl2561_data_p->address, 2, buffer);
210
    I2C_Master_Send(tsl2561_data_p->address, 2, buffer);
211
    do {
211
    do {
212
        result = I2C_Get_Status();
212
        result = I2C_Get_Status();
213
    } while (!result);
213
    } while (!result);
214
}
214
}
215
 
215
 
216
unsigned int LUX_Read2Bytes(unsigned char reg) {
216
unsigned int LUX_Read_2_Bytes(unsigned char reg) {
217
    unsigned char result, length, buffer[2];
217
    unsigned char result, length, buffer[2];
218
    unsigned int ret;
218
    unsigned int ret;
219
 
219
 
220
    I2C_Master_Restart(tsl2561_data_p->address, reg, 2);
220
    I2C_Master_Restart(tsl2561_data_p->address, reg, 2);
221
    do {
221
    do {