Advertisement
hak8or

Untitled

May 12th, 2014
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.66 KB | None | 0 0
  1. /**
  2.  * @brief Starts the sensing of how many lumens there are.
  3.  * @details Requests the sensor to retriever how many lumens there are
  4.  * based on how long it is sensing, with shorter sensing durations being
  5.  * able to read smaller lumens levels but cutting off at a lower lumen amount.
  6.  * If there is a maximum light level for the interval try sensing at a larger
  7.  * amount to see the non chipped off higher lumen input.
  8.  *
  9.  * @param time 400, 200, or 100. Units are in milliseconds.
  10.  */
  11. void Lumen_Sensor::start_continous_conversion(uint8_t time){
  12.     // First set the timing interval.
  13.         // Ask for access to the Lumen sensor
  14.         Wire.beginTransmission(this->address);
  15.  
  16.         // We will be working with the Control Register(0x00).
  17.         // Command bit = 1
  18.         // 6:4 = 0
  19.         // 3:0 = Config Register (0x01)
  20.         Wire.write((1 << 7) | 0x01);
  21.  
  22.         // Set to continous conversion.
  23.         // 0b00 = 400 milliseconds, 1x multiplier.
  24.         // 0b01 = 200 milliseconds, 2x multiplier.
  25.         // 0b10 = 100 milliseconds, 4x multiplier.
  26.         // 0b11 = Reserved
  27.         uint8_t TCNTRL;
  28.         if (time == 400)
  29.             TCNTRL = 0b00;
  30.         else if (time == 200)
  31.             TCNTRL = 0b01;
  32.         else if (time == 100)
  33.             TCNTRL = 0b10;
  34.         else
  35.             TCNTRL = 0b00;
  36.         Wire.write(0x00 | TCNTRL);
  37.  
  38.         // End transmission.
  39.         Wire.endTransmission();
  40.  
  41.     // And now we start the continous conversion!
  42.         // Ask for access to the Lumen sensor
  43.         Wire.beginTransmission(this->address);
  44.  
  45.         // We will be working with the Control Register(0x00).
  46.         // Command bit = 1
  47.         // 6:4 = 0
  48.         // 3:0 = Control Register (0x00)
  49.         Wire.write((1 << 7) | 0x00);
  50.  
  51.         // Set to continous conversion(Normal Operation)
  52.         // 0b00 = Power down.
  53.         // 0b01 = Reserved.
  54.         // 0b10 = Run a single ADC cycle and return to PowerDown.
  55.         // 0b11 = Normal operation.
  56.         Wire.write(0x00 | 0b11);
  57.  
  58.         // End transmission.
  59.         Wire.endTransmission();
  60. }
  61.  
  62.  
  63. /**
  64.  * @brief Displays the contents of each register via the serial bus.
  65.  * @details The last byte is the device ID register, shift it to the right
  66.  * four positions to check what device you have if you can't do it via
  67.  * get_device_id().
  68.  */
  69. void Lumen_Sensor::display_all_registers(void){
  70.     // This is split up like this because the address auto incrementation on the
  71.     // IC does not seem to work or I don't know how to get it to work correctly.
  72.     // Will hold the contents of the device.
  73.     uint8_t data[10];
  74.  
  75.     // Ask for access to the Lumen sensor
  76.     Wire.beginTransmission(0x29);
  77.  
  78.     // Command bit = 1
  79.     // 6:4 = 0
  80.     // 3:0 = Register address(0x00)
  81.     Wire.write(1 << 7);
  82.  
  83.     // End transmission.
  84.     Wire.endTransmission();
  85.  
  86.     // Request a byte from device 0x29 (lumen meter).
  87.     Wire.requestFrom(0x29, 0x01);
  88.  
  89.     // Get the return data.
  90.     data[0] = Wire.read();
  91.  
  92.     //==========
  93.  
  94.     // Ask for access to the Lumen sensor
  95.     Wire.beginTransmission(0x29);
  96.  
  97.     // Command bit = 1
  98.     // 6:4 = 0
  99.     // 3:0 = Register address(0x00)
  100.     Wire.write((1 << 7) | 0x01);
  101.  
  102.     // End transmission.
  103.     Wire.endTransmission();
  104.  
  105.     // Request a byte from device 0x29 (lumen meter).
  106.     Wire.requestFrom(0x29, 0x01);
  107.  
  108.     // Get the return data.
  109.     data[1] = Wire.read();
  110.  
  111.     //==========
  112.  
  113.     // Ask for access to the Lumen sensor
  114.     Wire.beginTransmission(0x29);
  115.  
  116.     // Command bit = 1
  117.     // 6:4 = 0
  118.     // 3:0 = Register address(0x00)
  119.     Wire.write((1 << 7) | 0x04);
  120.  
  121.     // End transmission.
  122.     Wire.endTransmission();
  123.  
  124.     // Request a byte from device 0x29 (lumen meter).
  125.     Wire.requestFrom(0x29, 0x01);
  126.  
  127.     // Get the return data.
  128.     data[2] = Wire.read();
  129.  
  130.     //==========
  131.  
  132.     // Ask for access to the Lumen sensor
  133.     Wire.beginTransmission(0x29);
  134.  
  135.     // Command bit = 1
  136.     // 6:4 = 0
  137.     // 3:0 = Register address(0x00)
  138.     Wire.write((1 << 7) | 0x05);
  139.  
  140.     // End transmission.
  141.     Wire.endTransmission();
  142.  
  143.     // Request a byte from device 0x29 (lumen meter).
  144.     Wire.requestFrom(0x29, 0x01);
  145.  
  146.     // Get the return data.
  147.     data[3] = Wire.read();
  148.  
  149.     //==========
  150.  
  151.     // Ask for access to the Lumen sensor
  152.     Wire.beginTransmission(0x29);
  153.  
  154.     // Command bit = 1
  155.     // 6:4 = 0
  156.     // 3:0 = Register address(0x00)
  157.     Wire.write((1 << 7) | 0x0A);
  158.  
  159.     // End transmission.
  160.     Wire.endTransmission();
  161.  
  162.     // Request a byte from device 0x29 (lumen meter).
  163.     Wire.requestFrom(0x29, 0x01);
  164.  
  165.     // Get the return data.
  166.     data[4] = Wire.read();
  167.  
  168.     // And now we display it all to the serial port.
  169.     Serial.println("TSL45315 registers: ");
  170.     Serial.print("  CONTROL(0x00): ");
  171.     Serial.println(data[0]);
  172.     Serial.print("  CONFIG(0x01): ");
  173.     Serial.println(data[1]);
  174.     Serial.print("  DATALOW(0x04): ");
  175.     Serial.println(data[2]);
  176.     Serial.print("  DATAHIGH(0x05): ");
  177.     Serial.println(data[3]);
  178.     Serial.print("  Device_ID(0x0A): ");
  179.     Serial.print(data[4], HEX);
  180.     Serial.print(" or when converted: ");
  181.     Serial.println(data[4] >> 4, HEX);
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement