找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3017|回复: 2
收起左侧

VL53L0x激光测距STM32源程序

[复制链接]
ID:547839 发表于 2020-2-17 20:28 | 显示全部楼层 |阅读模式
激光测距资料,移植参考的资料

单片机源程序如下:
  1. #include "vl53l0.h"
  2. #include "usart.h"       

  3. u8 sendbuff[6]={0,0,0,0,0,0};


  4. VL53L0X_Dev_t vl53l0x_dev;
  5. VL53L0X_DeviceInfo_t vl53l0x_dev_info;
  6. VL53L0X_RangingMeasurementData_t vl53l0x_data;
  7. VL53L0X_Error vl53l0x_status;

  8. /***************************************************************************************/

  9. void print_pal_error(VL53L0X_Error Status)
  10. {
  11.     char buf[VL53L0X_MAX_STRING_LENGTH];
  12.     VL53L0X_GetPalErrorString(Status, buf);  //得到错误码与字符串
  13.     printf("API Status: %i : %s\n", Status, buf);
  14. }

  15. void print_range_status(VL53L0X_RangingMeasurementData_t* pdata)
  16. {
  17.     char buf[VL53L0X_MAX_STRING_LENGTH];
  18.     uint8_t RangeStatus;
  19.     // New Range Status: data is valid when pdata->RangeStatus = 0
  20.     RangeStatus = pdata->RangeStatus;

  21.     VL53L0X_GetRangeStatusString(RangeStatus, buf);
  22.     printf("Range Status: %i : %s\n", RangeStatus, buf);
  23. }

  24. VL53L0X_Error vl53l0x_start_single_test(VL53L0X_Dev_t *pdev, \
  25.                             VL53L0X_RangingMeasurementData_t *pdata)
  26. {
  27.                 int i=0,j=0,sum=0;
  28.     VL53L0X_Error status = VL53L0X_ERROR_NONE;
  29.    
  30.     if(vl53l0x_status != VL53L0X_ERROR_NONE)
  31.         return vl53l0x_status;

  32.     status = VL53L0X_PerformSingleRangingMeasurement(pdev, pdata);   //VL53L0X执行单一测量范围
  33.     if(status != VL53L0X_ERROR_NONE){
  34.         printf("error:Call of VL53L0X_PerformSingleRangingMeasurement\n");
  35.         return status;
  36.     }
  37.     print_range_status(pdata);
  38.                 for(i=0;i<5;i++)
  39.                         sum+=pdata->RangeMilliMeter;
  40.                 pdata->RangeMilliMeter=sum/5;
  41.     printf("Measured distance: %i\n\n", pdata->RangeMilliMeter);
  42.                
  43.                 sendbuff[0]=200;
  44.                 sendbuff[1]=201;
  45.                 sendbuff[2]=pdata->RangeMilliMeter%100;
  46.                 sendbuff[3]=pdata->RangeMilliMeter/100;
  47.                 sendbuff[4]=data1;
  48.                 sendbuff[5]=data2;
  49.                
  50.                 for(j=0;j<6;j++)
  51.                 {
  52.                         USART1->DR = sendbuff[j];
  53.                         while((USART1->SR&0X40)==0);//循环发送,直到发送完毕
  54.                 }
  55.     return status;
  56. }


  57. VL53L0X_Error vl53l0x_measure_init(VL53L0X_Dev_t *pMyDevice)
  58. {
  59.     VL53L0X_Error Status = VL53L0X_ERROR_NONE;

  60.     //FixPoint1616_t LimitCheckCurrent;
  61.     uint32_t refSpadCount;
  62.     uint8_t isApertureSpads;
  63.     uint8_t VhvSettings;
  64.     uint8_t PhaseCal;
  65.     // Device Initialization
  66.     Status = VL53L0X_StaticInit(pMyDevice);   //基本设备初始化
  67.     if(Status != VL53L0X_ERROR_NONE){         //判断是否初始化成功 失败打印错误信息
  68.         printf ("Call of VL53L0X_StaticInit\n");
  69.         print_pal_error(Status);
  70.         return Status;
  71.     }
  72.     // Device Initialization
  73.     Status = VL53L0X_PerformRefCalibration(pMyDevice, &VhvSettings, &PhaseCal);    //执行参考校准
  74.     if(Status != VL53L0X_ERROR_NONE){        //判断是否校准成功
  75.         printf ("Call of VL53L0X_PerformRefCalibration\n");
  76.         print_pal_error(Status);
  77.         return Status;
  78.     }

  79.     // needed if a coverglass is used and no calibration has been performed
  80.     Status = VL53L0X_PerformRefSpadManagement(pMyDevice,&refSpadCount, &isApertureSpads);
  81.     if(Status != VL53L0X_ERROR_NONE){
  82.         printf ("Call of VL53L0X_PerformRefSpadManagement\n");
  83.         printf ("refSpadCount = %d, isApertureSpads = %d\n", refSpadCount, isApertureSpads);
  84.         print_pal_error(Status);
  85.         return Status;
  86.     }

  87.     // no need to do this when we use VL53L0X_PerformSingleRangingMeasurement  当我们使用VL53L0X_PerformSingleRangingMeasurement不需要
  88.     Status = VL53L0X_SetDeviceMode(pMyDevice, VL53L0X_DEVICEMODE_SINGLE_RANGING); // Setup in single ranging mode 设置单一测量模式
  89.     if(Status != VL53L0X_ERROR_NONE){
  90.         printf ("Call of VL53L0X_SetDeviceMode\n");
  91.         print_pal_error(Status);
  92.         return Status;
  93.     }

  94.     // Enable/Disable Sigma and Signal check
  95.     if(Status == VL53L0X_ERROR_NONE){
  96.         Status = VL53L0X_SetLimitCheckEnable(pMyDevice,  //启用/禁用特定限制检查
  97.                         VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE, 1);
  98.     }
  99.     if(Status == VL53L0X_ERROR_NONE){
  100.         Status = VL53L0X_SetLimitCheckEnable(pMyDevice,
  101.                         VL53L0X_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, 1);
  102.     }
  103.                                
  104.     if(Status == VL53L0X_ERROR_NONE){
  105.         Status = VL53L0X_SetLimitCheckValue(pMyDevice,
  106.                         VL53L0X_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE,
  107.                         (FixPoint1616_t)(0.25*65536));
  108.         }                       
  109.     if(Status == VL53L0X_ERROR_NONE){
  110.         Status = VL53L0X_SetLimitCheckValue(pMyDevice,
  111.                         VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE,
  112.                         (FixPoint1616_t)(18*65536));                       
  113.     }
  114.     if(Status == VL53L0X_ERROR_NONE){
  115.         Status = VL53L0X_SetMeasurementTimingBudgetMicroSeconds(pMyDevice,   //设置微秒时间
  116.                         200000);
  117.     }
  118.     if(Status != VL53L0X_ERROR_NONE){
  119.         printf ("Sigma and Signal check error\n");
  120.         print_pal_error(Status);
  121.         return Status;
  122.     }
  123.     /*
  124.      *  Step  4 : Test ranging mode
  125.      */
  126.   /*  for(i=0;i<10;i++){
  127.             printf ("Call of VL53L0X_PerformSingleRangingMeasurement\n");
  128.             Status = VL53L0X_PerformSingleRangingMeasurement(pMyDevice,
  129.                             &vl53l0x_data);

  130.             print_pal_error(Status);
  131.             print_range_status(&vl53l0x_data);

  132.             VL53L0X_GetLimitCheckCurrent(pMyDevice,
  133.                             VL53L0X_CHECKENABLE_RANGE_IGNORE_THRESHOLD, &LimitCheckCurrent);

  134.             printf("RANGE IGNORE THRESHOLD: %f\n\n", (float)LimitCheckCurrent/65536.0);


  135.             if (Status != VL53L0X_ERROR_NONE) break;

  136.             printf("Measured distance: %i\n\n", vl53l0x_data.RangeMilliMeter);

  137.     }
  138. */
  139.     return Status;   //返回0
  140. }

  141. VL53L0X_Error vl53l0x_init(void)
  142. {
  143.     VL53L0X_Error Status = VL53L0X_ERROR_NONE;   //初始值赋值为0

  144.     VL53L0X_Dev_t *pMyDevice = &vl53l0x_dev;       //得到设备地址

  145.     pMyDevice->I2cDevAddr      = 0x52;            //iic地址
  146.     pMyDevice->comms_type      =  1;              //选择IIC还是SPI    iic=1;SPI=0
  147.     pMyDevice->comms_speed_khz =  400;            //iic速率
  148.    
  149.     VL53L0X_i2c_init();          //IIC配置   

  150.     Status = VL53L0X_DataInit(pMyDevice); // Data initialization  //VL53L0X_DataInit:一次设备的初始化,初始化成功返回0
  151.     if(Status != VL53L0X_ERROR_NONE){     //判断如果状态不为0   打印错误信息
  152.         print_pal_error(Status);
  153.         return Status;        //  返回错误值 可通过此值DEBUG查找错误位置
  154.     }

  155.     Status = VL53L0X_GetDeviceInfo(pMyDevice, &vl53l0x_dev_info);   //读取给定设备的设备信息
  156.     if(Status != VL53L0X_ERROR_NONE){
  157.         print_pal_error(Status);
  158.         return Status;
  159.     }
  160.     printf("VL53L0X_GetDeviceInfo:\n");
  161.     printf("Device Name : %s\n", vl53l0x_dev_info.Name);     //设备名
  162.     printf("Device Type : %s\n", vl53l0x_dev_info.Type);    //产品类型VL53L0X = 1, VL53L1 = 2
  163.     printf("Device ID : %s\n", vl53l0x_dev_info.ProductId);   // 设备ID
  164.     printf("ProductRevisionMajor : %d\n", vl53l0x_dev_info.ProductRevisionMajor);
  165.     printf("ProductRevisionMinor : %d\n", vl53l0x_dev_info.ProductRevisionMinor);

  166.     if ((vl53l0x_dev_info.ProductRevisionMajor != 1) && (vl53l0x_dev_info.ProductRevisionMinor != 1)){
  167.         printf("Error expected cut 1.1 but found cut %d.%d\n",
  168.         vl53l0x_dev_info.ProductRevisionMajor, vl53l0x_dev_info.ProductRevisionMinor);
  169.         Status = VL53L0X_ERROR_NOT_SUPPORTED;
  170.         print_pal_error(Status);
  171.         return Status;
  172.     }

  173.     Status = vl53l0x_measure_init(pMyDevice);   //测量配置
  174.     vl53l0x_status = Status;
  175.     if(Status != VL53L0X_ERROR_NONE){    //判断如果不为0打印错误信息
  176.         print_pal_error(Status);
  177.         return Status;
  178.     }

  179.     return Status;           //返回0
  180. }
复制代码
全部资料51hei下载地址:
VL53L0X激光测距传感器.7z (449.71 KB, 下载次数: 76)
回复

使用道具 举报

ID:511225 发表于 2020-3-1 21:21 | 显示全部楼层
谢谢大佬,请问有sstm32f4的激光测距程序吗
回复

使用道具 举报

ID:1068030 发表于 2023-4-6 16:17 | 显示全部楼层
他这个现象是什么
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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