Proton BASIC Compiler - VL53L0X laser-ranging sensor sample code in PIC16F628A


  • PicŪ Basic


  • VL53L0X laser-ranging sensor sample code for PIC16F628A

    The VL53L0X is a new generation Time-of-Flight (ToF) laser-ranging module housed in the smallest package on the market today, providing accurate distance measurement whatever the target reflectances unlike conventional technologies. It can measure absolute distances up to 2m, setting a new benchmark in ranging performance levels, opening the door to various new applications.




    This Code was created from other codes based in Arduino, C and Python.

    Code:
    '****************************************************************
    '*  Name    : VL53L0x.BAS                                       *
    '*  Author  : MTH                                               *
    '*  Notice  : Copyright (c) 2017 SaitekControl Argentina        *
    '*          : All Rights Reserved                               *
    '*  Date    : 20/12/2017                                        *
    '*  Version : 1.0                                               *
    '*  Notes   :                                                   *
    '*          :                                                   *
    '****************************************************************
    Device = 16F628A
    
    
    Config FOSC_INTOSCIO, WDTE_OFF, PWRTE_OFF, MCLRE_OFF, BOREN_OFF, LVP_OFF, CPD_OFF, CP_OFF
    
    
    ;**** End of Fuse Configurator Settings ****
    ;-------------------------------------------------------------------------------
    
    
    'Config 16F628A
    Xtal 4
    CMCON = 7
    VRCON = 0
    Declare PortB_Pullups = Off
    Declare Watchdog =Off
    Declare All_Digital On
    
    
    
    
    'Ports i2C VL53L0x
    Symbol SDA = PORTA.0
    Symbol SCL = PORTA.1
    'Port Serial monitor
    Symbol Data_TX = PORTA.2
    
    
    'VL53L0x Registers Write and Read
    Symbol W_ADDRESS    = $52
    Symbol R_ADDRESS    = $53
    
    
    
    
    'Variables
    Dim i As Byte
    Dim DatoVL As Byte
    Dim DatoSensor[12] As Byte
    Dim Distance As Word
    Dim devError As Byte
    Dim SPAD As Float
    Dim Signal As Word
    Dim Ambient As Word
    'Config TTL Serial
    Declare Serial_Baud 4800  
    Declare RsOut_Pin Data_TX
    Declare RsOut_Mode TRUE 
    High Data_TX
    
    DelayMS 100
    
    
    
    
    
    
    'Send info data
    RsOut 13,10,13,10,13,10
     RsOut "Test sensor VL53L0x V13-12-17 by MTH Argentina",13,10,13,10
     GoSub Info_VL53
    
    
     DelayMS 1000
    
    'Loop of program
    While 1=1
    Clrwdt
    'Sys range start
    I2COut SDA,SCL,W_ADDRESS,[$00,$01]
      DelayMS 5
    'Range status 
    I2COut SDA,SCL,W_ADDRESS,[$14]
      DelayMS 5                                
    'Read 12 raw data of Sensor:
    I2CIn  SDA,SCL,R_ADDRESS,[Str DatoSensor]
    '  'Activate for view raw data
    '  If 1=0 Then
    '   For i=0 To 11
    [I]'    RsOut Dec3 DatoSensor
    '    If i<11 Then RsOut ","
    '   Next i
    '   RsOut 13,10
    '  EndIf
      'Resolve sensor error list:
    devError = DatoSensor[0] & 0x78 >> 3 ' // Check for errors
    RsOut "Status: "
    If devError = $00 Then  RsOut "Data OK!" ';// No device error
    If devError = $01 Then  RsOut "VCSEL CONTINUITY TEST FAILURE!"
      If devError = $02 Then  RsOut "VCSEL WATCHDOG TEST FAILURE!"
    If devError = $03 Then  RsOut "NO VHV VALUE FOUND!"
    If devError = $04 Then  RsOut "MSRC NO TARGET!"
    If devError = $05 Then  RsOut "SNR CHECK!"
    If devError = $06 Then  RsOut "RANGE PHASE CHECK!"
      If devError = $07 Then  RsOut "SIGMA THRESHOLD CHECK!"
    If devError = $08 Then  RsOut "TCC!"
    If devError = $09 Then  RsOut "PHASE CONSISTENCY!"
    If devError = $0A Then  RsOut "MIN CLIP!"
    If devError = $0B Then  RsOut "RANGE COMPLETE!"
    If devError = $0C Then  RsOut "ALGO UNDERFLOW!"
    If devError = $0D Then  RsOut "ALGO OVERFLOW!"
      If devError = $0E Then  RsOut "RANGE IGNORE THRESHOLD!"
    RsOut "(",Hex2 devError,")",13,10
    
    
    'Resolve info of sensor VL53L0x:
      'If devError = $00 or devError = $06 update Distance
      'If devError = $00 Or devError = $06Then 
    Distance=DatoSensor[10]*256+DatoSensor[11] 
    'endif
      'Effective SPAD Return Count        
    SPAD = DatoSensor[2] + DatoSensor[3]/255
      'Signal Rate
    Signal = DatoSensor[6]*256+DatoSensor[7]
      'Ambient Rate 
    Ambient= DatoSensor[8]*256+DatoSensor[9]
    
    
    
    
    'Send terminal info:
    RsOut "Distance: ",Dec Distance," mm",13,10
      RsOut "Ambient Rate = ",Dec Ambient,"mc/s",13,10
      RsOut "Signal Rate = ",Dec Signal,"mc/s",13,10
      RsOut "SPAD = ",Dec SPAD,13,10
    
    'wait a moment for view info
    DelayMS 1000
     Wend
    'Read sensor info 
    Info_VL53:
    'IDENTIFICATION REVISION ID
    I2CIn  SDA,SCL,R_ADDRESS,$C2,[DatoVL]
      RsOut "Revision ID: ",Hex2 DatoVL,13,10
    
    
    'IDENTIFICATION MODEL ID
    I2CIn  SDA,SCL,R_ADDRESS,$C0,[DatoVL]
      RsOut "Device ID: ",Hex2 DatoVL,13,10
    
    
    'PRE RANGE CONFIG VCSEL PERIOD
    I2CIn  SDA,SCL,R_ADDRESS,$50,[DatoVL]
      RsOut "Pre Range Config Period: ",Hex2 DatoVL,13,10 
    
    
    'FINAL RANGE CONFIG VCSEL PERIOD
    I2CIn  SDA,SCL,R_ADDRESS,$70,[DatoVL]
      RsOut "Final Period: ",Hex2 DatoVL,13,10
    
    
     Return
    Continue updating this document by adding images of the tests performed.


    Thank you