TCS34725(1053).c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. #include "TCS34725.h"
  2. tcs34725IntegrationTime_t _tcs34725IntegrationTime;
  3. tcs34725Gain_t _tcs34725Gain;
  4. uint8_t TCS34725_I2C_Read(uint8_t addr, uint8_t reg)
  5. {
  6. uint8_t TCS34725_I2C[3]={0,};
  7. uint16_t value = 0;
  8. uint8_t data = 0;
  9. data=HAL_I2C_Mem_Read(&hi2c1, addr, reg, 1, TCS34725_I2C, 1, 10);
  10. // i2c_status(data);
  11. value = TCS34725_I2C[0];
  12. return value;
  13. }
  14. void TCS34725_I2C_Write(uint8_t addr, uint8_t reg, uint8_t data)
  15. {
  16. uint8_t tmp_afe;
  17. tmp_afe = data;
  18. HAL_I2C_Mem_Write(&hi2c1, addr, reg, 1, &tmp_afe, 1, 10);
  19. }
  20. /**************************************************************************/
  21. /*!
  22. Adjusts the gain on the TCS34725 (adjusts the sensitivity to light)
  23. */
  24. /**************************************************************************/
  25. void TCS34725_getrawdata(void)
  26. {
  27. uint8_t data_ret_R =0;
  28. uint8_t data_ret_G =0;
  29. uint8_t data_ret_B =0;
  30. uint8_t data_ret_C =0;
  31. uint8_t DEV_DATA = TCS34725_I2C_Read(TCS34725_ADDRESS, TCS34725_COMMAND_BIT | TCS34725_ID);
  32. uint8_t C_DATA_L = TCS34725_I2C_Read(TCS34725_ADDRESS, TCS34725_COMMAND_BIT | TCS34725_CDATAL);
  33. uint8_t C_DATA_H = TCS34725_I2C_Read(TCS34725_ADDRESS, TCS34725_COMMAND_BIT | TCS34725_CDATAH);
  34. uint8_t R_DATA_L = TCS34725_I2C_Read(TCS34725_ADDRESS, TCS34725_COMMAND_BIT | TCS34725_RDATAL);
  35. uint8_t R_DATA_H = TCS34725_I2C_Read(TCS34725_ADDRESS, TCS34725_COMMAND_BIT | TCS34725_RDATAH);
  36. uint8_t G_DATA_L = TCS34725_I2C_Read(TCS34725_ADDRESS, TCS34725_COMMAND_BIT | TCS34725_GDATAL);
  37. uint8_t G_DATA_H = TCS34725_I2C_Read(TCS34725_ADDRESS, TCS34725_COMMAND_BIT | TCS34725_GDATAH);
  38. uint8_t B_DATA_L = TCS34725_I2C_Read(TCS34725_ADDRESS, TCS34725_COMMAND_BIT | TCS34725_BDATAL);
  39. uint8_t B_DATA_H = TCS34725_I2C_Read(TCS34725_ADDRESS, TCS34725_COMMAND_BIT | TCS34725_BDATAH);
  40. printf("C_DATA_L : %02x C_DATA_H %02x \r\n",C_DATA_L,C_DATA_H);
  41. printf("R_DATA_L : %02x R_DATA_H %02x \r\n",R_DATA_L,R_DATA_H);
  42. printf("G_DATA_L : %02x G_DATA_H %02x \r\n",G_DATA_L,G_DATA_H);
  43. printf("B_DATA_L : %02x B_DATA_H %02x \r\n",B_DATA_L,B_DATA_H);
  44. #if 0
  45. double CLEAR = 0;
  46. double RED = 0;
  47. double GREEN = 0;
  48. double BLUE = 0;
  49. printf("************************************\r\n");
  50. printf("1. DEV ID\t:\t%x\r\n", DEV_DATA);
  51. CLEAR = (C_DATA_H << 8) | C_DATA_L;
  52. data_ret_C = (CLEAR / 65535)*255;
  53. printf("CLEAR : %d\r\n",data_ret_C);
  54. RED = (R_DATA_H << 8) | R_DATA_L;
  55. data_ret_R = (RED / 65535)*255;
  56. printf("RED : %d\r\n",data_ret_R);
  57. GREEN = (G_DATA_H << 8) | G_DATA_L;
  58. data_ret_G = (GREEN / 65535)*255;
  59. printf("GREEN : %d\r\n",data_ret_G);
  60. BLUE = (B_DATA_H << 8) | B_DATA_L;
  61. data_ret_B = (BLUE / 65535)*255;
  62. printf("BLUE : %d\r\n",data_ret_B);
  63. //#else
  64. double CLEAR = 0;
  65. double RED = 0;
  66. double GREEN = 0;
  67. double BLUE = 0;
  68. CLEAR = (C_DATA_H << 8) | C_DATA_L;
  69. data_ret_C = (CLEAR / 65535)*255;
  70. RED = (R_DATA_H << 8) | R_DATA_L;
  71. data_ret_R = (RED / 65535)*255;
  72. GREEN = (G_DATA_H << 8) | G_DATA_L;
  73. data_ret_G = (GREEN / 65535)*255;
  74. BLUE = (B_DATA_H << 8) | B_DATA_L;
  75. data_ret_B = (BLUE / 65535)*255;
  76. #if 0
  77. printf("02%04x%05d%04x%05d%04x%05d%04x%05d03\r\n",data_ret_C,data_ret_C,data_ret_R,data_ret_R,data_ret_G,data_ret_G,data_ret_B,data_ret_B);
  78. #else
  79. // printf("%04x,%05d,%04x,%05d,%04x,%05d,%04x,%05d,\r\n",data_ret_C,data_ret_C,data_ret_R,data_ret_R,data_ret_G,data_ret_G,data_ret_B,data_ret_B);
  80. printf("%x %d %d %d %d %d %d %d %d\r\n",DEV_DATA,(uint16_t)CLEAR,data_ret_C,(uint16_t)RED,data_ret_R,(uint16_t)GREEN,data_ret_G,(uint16_t)BLUE,data_ret_B);
  81. #endif
  82. #endif
  83. #if 0
  84. uint16_t CLEAR = 0;
  85. uint16_t RED = 0;
  86. uint16_t GREEN = 0;
  87. uint16_t BLUE = 0;
  88. CLEAR = (C_DATA_H << 8) | C_DATA_L;
  89. RED = (R_DATA_H << 8) | R_DATA_L;
  90. GREEN = (G_DATA_H << 8) | G_DATA_L;
  91. BLUE = (B_DATA_H << 8) | B_DATA_L;
  92. printf("1. DEV ID\t:\t%x\r\n", DEV_DATA);
  93. printf("3. C\t:\tHEX : [%04x]\tDEC : [%05d]\r\n", CLEAR,CLEAR);
  94. printf("4. R\t:\tHEX : [%04x]\tDEC : [%05d]\r\n", RED,RED);
  95. printf("5. G\t:\tHEX : [%04x]\tDEC : [%05d]\r\n", GREEN,GREEN);
  96. printf("6. B\t:\tHEX : [%04x]\tDEC : [%05d]\r\n", BLUE,BLUE);
  97. #else
  98. #endif
  99. }
  100. void TCS34725_disable(void){
  101. /* Turn the device off to save power */
  102. uint8_t reg = 0;
  103. reg = TCS34725_I2C_Read(TCS34725_ADDRESS,TCS34725_ENABLE);
  104. TCS34725_I2C_Write(TCS34725_ADDRESS,TCS34725_ENABLE, reg & ~(TCS34725_ENABLE_PON | TCS34725_ENABLE_AEN));
  105. }
  106. void TCS34725_enable(void)
  107. {
  108. TCS34725_I2C_Write(TCS34725_ADDRESS, TCS34725_ENABLE, TCS34725_ENABLE_PON);
  109. HAL_Delay(3);
  110. TCS34725_I2C_Write(TCS34725_ADDRESS, TCS34725_ENABLE, TCS34725_ENABLE_PON | TCS34725_ENABLE_AEN);
  111. switch (_tcs34725IntegrationTime)
  112. {
  113. case TCS34725_INTEGRATIONTIME_2_4MS:
  114. HAL_Delay(3);
  115. break;
  116. case TCS34725_INTEGRATIONTIME_24MS:
  117. HAL_Delay(24);
  118. break;
  119. case TCS34725_INTEGRATIONTIME_50MS:
  120. HAL_Delay(50);
  121. break;
  122. case TCS34725_INTEGRATIONTIME_101MS:
  123. HAL_Delay(101);
  124. break;
  125. case TCS34725_INTEGRATIONTIME_154MS:
  126. HAL_Delay(154);
  127. break;
  128. case TCS34725_INTEGRATIONTIME_700MS:
  129. HAL_Delay(700);
  130. break;
  131. }
  132. }
  133. void tcs34725SetIntegrationTime(tcs34725IntegrationTime_t it)
  134. {
  135. TCS34725_I2C_Write(TCS34725_ADDRESS,TCS34725_ATIME, it);
  136. _tcs34725IntegrationTime = it;
  137. }
  138. void getRawDataOneShot (void)
  139. {
  140. // TCS34725_enable();
  141. TCS34725_getrawdata();
  142. // TCS34725_disable();
  143. }
  144. /**************************************************************************/
  145. /*!
  146. @brief Converts the raw R/G/B values to color temperature in degrees
  147. Kelvin
  148. */
  149. /**************************************************************************/
  150. uint16_t calculateColorTemperature(uint16_t r, uint16_t g, uint16_t b)
  151. {
  152. float X, Y, Z; /* RGB to XYZ correlation */
  153. float xc, yc; /* Chromaticity co-ordinates */
  154. float n; /* McCamy's formula */
  155. float cct;
  156. /* 1. Map RGB values to their XYZ counterparts. */
  157. /* Based on 6500K fluorescent, 3000K fluorescent */
  158. /* and 60W incandescent values for a wide range. */
  159. /* Note: Y = Illuminance or lux */
  160. X = (-0.14282F * r) + (1.54924F * g) + (-0.95641F * b);
  161. Y = (-0.32466F * r) + (1.57837F * g) + (-0.73191F * b);
  162. Z = (-0.68202F * r) + (0.77073F * g) + ( 0.56332F * b);
  163. /* 2. Calculate the chromaticity co-ordinates */
  164. xc = (X) / (X + Y + Z);
  165. yc = (Y) / (X + Y + Z);
  166. /* 3. Use McCamy's formula to determine the CCT */
  167. n = (xc - 0.3320F) / (0.1858F - yc);
  168. /* Calculate the final CCT */
  169. cct = (449.0F * powf(n, 3)) + (3525.0F * powf(n, 2)) + (6823.3F * n) + 5520.33F;
  170. /* Return the results in degrees Kelvin */
  171. return (uint16_t)cct;
  172. }
  173. /**************************************************************************/
  174. /*!
  175. @brief Converts the raw R/G/B values to lux
  176. */
  177. /**************************************************************************/
  178. uint16_t calculateLux(uint16_t r, uint16_t g, uint16_t b)
  179. {
  180. float illuminance;
  181. /* This only uses RGB ... how can we integrate clear or calculate lux */
  182. /* based exclusively on clear since this might be more reliable? */
  183. illuminance = (-0.32466F * r) + (1.57837F * g) + (-0.73191F * b);
  184. return (uint16_t)illuminance;
  185. }
  186. /**************************************************************************/
  187. /*!
  188. @brief Sets gain to the specified value
  189. */
  190. /**************************************************************************/
  191. void tcs34725SetGain(tcs34725Gain_t gain)
  192. {
  193. TCS34725_I2C_Write(TCS34725_ADDRESS,TCS34725_CONTROL, gain);
  194. _tcs34725Gain = gain;
  195. }
  196. /**************************************************************************/
  197. /*!
  198. @brief Converts the raw R/G/B values to color temperature in degrees
  199. Kelvin using the algorithm described in DN40 from Taos (now AMS).
  200. */
  201. /**************************************************************************/
  202. uint16_t calculateColorTemperature_dn40(uint16_t r, uint16_t g, uint16_t b, uint16_t c)
  203. {
  204. int rc; /* Error return code */
  205. uint16_t r2, g2, b2; /* RGB values minus IR component */
  206. int gl; /* Results of the initial lux conversion */
  207. uint8_t gain_int; /* Gain multiplier as a normal integer */
  208. uint16_t sat; /* Digital saturation level */
  209. uint16_t ir; /* Inferred IR content */
  210. /* Analog/Digital saturation:
  211. *
  212. * (a) As light becomes brighter, the clear channel will tend to
  213. * saturate first since R+G+B is approximately equal to C.
  214. * (b) The TCS34725 accumulates 1024 counts per 2.4ms of integration
  215. * time, up to a maximum values of 65535. This means analog
  216. * saturation can occur up to an integration time of 153.6ms
  217. * (64*2.4ms=153.6ms).
  218. * (c) If the integration time is > 153.6ms, digital saturation will
  219. * occur before analog saturation. Digital saturation occurs when
  220. * the count reaches 65535.
  221. */
  222. if ((256 - _tcs34725IntegrationTime) > 63) {
  223. /* Track digital saturation */
  224. sat = 65535;
  225. } else {
  226. /* Track analog saturation */
  227. sat = 1024 * (256 - _tcs34725IntegrationTime);
  228. }
  229. /* Ripple rejection:
  230. *
  231. * (a) An integration time of 50ms or multiples of 50ms are required to
  232. * reject both 50Hz and 60Hz ripple.
  233. * (b) If an integration time faster than 50ms is required, you may need
  234. * to average a number of samples over a 50ms period to reject ripple
  235. * from fluorescent and incandescent light sources.
  236. *
  237. * Ripple saturation notes:
  238. *
  239. * (a) If there is ripple in the received signal, the value read from C
  240. * will be less than the max, but still have some effects of being
  241. * saturated. This means that you can be below the 'sat' value, but
  242. * still be saturating. At integration times >150ms this can be
  243. * ignored, but <= 150ms you should calculate the 75% saturation
  244. * level to avoid this problem.
  245. */
  246. if ((256 - _tcs34725IntegrationTime) <= 63) {
  247. /* Adjust sat to 75% to avoid analog saturation if atime < 153.6ms */
  248. sat -= sat/4;
  249. }
  250. /* Check for saturation and mark the sample as invalid if true */
  251. if (c >= sat) {
  252. return 0;
  253. }
  254. /* AMS RGB sensors have no IR channel, so the IR content must be */
  255. /* calculated indirectly. */
  256. ir = (r + g + b > c) ? (r + g + b - c) / 2 : 0;
  257. /* Remove the IR component from the raw RGB values */
  258. r2 = r - ir;
  259. g2 = g - ir;
  260. b2 = b - ir;
  261. /* Convert gain to a usable integer value */
  262. switch(_tcs34725Gain) {
  263. case TCS34725_GAIN_4X: /* GAIN 4X */
  264. gain_int = 4;
  265. break;
  266. case TCS34725_GAIN_16X: /* GAIN 16X */
  267. gain_int = 16;
  268. break;
  269. case TCS34725_GAIN_60X: /* GAIN 60X */
  270. gain_int = 60;
  271. break;
  272. case TCS34725_GAIN_1X: /* GAIN 1X */
  273. default:
  274. gain_int = 1;
  275. break;
  276. }
  277. /* Calculate the counts per lux (CPL), taking into account the optional
  278. * arguments for Glass Attenuation (GA) and Device Factor (DF).
  279. *
  280. * GA = 1/T where T is glass transmissivity, meaning if glass is 50%
  281. * transmissive, the GA is 2 (1/0.5=2), and if the glass attenuates light
  282. * 95% the GA is 20 (1/0.05). A GA of 1.0 assumes perfect transmission.
  283. *
  284. * NOTE: It is recommended to have a CPL > 5 to have a lux accuracy
  285. * < +/- 0.5 lux, where the digitization error can be calculated via:
  286. * 'DER = (+/-2) / CPL'.
  287. */
  288. float cpl = (((256-_tcs34725IntegrationTime)*2.4f) * gain_int) /
  289. (1.0f * 310.0f);
  290. /* Determine lux accuracy (+/- lux) */
  291. float der = 2.0f / cpl;
  292. /* Determine the maximum lux value */
  293. float max_lux = 65535.0 / (cpl * 3);
  294. /* Lux is a function of the IR-compensated RGB channels and the associated
  295. * color coefficients, with G having a particularly heavy influence to
  296. * match the nature of the human eye.
  297. *
  298. * NOTE: The green value should be > 10 to ensure the accuracy of the lux
  299. * conversions. If it is below 10, the gain should be increased, but
  300. * the clear<100 check earlier should cover this edge case.
  301. */
  302. gl = 0.136f * (float)r2 + /** Red coefficient. */
  303. 1.000f * (float)g2 + /** Green coefficient. */
  304. -0.444f * (float)b2; /** Blue coefficient. */
  305. float lux = gl / cpl;
  306. /* A simple method of measuring color temp is to use the ratio of blue */
  307. /* to red light, taking IR cancellation into account. */
  308. uint16_t cct = (3810 * (uint32_t)b2) / /** Color temp coefficient. */
  309. (uint32_t)r2 + 1391; /** Color temp offset. */
  310. return cct;
  311. }
  312. void i2c_status(HAL_StatusTypeDef data){
  313. switch(data){
  314. case HAL_OK :printf("HAL_OK \r\n");break;
  315. case HAL_ERROR :printf("HAL_ERROR \r\n");break;
  316. case HAL_BUSY :printf("HAL_BUSY \r\n");break;
  317. case HAL_TIMEOUT :printf("HAL_TIMEOUT\r\n");break;
  318. }
  319. }
  320. void TCS34725_init(void){
  321. uint8_t DEV_DATA = TCS34725_I2C_Read(TCS34725_ADDRESS, TCS34725_COMMAND_BIT | TCS34725_ID);
  322. // tcs34725SetIntegrationTime(TCS34725_INTEGRATIONTIME_700MS);
  323. // tcs34725SetGain(TCS34725_GAIN_60X);
  324. TCS34725_enable();
  325. if(DEV_DATA == 0x44 || DEV_DATA == 0x4D){
  326. printf("TCS34725_Success\r\n");
  327. }else{
  328. printf("TCS34725_Failed : %02x\r\n",DEV_DATA);
  329. }
  330. }