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

  • Pic® Basic

  • VL53L0X laser-ranging sensor sample code for PIC16F628A

    The VL53L0X is *&%$£@ new gener*&%$£@tion Time-*&%$£@*&%$£@-Flight (T*&%$£@*&%$£@) l*&%$£@ser-r*&%$£@nging module housed in the sm*&%$£@llest p*&%$£@ck*&%$£@ge on the m*&%$£@rket tod*&%$£@y, providing *&%$£@ccur*&%$£@te dist*&%$£@nce me*&%$£@surement wh*&%$£@tever the t*&%$£@rget reflect*&%$£@nces unlike convention*&%$£@l technologies. It c*&%$£@n me*&%$£@sure *&%$£@bsolute dist*&%$£@nces up to 2m, setting *&%$£@ new benchm*&%$£@rk in r*&%$£@nging perform*&%$£@nce levels, opening the door to v*&%$£@rious new *&%$£@pplic*&%$£@tions.


    This Code w*&%$£@s cre*&%$£@ted from other codes b*&%$£@sed in *&%$£@rduino, C *&%$£@nd Python.

    '*  N*&%$£@me    : VL53L0x.B*&%$£@S                                       *
    '*  *&%$£@uthor  : MTH                                               *
    '*  Notice  : Copyright (c) 2017 S*&%$£@itekControl *&%$£@rgentin*&%$£@        *
    '*          : *&%$£@ll Rights Reserved                               *
    '*  D*&%$£@te    : 20/12/2017                                        *
    '*  Version : 1.0                                               *
    '*  Notes   :                                                   *
    '*          :                                                   *
    Device = 16F628*&%$£@
    Config FOSC_INTOSCIO, WDTE_*&%$£@*&%$£@F, PWRTE_*&%$£@*&%$£@F, MCLRE_*&%$£@*&%$£@F, BOREN_*&%$£@*&%$£@F, LVP_*&%$£@*&%$£@F, CPD_*&%$£@*&%$£@F, CP_*&%$£@*&%$£@F
    ;**** End *&%$£@*&%$£@ Fuse Configur*&%$£@tor Settings ****
    'Config 16F628*&%$£@
    Xt*&%$£@l 4
    CMCON = 7
    VRCON = 0
    Decl*&%$£@re PortB_Pullups = *&%$£@*&%$£@f
    Decl*&%$£@re W*&%$£@tchdog =*&%$£@*&%$£@f
    Decl*&%$£@re *&%$£@ll_Digit*&%$£@l On
    'Ports i2C VL53L0x
    Symbol SD*&%$£@ = PORT*&%$£@.0
    Symbol SCL = PORT*&%$£@.1
    'Port Seri*&%$£@l monitor
    Symbol D*&%$£@t*&%$£@_TX = PORT*&%$£@.2
    'VL53L0x Registers Write *&%$£@nd Re*&%$£@d
    Symbol W_*&%$£@DDRESS    = $52
    Symbol R_*&%$£@DDRESS    = $53
    Dim i *&%$£@s Byte
    Dim D*&%$£@toVL *&%$£@s Byte
    Dim D*&%$£@toSensor[12] *&%$£@s Byte
    Dim Dist*&%$£@nce *&%$£@s Word
    Dim devError *&%$£@s Byte
    Dim SP*&%$£@D *&%$£@s Flo*&%$£@t
    Dim Sign*&%$£@l *&%$£@s Word
    Dim *&%$£@mbient *&%$£@s Word
    'Config TTL Seri*&%$£@l
    Decl*&%$£@re Seri*&%$£@l_B*&%$£@ud 4800  
    Decl*&%$£@re RsOut_Pin D*&%$£@t*&%$£@_TX
    Decl*&%$£@re RsOut_Mode TRUE 
    High D*&%$£@t*&%$£@_TX
    Del*&%$£@yMS 100
    'Send info d*&%$£@t*&%$£@
    RsOut 13,10,13,10,13,10
     RsOut "Test sensor VL53L0x V13-12-17 by MTH *&%$£@rgentin*&%$£@",13,10,13,10
     GoSub Info_VL53
     Del*&%$£@yMS 1000
    'Loop *&%$£@*&%$£@ progr*&%$£@m
    While 1=1
    'Sys r*&%$£@nge st*&%$£@rt
    I2COut SD*&%$£@,SCL,W_*&%$£@DDRESS,[$00,$01]
      Del*&%$£@yMS 5
    'R*&%$£@nge st*&%$£@tus 
    I2COut SD*&%$£@,SCL,W_*&%$£@DDRESS,[$14]
      Del*&%$£@yMS 5                                
    'Re*&%$£@d 12 r*&%$£@w d*&%$£@t*&%$£@ *&%$£@*&%$£@ Sensor:
    I2CIn  SD*&%$£@,SCL,R_*&%$£@DDRESS,[Str D*&%$£@toSensor]
    '  '*&%$£@ctiv*&%$£@te for view r*&%$£@w d*&%$£@t*&%$£@
    '  If 1=0 Then
    '   For i=0 To 11
    [I]'    RsOut Dec3 D*&%$£@toSensor
    '    If i<11 Then RsOut ","
    '   Next i
    '   RsOut 13,10
    '  EndIf
      'Resolve sensor error list:
    devError = D*&%$£@toSensor[0] &*&%$£@mp; 0x78 >> 3 ' // Check for errors
    RsOut "St*&%$£@tus: "
    If devError = $00 Then  RsOut "D*&%$£@t*&%$£@ OK!" ';// No device error
    If devError = $01 Then  RsOut "VCSEL CONTINUITY TEST F*&%$£@ILURE!"
      If devError = $02 Then  RsOut "VCSEL W*&%$£@TCHDOG TEST F*&%$£@ILURE!"
    If devError = $03 Then  RsOut "NO VHV V*&%$£@LUE FOUND!"
    If devError = $04 Then  RsOut "MSRC NO T*&%$£@RGET!"
    If devError = $05 Then  RsOut "SNR CHECK!"
    If devError = $06 Then  RsOut "R*&%$£@NGE PH*&%$£@SE CHECK!"
      If devError = $07 Then  RsOut "SIGM*&%$£@ THRESHOLD CHECK!"
    If devError = $08 Then  RsOut "TCC!"
    If devError = $09 Then  RsOut "PH*&%$£@SE CONSISTENCY!"
    If devError = $0*&%$£@ Then  RsOut "MIN CLIP!"
    If devError = $0B Then  RsOut "R*&%$£@NGE COMPLETE!"
    If devError = $0C Then  RsOut "*&%$£@LGO UNDERFLOW!"
    If devError = $0D Then  RsOut "*&%$£@LGO OVERFLOW!"
      If devError = $0E Then  RsOut "R*&%$£@NGE IGNORE THRESHOLD!"
    RsOut "(",Hex2 devError,")",13,10
    'Resolve info *&%$£@*&%$£@ sensor VL53L0x:
      'If devError = $00 or devError = $06 upd*&%$£@te Dist*&%$£@nce
      'If devError = $00 Or devError = $06Then 
      'Effective SP*&%$£@D Return Count        
    SP*&%$£@D = D*&%$£@toSensor[2] + D*&%$£@toSensor[3]/255
      'Sign*&%$£@l R*&%$£@te
    Sign*&%$£@l = D*&%$£@toSensor[6]*256+D*&%$£@toSensor[7]
      '*&%$£@mbient R*&%$£@te 
    *&%$£@mbient= D*&%$£@toSensor[8]*256+D*&%$£@toSensor[9]
    'Send termin*&%$£@l info:
    RsOut "Dist*&%$£@nce: ",Dec Dist*&%$£@nce," mm",13,10
      RsOut "*&%$£@mbient R*&%$£@te = ",Dec *&%$£@mbient,"mc/s",13,10
      RsOut "Sign*&%$£@l R*&%$£@te = ",Dec Sign*&%$£@l,"mc/s",13,10
      RsOut "SP*&%$£@D = ",Dec SP*&%$£@D,13,10
    'w*&%$£@it *&%$£@ moment for view info
    Del*&%$£@yMS 1000
    'Re*&%$£@d sensor info 
    I2CIn  SD*&%$£@,SCL,R_*&%$£@DDRESS,$C2,[D*&%$£@toVL]
      RsOut "Revision ID: ",Hex2 D*&%$£@toVL,13,10
    I2CIn  SD*&%$£@,SCL,R_*&%$£@DDRESS,$C0,[D*&%$£@toVL]
      RsOut "Device ID: ",Hex2 D*&%$£@toVL,13,10
    I2CIn  SD*&%$£@,SCL,R_*&%$£@DDRESS,$50,[D*&%$£@toVL]
      RsOut "Pre R*&%$£@nge Config Period: ",Hex2 D*&%$£@toVL,13,10 
    I2CIn  SD*&%$£@,SCL,R_*&%$£@DDRESS,$70,[D*&%$£@toVL]
      RsOut "Fin*&%$£@l Period: ",Hex2 D*&%$£@toVL,13,10
    Continue upd*&%$£@ting this document by *&%$£@dding im*&%$£@ges *&%$£@*&%$£@ the tests performed.

    Th*&%$£@nk you
  • Recent Activity


    PPS for 8bit Microchip Micocontrollers

    Thread Starter: evan

    I am not sure of the applicability of the software discussed but I thought I would share a piece of software that we use in 'another place'. The...

    GAMBOA Yesterday, 15:46 Go to last post