Advertisement
Guest User

Untitled

a guest
Apr 5th, 2014
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. #define VCNL4000_ADDRESS 0x13 //I2C Address of the board
  2.  
  3. // VCNL4000 Register Map
  4. #define COMMAND_0 0x80 // starts measurments, relays data ready info
  5. #define PRODUCT_ID 0x81 // product ID/revision ID, should read 0x11
  6. #define IR_CURRENT 0x83 // sets IR current in steps of 10mA 0-200mA
  7. #define AMBIENT_PARAMETER 0x84 // Configures ambient light measures
  8. #define AMBIENT_RESULT_MSB 0x85 // high byte of ambient light measure
  9. #define AMBIENT_RESULT_LSB 0x86 // low byte of ambient light measure
  10. #define PROXIMITY_RESULT_MSB 0x87 // High byte of proximity measure
  11. #define PROXIMITY_RESULT_LSB 0x88 // low byte of proximity measure
  12. #define PROXIMITY_FREQ 0x89 // Proximity IR test signal freq, 0-3
  13. #define PROXIMITY_MOD 0x8A // proximity modulator timing
  14.  
  15. void setup() {
  16. Serial.begin(9600); // Serial's used to debug and print data
  17.  
  18. RGB.control(true);
  19. RGB.color(20, 0, 0);
  20.  
  21. delay(4000);
  22.  
  23. RGB.color(0, 20, 0);
  24.  
  25. Wire.begin(); // initialize I2C stuff
  26. Serial.println("starting ");
  27.  
  28. initVCNL4000();
  29. }
  30.  
  31. void loop(){
  32. Serial.println(Wire.available());
  33. delay(1000);
  34. return;
  35.  
  36. // unsigned int ambientValue = readAmbient(); //can a tiny bit slow
  37. unsigned int proximityValue = readProximity();
  38.  
  39. // Serial.print(ambientValue);
  40. // Serial.print(" | ");
  41. Serial.println(proximityValue);
  42.  
  43. delay(1000); //Just here to slow down the printing
  44. //note that the readings take about 100ms to execute
  45. }
  46.  
  47. void initVCNL4000(){
  48. byte temp = readVCNLByte(PRODUCT_ID);
  49. if (temp != 0x11){ // Product ID Should be 0x11
  50. Serial.print("initVCNL4000 failed to initialize");
  51. Serial.println(temp, HEX);
  52. }else{
  53. Serial.println("VNCL4000 Online...");
  54. }
  55.  
  56. /*VNCL400 init params
  57. Feel free to play with any of these values, but check the datasheet first!*/
  58. // writeVCNLByte(0x84, 0x0F); // Configures ambient light measures - Single conversion mode, 128 averages
  59. // writeVCNLByte(0x83, 15); // sets IR current in steps of 10mA 0-200mA --> 200mA
  60. // writeVCNLByte(0x89, 2); // Proximity IR test signal freq, 0-3 - 781.25 kHz
  61. // writeVCNLByte(0x8A, 0x81); // proximity modulator timing - 129, recommended by Vishay
  62. }
  63.  
  64.  
  65. unsigned int readProximity(){
  66. // readProximity() returns a 16-bit value from the VCNL4000's proximity data registers
  67. byte temp = readVCNLByte(0x80);
  68. writeVCNLByte(0x80, temp | 0x08); // command the sensor to perform a proximity measure
  69.  
  70. while(!(readVCNLByte(0x80)&0x20)); // Wait for the proximity data ready bit to be set
  71. unsigned int data = readVCNLByte(0x87) << 8;
  72. data |= readVCNLByte(0x88);
  73.  
  74. return data;
  75. }
  76.  
  77.  
  78. unsigned int readAmbient(){
  79. // readAmbient() returns a 16-bit value from the VCNL4000's ambient light data registers
  80. byte temp = readVCNLByte(0x80);
  81. writeVCNLByte(0x80, temp | 0x10); // command the sensor to perform ambient measure
  82.  
  83. while(!(readVCNLByte(0x80)&0x40)); // wait for the proximity data ready bit to be set
  84. unsigned int data = readVCNLByte(0x85) << 8;
  85. data |= readVCNLByte(0x86);
  86.  
  87. return data;
  88. }
  89.  
  90.  
  91. void writeVCNLByte(byte address, byte data){
  92. // writeVCNLByte(address, data) writes a single byte of data to address
  93. Wire.beginTransmission(VCNL4000_ADDRESS);
  94. Wire.write(address);
  95. Wire.write(data);
  96. Wire.endTransmission();
  97. }
  98.  
  99.  
  100. byte readVCNLByte(byte address){
  101. // readByte(address) reads a single byte of data from address
  102. Wire.beginTransmission(VCNL4000_ADDRESS);
  103. Wire.write(address);
  104. Wire.endTransmission();
  105. Wire.requestFrom(VCNL4000_ADDRESS, 1);
  106.  
  107. // while(!Wire.available());
  108.  
  109. byte data = Wire.read();
  110.  
  111. return data;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement