ToF_VL53L0X.ino 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. Description: Use ToF Unit to detect distance and display distance data on the screen in real time.
  3. */
  4. #include <M5Stack.h>
  5. #include <Wire.h>
  6. #define VL53L0X_REG_IDENTIFICATION_MODEL_ID 0xc0
  7. #define VL53L0X_REG_IDENTIFICATION_REVISION_ID 0xc2
  8. #define VL53L0X_REG_PRE_RANGE_CONFIG_VCSEL_PERIOD 0x50
  9. #define VL53L0X_REG_FINAL_RANGE_CONFIG_VCSEL_PERIOD 0x70
  10. #define VL53L0X_REG_SYSRANGE_START 0x00
  11. #define VL53L0X_REG_RESULT_INTERRUPT_STATUS 0x13
  12. #define VL53L0X_REG_RESULT_RANGE_STATUS 0x14
  13. #define address 0x29
  14. byte gbuf[16];
  15. void setup() {
  16. // put your setup code here, to run once:
  17. Wire.begin(); // join i2c bus (address optional for master)
  18. Serial.begin(115200); // start serial for output
  19. Serial.println("VLX53LOX test started.");
  20. //---osmar
  21. M5.begin();
  22. M5.Power.begin();
  23. M5.Lcd.fillScreen(BLACK);
  24. M5.Lcd.setTextColor(YELLOW);
  25. M5.Lcd.setCursor(50, 0, 4);
  26. M5.Lcd.println(("VLX53LOX Example"));
  27. M5.Lcd.setTextColor(WHITE);
  28. //---osmar
  29. }
  30. void loop() {
  31. Serial.println("----- START TEST ----");
  32. test();
  33. Serial.println("----- END TEST ----");
  34. Serial.println("");
  35. delay(500);
  36. }
  37. void test() {
  38. byte val1 = read_byte_data_at(VL53L0X_REG_IDENTIFICATION_REVISION_ID);
  39. Serial.print("Revision ID: "); Serial.println(val1);
  40. val1 = read_byte_data_at(VL53L0X_REG_IDENTIFICATION_MODEL_ID);
  41. Serial.print("Device ID: "); Serial.println(val1);
  42. val1 = read_byte_data_at(VL53L0X_REG_PRE_RANGE_CONFIG_VCSEL_PERIOD);
  43. Serial.print("PRE_RANGE_CONFIG_VCSEL_PERIOD="); Serial.println(val1);
  44. Serial.print(" decode: "); Serial.println(VL53L0X_decode_vcsel_period(val1));
  45. val1 = read_byte_data_at(VL53L0X_REG_FINAL_RANGE_CONFIG_VCSEL_PERIOD);
  46. Serial.print("FINAL_RANGE_CONFIG_VCSEL_PERIOD="); Serial.println(val1);
  47. Serial.print(" decode: "); Serial.println(VL53L0X_decode_vcsel_period(val1));
  48. write_byte_data_at(VL53L0X_REG_SYSRANGE_START, 0x01);
  49. byte val = 0;
  50. int cnt = 0;
  51. while (cnt < 100) { // 1 second waiting time max
  52. delay(10);
  53. val = read_byte_data_at(VL53L0X_REG_RESULT_RANGE_STATUS);
  54. if (val & 0x01) break;
  55. cnt++;
  56. }
  57. if (val & 0x01) Serial.println("ready"); else Serial.println("not ready");
  58. read_block_data_at(0x14, 12);
  59. uint16_t acnt = makeuint16(gbuf[7], gbuf[6]);
  60. uint16_t scnt = makeuint16(gbuf[9], gbuf[8]);
  61. uint16_t dist = makeuint16(gbuf[11], gbuf[10]);
  62. byte DeviceRangeStatusInternal = ((gbuf[0] & 0x78) >> 3);
  63. Serial.print("ambient count: "); Serial.println(acnt);
  64. Serial.print("signal count: "); Serial.println(scnt);
  65. Serial.print("distance "); Serial.println(dist);
  66. Serial.print("status: "); Serial.println(DeviceRangeStatusInternal);
  67. // M5.Lcd.fillScreen(BLACK);
  68. // M5.Lcd.setTextColor(YELLOW);
  69. // M5.Lcd.setCursor(50, 0, 4);
  70. // M5.Lcd.println(("GPS Raw Example"));
  71. M5.Lcd.fillRect(0, 80, 319, 239, BLACK);
  72. M5.Lcd.setTextColor(WHITE);
  73. M5.Lcd.setCursor(100, 100, 4);
  74. M5.Lcd.printf("D:%d ",dist);
  75. }
  76. uint16_t bswap(byte b[]) {
  77. // Big Endian unsigned short to little endian unsigned short
  78. uint16_t val = ((b[0] << 8) & b[1]);
  79. return val;
  80. }
  81. uint16_t makeuint16(int lsb, int msb) {
  82. return ((msb & 0xFF) << 8) | (lsb & 0xFF);
  83. }
  84. void write_byte_data(byte data) {
  85. Wire.beginTransmission(address);
  86. Wire.write(data);
  87. Wire.endTransmission();
  88. }
  89. void write_byte_data_at(byte reg, byte data) {
  90. // write data word at address and register
  91. Wire.beginTransmission(address);
  92. Wire.write(reg);
  93. Wire.write(data);
  94. Wire.endTransmission();
  95. }
  96. void write_word_data_at(byte reg, uint16_t data) {
  97. // write data word at address and register
  98. byte b0 = (data &0xFF);
  99. byte b1 = ((data >> 8) && 0xFF);
  100. Wire.beginTransmission(address);
  101. Wire.write(reg);
  102. Wire.write(b0);
  103. Wire.write(b1);
  104. Wire.endTransmission();
  105. }
  106. byte read_byte_data() {
  107. Wire.requestFrom(address, 1);
  108. while (Wire.available() < 1) delay(1);
  109. byte b = Wire.read();
  110. return b;
  111. }
  112. byte read_byte_data_at(byte reg) {
  113. //write_byte_data((byte)0x00);
  114. write_byte_data(reg);
  115. Wire.requestFrom(address, 1);
  116. while (Wire.available() < 1) delay(1);
  117. byte b = Wire.read();
  118. return b;
  119. }
  120. uint16_t read_word_data_at(byte reg) {
  121. write_byte_data(reg);
  122. Wire.requestFrom(address, 2);
  123. while (Wire.available() < 2) delay(1);
  124. gbuf[0] = Wire.read();
  125. gbuf[1] = Wire.read();
  126. return bswap(gbuf);
  127. }
  128. void read_block_data_at(byte reg, int sz) {
  129. int i = 0;
  130. write_byte_data(reg);
  131. Wire.requestFrom(address, sz);
  132. for (i=0; i<sz; i++) {
  133. while (Wire.available() < 1) delay(1);
  134. gbuf[i] = Wire.read();
  135. }
  136. }
  137. uint16_t VL53L0X_decode_vcsel_period(short vcsel_period_reg) {
  138. // Converts the encoded VCSEL period register value into the real
  139. // period in PLL clocks
  140. uint16_t vcsel_period_pclks = (vcsel_period_reg + 1) << 1;
  141. return vcsel_period_pclks;
  142. }