#Region Project Attributes
#AutoFlushLogs: True
#CheckArrayBounds: True
#StackBufferSize: 300
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'Public variables can be accessed from all modules.
Public Serial1 As Serial
Private Master As WireMaster
Private timer1 As Timer
Private const VL53L0X_REG_IDENTIFICATION_MODEL_ID=0xc0 As Byte
Private const VL53L0X_REG_IDENTIFICATION_REVISION_ID=0xc2 As Byte
Private const VL53L0X_REG_PRE_RANGE_CONFIG_VCSEL_PERIOD=0x50 As Byte
Private const VL53L0X_REG_FINAL_RANGE_CONFIG_VCSEL_PERIOD=0x70 As Byte
Private const VL53L0X_REG_SYSRANGE_START=0x00 As Byte
' Private const VL53L0X_REG_RESULT_INTERRUPT_STATUS=0x13 As Byte
Private const VL53L0X_REG_RESULT_RANGE_STATUS=0x14 As Byte
Private const VL53L0X_address=0x29 As Byte
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
Log("AppStart")
timer1.Initialize("Timer1_Tick",1000)
Master.Initialize
Log("I2C Init")
Get_Info
timer1.Enabled=True
End Sub
Private Sub Timer1_Tick
Get_Range
End Sub
'print out chip info
Private Sub Get_Info
Private readbyte(1) As Byte
Private writebyte(1) As Byte
writebyte(0)=VL53L0X_REG_IDENTIFICATION_MODEL_ID
Master.WriteTo2(VL53L0X_address,True,writebyte)
readbyte = Master.RequestFrom(VL53L0X_address,1)
Log("Revision ID: ",readbyte(0))
writebyte(0)=VL53L0X_REG_IDENTIFICATION_REVISION_ID
Master.WriteTo2(VL53L0X_address,True,writebyte)
readbyte = Master.RequestFrom(VL53L0X_address,1)
Log("Device ID: ",readbyte(0))
writebyte(0)=VL53L0X_REG_PRE_RANGE_CONFIG_VCSEL_PERIOD
Master.WriteTo2(VL53L0X_address,True,writebyte)
readbyte = Master.RequestFrom(VL53L0X_address,1)
Log("Prerange: ",readbyte(0))
Log("Decode: ",Bit.ShiftRight(readbyte(0)+1,1))
writebyte(0)=VL53L0X_REG_FINAL_RANGE_CONFIG_VCSEL_PERIOD
Master.WriteTo2(VL53L0X_address,True,writebyte)
readbyte = Master.RequestFrom(VL53L0X_address,1)
Log("Prerange: ",readbyte(0))
Log("Decode: ",Bit.ShiftRight(readbyte(0)+1,1))
Log("--")
End Sub
'call to get range reading in mm
private Sub Get_Range
Send_Start 'must call before each reading
Private readbyte(1) As Byte
Private writebyte(1) As Byte
writebyte(0)=VL53L0X_REG_RESULT_RANGE_STATUS
Private ready As Boolean = False
Private count As Int = 0
'wait for data to be ready
Do Until ready
Master.WriteTo2(VL53L0X_address,True,writebyte)
readbyte = Master.RequestFrom(VL53L0X_address,1)
If readbyte.Length=1 Then
If Bit.Get(readbyte(0),0)=1 Then ready=True
Else
Log("length error ",readbyte.Length)
End If
count=count+1
If count > 100 Then Exit
' Log(count," ",Bit.ToBinaryString(readbyte(0)))
Loop
'now retrieve data
writebyte(0)=VL53L0X_REG_RESULT_RANGE_STATUS
Master.WriteTo2(VL53L0X_address,True,writebyte)
readbyte = Master.RequestFrom(VL53L0X_address,12)
If readbyte.Length=12 Then
Private acnt, scnt, dist, dev_range_status_int As UInt
acnt=Bit.ShiftLeft(readbyte(6),8)+ readbyte(7)
scnt=Bit.ShiftLeft(readbyte(8),8)+ readbyte(9)
dist=Bit.ShiftLeft(readbyte(10),8)+ readbyte(11)
dev_range_status_int=Bit.ShiftRight(Bit.And(readbyte(0),0x78),3)
Log(acnt," ",scnt," ",dist," ",dev_range_status_int)
End If
End Sub
Private Sub Send_Start
Private writebyte(2) As Byte
writebyte(0)=VL53L0X_REG_SYSRANGE_START
writebyte(1)=0x01
Master.WriteTo2(VL53L0X_address,True,writebyte)
End Sub