SI114X.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * SI114X.cpp
  3. * A library for Grove - Sunlight Sensor v1.0
  4. *
  5. * Copyright (c) 2015 seeed technology inc.
  6. * Website : www.seeed.cc
  7. * Author : Fuhua.Chen
  8. * Modified Time: June 2015
  9. *
  10. * The MIT License (MIT)
  11. *
  12. * Permission is hereby granted, free of charge, to any person obtaining a copy
  13. * of this software and associated documentation files (the "Software"), to deal
  14. * in the Software without restriction, including without limitation the rights
  15. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  16. * copies of the Software, and to permit persons to whom the Software is
  17. * furnished to do so, subject to the following conditions:
  18. *
  19. * The above copyright notice and this permission notice shall be included in
  20. * all copies or substantial portions of the Software.
  21. *
  22. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  25. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  27. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  28. * THE SOFTWARE.
  29. */
  30. #include "SI114X.h"
  31. #include "Wire.h"
  32. /*--------------------------------------------------------//
  33. default init
  34. */
  35. void SI114X::DeInit(void)
  36. {
  37. //ENABLE UV reading
  38. //these reg must be set to the fixed value
  39. WriteByte(SI114X_UCOEFF0, 0x29);
  40. WriteByte(SI114X_UCOEFF1, 0x89);
  41. WriteByte(SI114X_UCOEFF2, 0x02);
  42. WriteByte(SI114X_UCOEFF3, 0x00);
  43. WriteParamData(SI114X_CHLIST, SI114X_CHLIST_ENUV |SI114X_CHLIST_ENALSIR | SI114X_CHLIST_ENALSVIS |SI114X_CHLIST_ENPS1);
  44. //
  45. //set LED1 CURRENT(22.4mA)(It is a normal value for many LED)
  46. //
  47. WriteParamData(SI114X_PS1_ADCMUX, SI114X_ADCMUX_LARGE_IR);
  48. WriteByte(SI114X_PS_LED21, SI114X_LED_CURRENT_22MA);
  49. WriteParamData(SI114X_PSLED12_SELECT, SI114X_PSLED12_SELECT_PS1_LED1); //
  50. //
  51. //PS ADC SETTING
  52. //
  53. WriteParamData(SI114X_PS_ADC_GAIN, SI114X_ADC_GAIN_DIV1);
  54. WriteParamData(SI114X_PS_ADC_COUNTER, SI114X_ADC_COUNTER_511ADCCLK);
  55. WriteParamData(SI114X_PS_ADC_MISC, SI114X_ADC_MISC_HIGHRANGE|SI114X_ADC_MISC_ADC_RAWADC);
  56. //
  57. //VIS ADC SETTING
  58. //
  59. WriteParamData(SI114X_ALS_VIS_ADC_GAIN, SI114X_ADC_GAIN_DIV1);
  60. WriteParamData(SI114X_ALS_VIS_ADC_COUNTER, SI114X_ADC_COUNTER_511ADCCLK);
  61. WriteParamData(SI114X_ALS_VIS_ADC_MISC, SI114X_ADC_MISC_HIGHRANGE);
  62. //
  63. //IR ADC SETTING
  64. //
  65. WriteParamData(SI114X_ALS_IR_ADC_GAIN, SI114X_ADC_GAIN_DIV1);
  66. WriteParamData(SI114X_ALS_IR_ADC_COUNTER, SI114X_ADC_COUNTER_511ADCCLK);
  67. WriteParamData(SI114X_ALS_IR_ADC_MISC, SI114X_ADC_MISC_HIGHRANGE);
  68. //
  69. //interrupt enable
  70. //
  71. WriteByte(SI114X_INT_CFG, SI114X_INT_CFG_INTOE);
  72. WriteByte(SI114X_IRQ_ENABLE, SI114X_IRQEN_ALS);
  73. //
  74. //AUTO RUN
  75. //
  76. WriteByte(SI114X_MEAS_RATE0, 0xFF);
  77. WriteByte(SI114X_COMMAND, SI114X_PSALS_AUTO);
  78. }
  79. /*--------------------------------------------------------//
  80. Init the si114x and begin to collect data
  81. */
  82. bool SI114X::Begin(void)
  83. {
  84. Wire.begin();
  85. //
  86. //Init IIC and reset si1145
  87. //
  88. if(ReadByte(SI114X_PART_ID)!=0X45)
  89. {
  90. return false;
  91. }
  92. Reset();
  93. //
  94. //INIT
  95. //
  96. DeInit();
  97. return true;
  98. }
  99. /*--------------------------------------------------------//
  100. reset the si114x
  101. inclue IRQ reg, command regs...
  102. */
  103. void SI114X::Reset(void)
  104. {
  105. WriteByte(SI114X_MEAS_RATE0, 0);
  106. WriteByte(SI114X_MEAS_RATE1, 0);
  107. WriteByte(SI114X_IRQ_ENABLE, 0);
  108. WriteByte(SI114X_IRQ_MODE1, 0);
  109. WriteByte(SI114X_IRQ_MODE2, 0);
  110. WriteByte(SI114X_INT_CFG, 0);
  111. WriteByte(SI114X_IRQ_STATUS, 0xFF);
  112. WriteByte(SI114X_COMMAND, SI114X_RESET);
  113. delay(10);
  114. WriteByte(SI114X_HW_KEY, 0x17);
  115. delay(10);
  116. }
  117. /*--------------------------------------------------------//
  118. write one byte into si114x's reg
  119. */
  120. void SI114X::WriteByte(uint8_t Reg, uint8_t Value)
  121. {
  122. Wire.beginTransmission(SI114X_ADDR);
  123. Wire.write(Reg);
  124. Wire.write(Value);
  125. Wire.endTransmission();
  126. }
  127. /*--------------------------------------------------------//
  128. read one byte data from si114x
  129. */
  130. uint8_t SI114X::ReadByte(uint8_t Reg)
  131. {
  132. Wire.beginTransmission(SI114X_ADDR);
  133. Wire.write(Reg);
  134. Wire.endTransmission();
  135. Wire.requestFrom(SI114X_ADDR, 1);
  136. return Wire.read();
  137. }
  138. /*--------------------------------------------------------//
  139. read half word(2 bytes) data from si114x
  140. */
  141. uint16_t SI114X::ReadHalfWord(uint8_t Reg)
  142. {
  143. uint16_t Value;
  144. Wire.beginTransmission(SI114X_ADDR);
  145. Wire.write(Reg);
  146. Wire.endTransmission();
  147. Wire.requestFrom(SI114X_ADDR, 2);
  148. Value = Wire.read();
  149. Value |= (uint16_t)Wire.read() << 8;
  150. return Value;
  151. }
  152. /*--------------------------------------------------------//
  153. read param data
  154. */
  155. uint8_t SI114X::ReadParamData(uint8_t Reg)
  156. {
  157. WriteByte(SI114X_COMMAND, Reg | SI114X_QUERY);
  158. return ReadByte(SI114X_RD);
  159. }
  160. /*--------------------------------------------------------//
  161. writ param data
  162. */
  163. uint8_t SI114X::WriteParamData(uint8_t Reg,uint8_t Value)
  164. {
  165. //write Value into PARAMWR reg first
  166. WriteByte(SI114X_WR, Value);
  167. WriteByte(SI114X_COMMAND, Reg | SI114X_SET);
  168. //SI114X writes value out to PARAM_RD,read and confirm its right
  169. return ReadByte(SI114X_RD);
  170. }
  171. /*--------------------------------------------------------//
  172. Read Visible Value
  173. */
  174. uint16_t SI114X::ReadVisible(void)
  175. {
  176. return ReadHalfWord(SI114X_ALS_VIS_DATA0);
  177. }
  178. /*--------------------------------------------------------//
  179. Read IR Value
  180. */
  181. uint16_t SI114X::ReadIR(void)
  182. {
  183. return ReadHalfWord(SI114X_ALS_IR_DATA0);
  184. }
  185. /*--------------------------------------------------------//
  186. Read Proximity Value
  187. */
  188. uint16_t SI114X::ReadProximity(uint8_t PSn)
  189. {
  190. return ReadHalfWord(PSn);
  191. }
  192. /*--------------------------------------------------------//
  193. Read UV Value
  194. this function is a int value ,but the real value must be div 100
  195. */
  196. uint16_t SI114X::ReadUV(void)
  197. {
  198. return (ReadHalfWord(SI114X_AUX_DATA0_UVINDEX0));
  199. }