找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 5195|回复: 0
打印 上一主题 下一主题
收起左侧

VL53L0X arduino源代码+电路图+PDF资料

[复制链接]
跳转到指定楼层
楼主
电路原理图如下:


VL53L0X 的Arduino源代码
  1. //the original code by Ted Meyers

  2. #include <Wire.h>

  3. #define VL53L0X_REG_IDENTIFICATION_MODEL_ID         0xc0
  4. #define VL53L0X_REG_IDENTIFICATION_REVISION_ID      0xc2
  5. #define VL53L0X_REG_PRE_RANGE_CONFIG_VCSEL_PERIOD   0x50
  6. #define VL53L0X_REG_FINAL_RANGE_CONFIG_VCSEL_PERIOD 0x70
  7. #define VL53L0X_REG_SYSRANGE_START                  0x00
  8. #define VL53L0X_REG_RESULT_INTERRUPT_STATUS         0x13
  9. #define VL53L0X_REG_RESULT_RANGE_STATUS             0x14
  10. #define address 0x29

  11. byte gbuf[16];

  12. void setup() {
  13.   // put your setup code here, to run once:
  14.   Wire.begin();        // join i2c bus (address optional for master)
  15.   Serial.begin(9600);  // start serial for output
  16.   Serial.println("VLX53LOX test started.");
  17. }

  18. void loop() {
  19.   Serial.println("----- START TEST ----");
  20.   test();
  21.   Serial.println("----- END TEST ----");
  22.   Serial.println("");
  23.   delay(1000);
  24. }

  25. void test() {
  26.   byte val1 = read_byte_data_at(VL53L0X_REG_IDENTIFICATION_REVISION_ID);
  27.   Serial.print("Revision ID: "); Serial.println(val1);

  28.   val1 = read_byte_data_at(VL53L0X_REG_IDENTIFICATION_MODEL_ID);
  29.   Serial.print("Device ID: "); Serial.println(val1);

  30.   val1 = read_byte_data_at(VL53L0X_REG_PRE_RANGE_CONFIG_VCSEL_PERIOD);
  31.   Serial.print("PRE_RANGE_CONFIG_VCSEL_PERIOD="); Serial.println(val1);
  32.   Serial.print(" decode: "); Serial.println(VL53L0X_decode_vcsel_period(val1));

  33.   val1 = read_byte_data_at(VL53L0X_REG_FINAL_RANGE_CONFIG_VCSEL_PERIOD);
  34.   Serial.print("FINAL_RANGE_CONFIG_VCSEL_PERIOD="); Serial.println(val1);
  35.   Serial.print(" decode: "); Serial.println(VL53L0X_decode_vcsel_period(val1));

  36.   write_byte_data_at(VL53L0X_REG_SYSRANGE_START, 0x01);

  37.   byte val = 0;
  38.   int cnt = 0;
  39.   while (cnt < 100) { // 1 second waiting time max
  40.     delay(10);
  41.     val = read_byte_data_at(VL53L0X_REG_RESULT_RANGE_STATUS);
  42.     if (val & 0x01) break;
  43.     cnt++;
  44.   }
  45.   if (val & 0x01) Serial.println("ready"); else Serial.println("not ready");

  46.   read_block_data_at(0x14, 12);
  47.   uint16_t acnt = makeuint16(gbuf[7], gbuf[6]);
  48.   uint16_t scnt = makeuint16(gbuf[9], gbuf[8]);
  49.   uint16_t dist = makeuint16(gbuf[11], gbuf[10]);
  50.   byte DeviceRangeStatusInternal = ((gbuf[0] & 0x78) >> 3);

  51.   Serial.print("ambient count: "); Serial.println(acnt);
  52.   Serial.print("signal count: ");  Serial.println(scnt);
  53.   Serial.print("distance ");       Serial.println(dist);
  54.   Serial.print("status: ");        Serial.println(DeviceRangeStatusInternal);
  55. }

  56. uint16_t bswap(byte b[]) {
  57.   // Big Endian unsigned short to little endian unsigned short
  58.   uint16_t val = ((b[0] << 8) & b[1]);
  59.   return val;
  60. }

  61. uint16_t makeuint16(int lsb, int msb) {
  62.     return ((msb & 0xFF) << 8) | (lsb & 0xFF);
  63. }

  64. void write_byte_data(byte data) {
  65.   Wire.beginTransmission(address);
  66.   Wire.write(data);
  67.   Wire.endTransmission();
  68. }

  69. void write_byte_data_at(byte reg, byte data) {
  70.   // write data word at address and register
  71.   Wire.beginTransmission(address);
  72.   Wire.write(reg);
  73.   Wire.write(data);
  74.   Wire.endTransmission();
  75. }

  76. void write_word_data_at(byte reg, uint16_t data) {
  77.   // write data word at address and register
  78.   byte b0 = (data &0xFF);
  79.   byte b1 = ((data >> 8) && 0xFF);
  80.    
  81.   Wire.beginTransmission(address);
  82.   Wire.write(reg);
  83.   Wire.write(b0);
  84.   Wire.write(b1);
  85.   Wire.endTransmission();
  86. }

  87. byte read_byte_data() {
  88.   Wire.requestFrom(address, 1);
  89.   while (Wire.available() < 1) delay(1);
  90.   byte b = Wire.read();
  91.   return b;
  92. }

  93. byte read_byte_data_at(byte reg) {
  94.   //write_byte_data((byte)0x00);
  95.   write_byte_data(reg);
  96.   Wire.requestFrom(address, 1);
  97.   while (Wire.available() < 1) delay(1);
  98.   byte b = Wire.read();
  99.   return b;
  100. }

  101. uint16_t read_word_data_at(byte reg) {
  102.   write_byte_data(reg);
  103.   Wire.requestFrom(address, 2);
  104.   while (Wire.available() < 2) delay(1);
  105.   gbuf[0] = Wire.read();
  106.   gbuf[1] = Wire.read();
  107.   return bswap(gbuf);
  108. }

  109. void read_block_data_at(byte reg, int sz) {
  110.   int i = 0;
  111.   write_byte_data(reg);
  112.   Wire.requestFrom(address, sz);
  113.   for (i=0; i<sz; i++) {
  114.     while (Wire.available() < 1) delay(1);
  115.     gbuf[i] = Wire.read();
  116.   }
  117. }


  118. uint16_t VL53L0X_decode_vcsel_period(short vcsel_period_reg) {
  119.   // Converts the encoded VCSEL period register value into the real
  120.   // period in PLL clocks
  121.   uint16_t vcsel_period_pclks = (vcsel_period_reg + 1) << 1;
  122.   return vcsel_period_pclks;
  123. }
复制代码
全部资料51hei下载地址:
GY-530 VL53L0X激光测距传感器模块 ToF测距 飞行时间测距传感器.rar (1.01 MB, 下载次数: 42)

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶1 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表