找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 10831|回复: 0
收起左侧

GY-530 VL53L0X的ARDUINO源程序和原理图 可实现激光测距

[复制链接]
ID:242296 发表于 2017-10-24 00:20 | 显示全部楼层 |阅读模式
GY-530 VL53L0X
0.png
模块原理图:
0.png

ARDUINO源程序:

  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.    
  80.   Wire.beginTransmission(address);
  81.   Wire.write(reg);
  82.   Wire.write(b0);
  83.   Wire.write(b1);
  84.   Wire.endTransmission();
  85. }

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

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

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

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


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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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