Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
TSL2550.cpp
00001 /* 00002 * mbed library to use a TAOS TSL2550 Ambient Light Sensor 00003 * Copyright (c) 2011 Stefan Goeckeritz, 00004 * RheinAhrCampus Remagen, Suedallee 2, 53424 Remagen 00005 * 00006 * Released under the MIT License: http://mbed.org/license/mit 00007 */ 00008 00009 #include "mbed.h" 00010 #include "TSL2550.h" 00011 00012 #define TSL2550_ADDR 0x72 //hardwired slave address of TSL2550 00013 #define xpow(x, y) ((long)1 << y) 00014 00015 00016 TSL2550::TSL2550 (PinName p_sda, PinName p_scl ) : i2c(p_sda, p_scl) { 00017 init(); 00018 } 00019 00020 TSL2550::TSL2550 (I2C& p_i2c) : i2c(p_i2c) { 00021 init(); 00022 } 00023 00024 unsigned short TSL2550::get_ch0() { 00025 return ch0; 00026 } 00027 00028 unsigned short TSL2550::get_ch1() { 00029 return ch1; 00030 } 00031 00032 double TSL2550::get_lux() { 00033 return lux; 00034 } 00035 00036 void TSL2550::update () { 00037 unsigned short c0, c1, s0, s1; 00038 int adc0, adc1; 00039 double r; 00040 00041 //read channel 0 and 1 00042 ch0 = twi_read(TSL2550_ADDR, 0x43); 00043 wait(0.5); 00044 ch1 = twi_read(TSL2550_ADDR, 0x83); 00045 00046 //now compute lux value, if data is valid 00047 if((ch0 & 0x80) && (ch1 & 0x80)) 00048 { 00049 c0 = (ch0 & ~0x80) >> 4 ; 00050 c1 = (ch1 & ~0x80) >> 4 ; 00051 s0 = ch0 & ~0xF0 ; 00052 s1 = ch1 & ~0xF0 ; 00053 00054 //ADC Count Value 00055 adc0 = int(16.5 * xpow(2,c0)) + (s0 * xpow(2,c0)); 00056 adc1 = int(16.5 * xpow(2,c1)) + (s1 * xpow(2,c1)); 00057 00058 r = (double(adc1) / double(adc0)); 00059 lux = double(adc0) * 0.46 * exp(-3.13 * r); 00060 00061 //give debug information 00062 //printf("\n\nZW: c0=%i s0=%i c1=%i s1=%i \n", c0, s0, c1, s1); 00063 //printf("ZW: adc0=%i, adc1=%i\n", adc0, adc1); 00064 } 00065 else 00066 printf("TSL2550: ***data not valid ! ***\n"); 00067 } 00068 00069 void TSL2550::init() { 00070 unsigned short i; 00071 00072 i = twi_read(TSL2550_ADDR, 0x03); 00073 if( i == 0x03 ) 00074 printf("TSL2550: Init successful\n"); 00075 else 00076 printf("TSL2550: Init ***failed*** !\n"); 00077 } 00078 00079 unsigned short TSL2550::twi_read (unsigned short id, unsigned short data) { 00080 unsigned short i; 00081 00082 //supply COMMAND byte to slave 00083 i2c.start(); 00084 i2c.write(id); 00085 i2c.write(data); 00086 00087 //demand ANSWER byte from slave 00088 i2c.start(); 00089 i2c.write(id | 1); 00090 i = i2c.read(0); 00091 i2c.stop(); 00092 00093 00094 return i; 00095 } 00096 00097 void TSL2550::twi_write (unsigned short id, unsigned short data) { 00098 00099 i2c.start(); 00100 i2c.write(id); 00101 i2c.write(data); 00102 i2c.stop(); 00103 } 00104
Generated on Wed Jul 13 2022 06:14:21 by
1.7.2