SI114X.cpp 6.2 KB

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