找回密码
 立即注册

QQ登录

只需一步,快速开始

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

求大佬帮忙看一下GY-530 VL53L0X arduino相关问题

[复制链接]
跳转到指定楼层
楼主
ID:307818 发表于 2018-4-23 10:42 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
找了许多资料,没有51的程序,只好用arduino的程序,但是自己没学过arduino,跟着网上教程看了几天,还是没有看懂

求大佬解释一下~有偿也行

//the original code by Ted Meyers

  1. #include <Wire.h>

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

  10. byte gbuf[16];

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

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

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

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

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

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

  35.   write_byte_data_at(VL53L0X_REG_SYSRANGE_START, 0x01);

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

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

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

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

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

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

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

  75. void write_word_data_at(byte reg, uint16_t data) {
  76.   // write data word at address and register
  77.   byte b0 = (data &0xFF);
  78.   byte b1 = ((data >> 8) && 0xFF);

  79.   Wire.beginTransmission(address);
  80.   Wire.write(reg);
  81.   Wire.write(b0);
  82.   Wire.write(b1);
  83.   Wire.endTransmission();
  84. }

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

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

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

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


  116. uint16_t VL53L0X_decode_vcsel_period(short vcsel_period_reg) {
  117.   // Converts the encoded VCSEL period register value into the real
  118.   // period in PLL clocks
  119.   uint16_t vcsel_period_pclks = (vcsel_period_reg + 1) << 1;
  120.   return vcsel_period_pclks;
  121. }
复制代码
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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