@stan
Thanks for the info!
I don't understand the error. It is only a byte so it should have auto dimensioned. This started to crop up after the readMulti was aligned with element 1 of the array instead of element 0. Before that I had subtracted 1 from the NumOfByteData variable and it recognized it as a variable. And it is actually Dimmed in the sub declaration.
BR
Mike
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I do not use subs with parameters.
Is it normal basic?
Does it make programs more readable?
Does it actually work?
I prefer all vars dimmed at start and use option explicit. I think they are called global variables.
I do not know why numofbytedata not defined showed for that line and other vars not dimmed passed.
Evan has used the passing vars to subs in his include files and it, imho, makes it difficult to follow. @mmotte your first code did give deverror as 0 and then printed distance.
Changed it to glcb display and ok.
Gone odd since then, deverrors and behaving odd.
Still thank you again for replying.
This is your code that gives deverror 0 and shows distance.
for mega328 and glcd ili9341
;vl53l0x converted from picaxe by mmotte;to ili9341 glcd
#chipmega328p, 16
#optionExplicit
#include<glcd.h>
#include<uno_mega328p.h>
#defineGLCD_TYPEGLCD_TYPE_ILI9341'Pin mappings for SPI - this GLCD driver supports Hardware SPI and Software SPI
#defineGLCD_DCDIGITAL_8' Data command line
#defineGLCD_CSDIGITAL_10' Chip select line
#defineGLCD_RESETDIGITAL_9' Reset line
#defineGLCD_DIDIGITAL_12' Data in | MISO - Not used therefore not really required
#defineGLCD_DODIGITAL_11' Data out | MOSI
#defineGLCD_SCKDIGITAL_13' Clock Line'Pin mappings for SPI - this GLCD driver supports Hardware SPI and Software SPI'#define GLCD_DC PortB.2 ; DIGITAL_8 'Datacommandline'#define GLCD_CS PortD.7 ; DIGITAL_10 'Chipselectline'#define GLCD_RESET PortD.4 ; DIGITAL_9 'Resetline'#define GLCD_DI PortB.4 ; DIGITAL_12 'Datain|MISO-Notusedthereforenotreallyrequired'#define GLCD_DO PortB.3 ; DIGITAL_11 'Dataout|MOSI'#define GLCD_SCK PortB.5 ; DIGITAL_13 'ClockLine;
#defineILI9341_HardwareSPI;;now rename colours to make it easier to set up sprite data
#definebkILI9341_BLACK
#definereILI9341_RED
#definegrILI9341_GREEN
#defineblILI9341_BLUE
#definewhILI9341_WHITE
#definepuILI9341_PURPLE
#defineyeILI9341_YELLOW
#definecyILI9341_CYAN
#definedgILI9341_D_GRAY
#definelgILI9341_L_GRAY
#definesiILI9341_SILVER
#definemaILI9341_MAROON
#defineolILI9341_OLIVE
#defineliILI9341_LIME
#defineaqILI9341_AQUA
#defineteILI9341_TEAL
#definenaILI9341_NAVY
#definefuILI9341_FUCHSIA'GLCD selected extension font set. ASCII characters 31-254, the extended font uses 1358 bytes of program memory
#defineGLCD_EXTENDEDFONTSET1GLCDfntDefaultsize=1'GLCDCLS supports GLCDBackground as default'GLCDCLS also support passing color parameter.'GLCDCLS [color]GLCDRotate(Landscape)GLCDfntDefaultsize=3GLCDCLSILI9341_BLUE
#defineW_ADDRESS0xEC
#defineR_ADDRESS0xED
#defineVL53Dev0x52
#defineHI2C_BAUD_RATE400
#defineHI2C_DATAPORTC.5
#defineHI2C_CLOCKPORTC.4HI2CModeMaster
#defineUSART_BAUD_RATE9600
#defineUSART_TX_BLOCKING
#defineUSART_DELAY1msdimVL52ByteValueasbytedimVL52WordValueasworddimDATABUFFER_POINTERasbyte'VL53L0x Registers Write and Read'No. Command Return value Description'1 0XA0 BYTE1,BYTE2 Word output mode, the output distance is (BYTE1 < 8) &BYTE2;' the conversion to 10 is the actual measurement distance, the unit is mm'2 0XA1 BYTE1,BYTE2 VL53L0X initialization (initialization time is 500mS)'3 0XB0 0XB0 The module baud rate is set to 9600, and it takes effect in time (default)'4 0XB1 0XB1 The module baud rate is set to 19200, and it takes effect in time'5 0XB2 0XB2 The module baud rate is set to 115200, and it takes effect in time'6 0XC0 0XC0 Set up as a long distance measurement mode (default)'7 0XC1 0XC1 Set up as a high-speed measurement model'8 0XC2 0XC2 Set to high precision measurement mode (measurement interval is greater than 180mS)'9 0XD0 0XD0 Set XSHUT to high level, module work normally and initialize module'10 0XD1 0XD1 Setting XSHUT to low level, VL53L0X closes'11 0XF0 BYTE1--BYTE4 Current baud rate (MSB)' BYTE5 0X00 long distance mode 0X01 high speed measurement mode' 0X02 high precision measurement mode' BYTE6 The status of the current XSHUTdimda(12)asbytedimdevError,datoVL,tmp,addr,ctr,NumOfByteDataasbytedimDistance,Signal,SPAD,Ambientasword' Soft reset'hi2cout $89,($01)wr_byte_VL52(VL53Dev,0x89, 0x01)wait200msdo'Sys range start';hi2cout $00,($01)wr_byte_VL52(VL53Dev,0x00, 0x01)'Read 12 raw data of Sensor:;hi2cin $14,(da1,da2,da3,da4,da5,da6,da7,da8,da9,da10,da11,da12)rd_multi_verVL53(VL53Dev, 0x14, 12, Da())'Resolve sensor errorsdevError=(Da(1)and0x78)/8;>> 3 ' // Check for errors'Resolve info of sensor VL53L0x:'If devError = $00 or devError = $06 update Distance'If devError = $00 Or devError = $06ThendevError=(Da(1)and0x78)/8;>> 3 ' // Check for errorsGLCDfntDefaultsize=3GLCDDrawString0,0,"Error "+str(devError)+"",wh;if devError=0 thenDistance=Da(10)*256+Da(11)GLCDDrawString(0,40,"Distance: ",wh)GLCDfntDefaultsize=5GLCDDrawString(0,120, str(Distance)+" mm ",wh)loopsubrd_byte_verVL53(inVL53Devasbyte, inVL53Addrasbyte, outVL53ByteValasbyte);read a single byte from an address
#ifdefHI2C_DATAHI2CStartHI2CSend(VL53Dev)HI2CSend(VL53Addr)HI2CReStartHI2CSend(VL53Devor1);set the read flagHI2CReceive(VL53ByteVal, NACK);read one byte and concludeHI2CStop
#endif
#ifdefI2C_DATAI2CStart;generate a start signalI2CSend(VL53Dev);inidcate a writeI2CSend(VL53Addr)I2CReStartI2CSend(VL53Devor1);set the read flagI2CReceive(VL53ByteVal, NACK);read one byte and concludeI2CStopI2CAckPoll(VL53Dev);wait for buffer write
#endifendsubsubrd_word_verVL53(inVL53Devasbyte, inVL53Addrasbyte, outVL53WordValasword);read a single byte from an address
#ifdefHI2C_DATAHI2CStartHI2CSend(VL53Dev)HI2CSend(VL53Addr)HI2CReStartHI2CSend(VL53Devor1);set the read flagHI2CReceive(VL53WordVal_h);read one byte and concludeHI2CReceive(VL53WordVal, NACK);read one byte and concludeHI2CStop
#endif
#ifdefI2C_DATAI2CStart;generate a start signalI2CSend(VL53Dev);inidcate a writeI2CSend(VL53Addr)I2CReStartI2CSend(VL53Devor1);set the read flagI2CReceive(VL53WordVal_h);read one byte and concludeI2CReceive(VL53WordVal, NACK);read one byte and concludeI2CStopI2CAckPoll(VL53Dev);wait for buffer write
#endifendsubsubrd_multi_verVL53(inVL53Devasbyte, inVL53Addrasbyte, inNumOfByteDataasbyte, outDataBuffer())'NumOfByteData--
#ifdefI2C_DATAI2CStartI2CSend(VL53Dev);Send addressI2CSend(VL53Addr)I2CReStartI2CSend(VL53Dev+1);indicate a read operationForDataBuffer_Pointer=0to(NumOfByteData-1)I2CReceiveDataBuffer(DataBuffer_Pointer), ACKnextI2CReceiveBME280_DataBuffer(NumOfByteData) , NACKI2CStop
#endif
#ifdefHI2C_DATAHI2CStartHI2CSend(VL53Dev)HI2CSend(VL53Addr)HI2CReStartHI2CSend(VL53Dev+1);indicate a read operationForDataBuffer_Pointer=0to(NumOfByteData-1)HI2CReceiveDataBuffer(DataBuffer_Pointer), ACKnextHI2CReceiveDataBuffer(NumOfByteData), NACKHI2CStop
#endifendsubsubwr_byte_VL52(inVL52Devasbyte, inVL52Addressasbyte, inVL52_SendByteasbyte)
#ifdefI2C_DATAI2CStartI2CSend(VL52Dev);Send addressI2CSend(VL52Address)I2CSend(VL52_SendByte);Send dataI2CStop
#endif
#ifdefHI2C_DATAHI2CStartHI2CSend(VL52Dev)HI2CSend(VL52Address)HI2CSend(VL52_SendByte)HI2CStop
#endifendsub
Last edit: stan cartwright 2020-05-21
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I have to dim numofbytedata or I get compile error?no reason yet.
mmotte's original code gave deverror as 0...that may be false
and gave a change for distance.Seemedto work a bit.
I do not see the reason for 2 arrays..da() and HI2CReceive DataBuffer( DataBuffer_Pointer), ACK
other than translation from picaxe.
It is because of the code and subs and vars I found it did not work using 1 array...but it could.
Last edit: stan cartwright 2020-05-21
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
changing
For DataBuffer_Pointer = 0 to (NumOfByteData-1) to For DataBuffer_Pointer = 1 to (NumOfByteData)
HI2CReceive DataBuffer( DataBuffer_Pointer), ACK
next
HI2CReceive DataBuffer( NumOfByteData ), NACK
HI2CStop
gives numofbytedata invalid,not defined
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
It seems rd_multi_verVL53 ( VL53Dev, 0x14, 12, DataBuffer() ) has problems..
If it was broken down to be simpler..understandable..what would it be?
...please. is there a problem ..or am I not bright?
is it I am using mega328p,16?
the first code mmotte posted worked.
could in NumOfByteData as byte be a constant. It is defined where???
Thanks again...I did not start this thread but shows others are interested in this device.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
dimptr,buffer(13)asbyteHI2CStart;software restartHI2CSend(0x52)HI2CSend(0x89)HI2CSend(0x01)HI2CStopwait200msdoHI2CStart;Sys Range StartHI2CSend(0x52)HI2CSend(0x00)HI2CSend(0x01)HI2CStop;read arrayHI2CStartHI2CSend(0x52)HI2CSend(0x14)HI2CReStartHI2CSend(0x53);indicate a read operationForptr=1to13HI2CReceivebuffer(ptr), ACKnextHI2CReceivebuffer(13), NACKHI2CStopGLCDfntDefaultsize=3GLCDDrawString0,0,"Error "+str((buffer(2)and0x78)/8)+"",whGLCDDrawString(0,40,"Distance: ",wh)GLCDfntDefaultsize=5GLCDDrawString(0,120, str(buffer(11)*256+buffer(12))+" mm ",wh)loop
#chipmega328p, 16
#optionExplicit;for terminal distance out
#defineUSART_BAUD_RATE9600
#defineUSART_TX_BLOCKING
#defineUSART_DELAY1ms;set up v53l0x
#defineHI2C_DATAPORTC.5;sda
#defineHI2C_CLOCKPORTC.4;scl
#defineHI2C_BAUD_RATE400HI2CModeMaster;main Get Distance-------dimptr,buffer(13)asbyteHI2CStart;software restartHI2CSend(0x52)HI2CSend(0x89)HI2CSend(0x01)HI2CStopwait200msdo;------------------------HI2CStart;Sys Range StartHI2CSend(0x52)HI2CSend(0x00)HI2CSend(0x01)HI2CStopHI2CStartHI2CSend(0x52)HI2CSend(0x14)HI2CReStartHI2CSend(0x53);indicate a read operationForptr=1to12HI2CReceivebuffer(ptr), ACKnextHI2CReceivebuffer(13), NACKHI2CStopwait300mshserprint" Distance: "+str(buffer(11)*256+buffer(12))+" mm ";loop;----------------------
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
hi2cin 0x1E,(b3,b2) ;read range in mm seems to be this in gcb
HI2CStart ;read distance
HI2CSend(0x52)
HI2CSend(0x1e)
HI2CReStart
HI2CSend(0x52 or 1) ;set the read flag
HI2CReceive(distance_hi) ;read one byte and conclude
HI2CReceive(distance_lo, NACK) ;read one byte and conclude
HI2CStop
It must be possible to make hi2creceive like picaxe hi2cin
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
This uses my cobbled together hi2cin 0x1E,(b3,b2) to read distance and it works fine but please check it for me as it was more imagination than knowing what I was doing.
#chipmega328p, 16
#optionExplicit;for terminal distance out
#defineUSART_BAUD_RATE9600
#defineUSART_TX_BLOCKING
#defineUSART_DELAY1ms;set up v53l0x
#defineHI2C_DATAPORTC.5;sda
#defineHI2C_CLOCKPORTC.4;scl
#defineHI2C_BAUD_RATE400HI2CModeMasterdimdistance_lo,distance_hiasbyteHI2CStart;software restartHI2CSend(0x52)HI2CSend(0x89)HI2CSend(0x01)HI2CStopwait200msdo;------------------------HI2CStart;Sys Range StartHI2CSend(0x52)HI2CSend(0x00)HI2CSend(0x01)HI2CStopHI2CStart;read distanceHI2CSend(0x52)HI2CSend(0x1e)HI2CReStartHI2CSend(0x53);set the read flagHI2CReceive(distance_hi, ACK)HI2CReceive(distance_lo, NACK)HI2CStophserprint" Distance: "+str(distance_hi*256+distance_lo)+" mm ";loop
Last edit: stan cartwright 2020-05-23
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
@Stan
You have "minimalized " this to the nth. Y ou might be interested in one more step. One feature of GCB is the bytes of word and long are named HUE. So when distance is read as two bytes they can be automatically combined into a "word" variable "distance". Probably doesn't save you another line of code but you don't need to do the multiply and add.
DimDistanceaswordDimDistance_HiasBytealiasDistance_HDimDistance_LoasBytealiasDistance
...
'hserprint " Distance: "+str(distance_hi*256+distance_lo)+" mm " is replaced byHSerPrintdistance'later these were added so it isn'tallstrungtogetherHSerPrintCRLFwait1s
ironically , in my original code , I had a subroutine
That for Hardware I2C looks exactly like your broken out code BUT it don't read the two bytes from reg 0x1E and 0x1F as the word. it only reads the register 0x1F. returns 0 from 0x1E. Yours works and mine don't!
Worse than that. I can not read the 0x14 register which is the status, by itself. If I Read Multiple reggisters like we were doing before it reads all the 12 regs from 0x14 to 0x1F and they read correctly. Reading just the one ox14 register would allow us to pick out the valid "range" reads . There is some strange going on.
I had wanted to make a more general library that could be hooked up as both HI2C andI2C. Also the 53L0X has ability to send an interrupt on the GPIO1 pin to read the valid distance. Besides that it has settings for better distance or better accuracy. Lots to learn, what are SPADS and TCC. Why is the measurement not accurate? Mine is off by ~75mm.
Just saying, i wouldn't consider this done.
BR
Mike
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Nice Mike and thanks again...for the nth time.
If you want to read the error code then
HI2CStart;software restartHI2CSend(0x52)HI2CSend(0x89)HI2CSend(0x01)HI2CStopwait200ms;you need all this before reading anything-once at start of program. Don't need to run it again.HI2CStart;this reads read error codeHI2CSend(0x52)HI2CSend(0x15);register to read-this is error code change to 0x14 if you wantHI2CReStartHI2CSend(0x53);set the read flagHI2CReceiveerrcode, NACK;read one byte and concludeHI2CStop
You read 12 bytes from 0x14 to array and 2nd element was error code which is usually 0.
I gave credit to Buzby on picaxe forum but looking lately and Pongo needs some credit.
It seems it was the proton basic program I posted that was altered to picaxe and that was half changed to gcb.
The latest method is just read 0x14 hi byte and 0x15 hi byte for range.
I did try dim distance as word then store 0x14 byte to distance_H and 0x15 byte to distance
but distance was only the lo byte ie 0x15 so distance only went to 255 then back to 0.
I used
You don't dim distance as word or gives compile error.
I don't consider this finished but we have ways to read and write registers now.
You can change the i2c address and use multiple devices.
It reads quite fast. It was the glcd slowing display rate.
It probably does several samples/second. Add 5 then divide by 5.
You used
sub rd_word_verVL53( in VL53Dev as byte, in VL53Addr as byte, out VL53WordVal as word )
Which seemed like Anobiums answer to David Briscoe.
I lost track of the variables being passed so used the constants for the device as the code is no use for anything else it makes sense and is easier IMHO to follow what's going on.
I have given you credit and didn't want the code to get lost over time as it might be as useful to someone else as it was for me. These devices have been in my box of bits too long. All I got was finding the i2c address from a gcb program in demos.
The actual board has what looks like a regulator and DOES work off 5V...not 2.2-3.3V.
As you said, no i2c pull ups needed...I was using 2.2K and 3.3V supply but they must be on the board.
Only moan is short range which might not be a problem for a robot. It drops to 20 mm on over range as you said.
cheers stan
you could make a sub to return any reg value
reg=0x14:getreg
if regval=xx then....
sub getreg
HI2CStart
HI2CSend(0x52)
HI2CSend(reg) ;register to read
HI2CReStart
HI2CSend(0x53) ;set the read flag
HI2CReceive regval, NACK ;read one byte and conclude
HI2CStop
end sub
Way to read all 12 regs to array that doesn't give compile errors
~~~
dim ptr,buffer(13) as byte
' HI2CStart
' HI2CSend(0x52)
' HI2CSend(0x14)
' HI2CReStart
' HI2CSend(0x53) ;indicate a read operation
' For ptr =1 to 12
' HI2CReceive buffer(ptr), ACK
' next
' HI2CReceive buffer(13), NACK
' HI2CStop
~~~
error code is buffer(2)
distance is str(buffer(11)*256+buffer(12))+" mm "
I think the problem you had was using 2 arrays and getting the elements not corresponding to registers. The error with numofbytedata not defined...and couldn't be! got to me....that's why I simplified it...but I never got an answer to the compile error which referred to a line the variable was not on...all very confusing...that's is why I don't pass vars and keep it simple ie global vars dimmed at start...old style,need for speed,if you use a sub only once-put it in line.
which is faster distance_hi256 +distance_lo..... or ... distance_hi256 or distance_lo...or even distance=fnLSL (distance _hi , 8 ) or distance_lo :)
Last edit: stan cartwright 2020-05-25
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Looking back to the first post...all the info is there
subrd_byte_verVL53(inVL53Devasbyte, inVL53Addrasbyte, outVL53ByteValasbyte);read a single byte from an address
#ifdefHI2C_DATAdoHI2CReStart;generate a start signalHI2CSend(VL53Dev);inidcate a writeloopWhileHI2CAckPollStateHI2CSend(VL53Addr)HI2CReStartHI2CSend(VL53Devor1);set the read flagHI2CReceive(VL53ByteVal, NACK);read one byte and concludeHI2CStop
#endif
#ifdefI2C_DATAI2CStart;generate a start signalI2CSend(VL53Dev);inidcate a writeI2CSend(VL53Addr)I2CReStartI2CSend(VL53Devor1);set the read flagI2CReceive(VL53ByteVal, NACK);read one byte and concludeI2CStopI2CAckPoll(VL53Dev);wait for buffer write
#endifendsubsubrd_word_verVL53(inVL53Devasbyte, inVL53Addrasbyte, outVL53WordValasword);read a single byte from an address
#ifdefHI2C_DATAdoHI2CReStart;generate a start signalHI2CSend(VL53Dev);inidcate a writeloopWhileHI2CAckPollStateHI2CSend(VL53Addr)HI2CReStartHI2CSend(VL53Devor1);set the read flagHI2CReceive(VL53WordVal_h);read one byte and concludeHI2CReceive(VL53WordVal, NACK);read one byte and concludeHI2CStop
#endif
#ifdefI2C_DATAI2CStart;generate a start signalI2CSend(VL53Dev);inidcate a writeI2CSend(VL53Addr)I2CReStartI2CSend(VL53Devor1);set the read flagI2CReceive(VL53WordVal_h);read one byte and concludeI2CReceive(VL53WordVal, NACK);read one byte and concludeI2CStopI2CAckPoll(VL53Dev);wait for buffer write
#endifendsub
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
why is line 255 the error not line 252?
Last edit: stan cartwright 2020-05-20
why is line 255 the error not line 252?
@Stan
Would you please hook up the terminal and dump the registers on your 53L0X so I can compare to mine and to the initialization values.
Here is the program to dump with and my dump of the registers.
Thanks
Mike
I get var not defined. When I define it I get the dump shown. It says deverror 11.
Last edit: stan cartwright 2020-05-21
@stan
Thanks for the info!
I don't understand the error. It is only a byte so it should have auto dimensioned. This started to crop up after the readMulti was aligned with element 1 of the array instead of element 0. Before that I had subtracted 1 from the NumOfByteData variable and it recognized it as a variable. And it is actually Dimmed in the sub declaration.
BR
Mike
I do not use subs with parameters.
Is it normal basic?
Does it make programs more readable?
Does it actually work?
I prefer all vars dimmed at start and use option explicit. I think they are called global variables.
I do not know why numofbytedata not defined showed for that line and other vars not dimmed passed.
Evan has used the passing vars to subs in his include files and it, imho, makes it difficult to follow.
@mmotte your first code did give deverror as 0 and then printed distance.
Changed it to glcb display and ok.
Gone odd since then, deverrors and behaving odd.
Still thank you again for replying.
This is your code that gives deverror 0 and shows distance.
for mega328 and glcd ili9341
Last edit: stan cartwright 2020-05-21
You should try. Makes the code resusable and reduces the RAM.
Yes
Yes. As the variables are stated in the sub/function not the user define variable.
Yes. Extensively.
This is good also.
To help Mike. Remove all the GCLD and use something more common. Like HSerPrint.
I have to say, I use Subs with parameters and Functions as much as I can.
In my eye it makes code much easier to understand.
I have to dim numofbytedata or I get compile error?no reason yet.
mmotte's original code gave deverror as 0...that may be false
and gave a change for distance.Seemedto work a bit.
I do not see the reason for 2 arrays..da() and HI2CReceive DataBuffer( DataBuffer_Pointer), ACK
other than translation from picaxe.
It is because of the code and subs and vars I found it did not work using 1 array...but it could.
Last edit: stan cartwright 2020-05-21
In mmotte;s original
delete dim da(14) as byte
add
dim DataBuffer(12) as byte
change
rd_multi_verVL53 ( VL53Dev, 0x14, 12, DataBuffer() )
devError = (DataBuffer(1) and 0x78)/8 ;>> 3 ' // Check for errors
Distance=DataBuffer(10)*256+DataBuffer(11)
now just 1 array...seems to work as before with glcd.
changing
For DataBuffer_Pointer = 0 to (NumOfByteData-1) to For DataBuffer_Pointer = 1 to (NumOfByteData)
HI2CReceive DataBuffer( DataBuffer_Pointer), ACK
next
HI2CReceive DataBuffer( NumOfByteData ), NACK
HI2CStop
gives numofbytedata invalid,not defined
It seems rd_multi_verVL53 ( VL53Dev, 0x14, 12, DataBuffer() ) has problems..
If it was broken down to be simpler..understandable..what would it be?
...please. is there a problem ..or am I not bright?
is it I am using mega328p,16?
the first code mmotte posted worked.
could in NumOfByteData as byte be a constant. It is defined where???
Thanks again...I did not start this thread but shows others are interested in this device.
I got the core code down to
It works! only ptr and buffer(13) variables.
Much simpler for just distance.
https://www.youtube.com/watch?v=bqeT5oR1ERI&feature=youtu.be
I still can't convert
Then the distance in mm will be ready in registers 0x1E, 0x1F :
hi2cin 0x1E,(b3,b2) ;read range in mm
To convert from the two input bytes to word value w3:
w3 = b3
w3 = w3 * 256
w3 = w3 | b2
Last edit: stan cartwright 2020-05-22
This is shortened for terminal output
hi2cin 0x1E,(b3,b2) ;read range in mm seems to be this in gcb
HI2CStart ;read distance
HI2CSend(0x52)
HI2CSend(0x1e)
HI2CReStart
HI2CSend(0x52 or 1) ;set the read flag
HI2CReceive(distance_hi) ;read one byte and conclude
HI2CReceive(distance_lo, NACK) ;read one byte and conclude
HI2CStop
It must be possible to make hi2creceive like picaxe hi2cin
This uses my cobbled together hi2cin 0x1E,(b3,b2) to read distance and it works fine but please check it for me as it was more imagination than knowing what I was doing.
Last edit: stan cartwright 2020-05-23
I'm starting to like this device.Could be nice on a small robot vehicle.
Does not like flesh,maybe it's infrared laser but even black denim it sees.
@Stan
You have "minimalized " this to the nth. Y ou might be interested in one more step. One feature of GCB is the bytes of word and long are named HUE. So when distance is read as two bytes they can be automatically combined into a "word" variable "distance". Probably doesn't save you another line of code but you don't need to do the multiply and add.
ironically , in my original code , I had a subroutine
That for Hardware I2C looks exactly like your broken out code BUT it don't read the two bytes from reg 0x1E and 0x1F as the word. it only reads the register 0x1F. returns 0 from 0x1E. Yours works and mine don't!
Worse than that. I can not read the 0x14 register which is the status, by itself. If I Read Multiple reggisters like we were doing before it reads all the 12 regs from 0x14 to 0x1F and they read correctly. Reading just the one ox14 register would allow us to pick out the valid "range" reads . There is some strange going on.
I had wanted to make a more general library that could be hooked up as both HI2C andI2C. Also the 53L0X has ability to send an interrupt on the GPIO1 pin to read the valid distance. Besides that it has settings for better distance or better accuracy. Lots to learn, what are SPADS and TCC. Why is the measurement not accurate? Mine is off by ~75mm.
Just saying, i wouldn't consider this done.
BR
Mike
Nice Mike and thanks again...for the nth time.
If you want to read the error code then
You read 12 bytes from 0x14 to array and 2nd element was error code which is usually 0.
I gave credit to Buzby on picaxe forum but looking lately and Pongo needs some credit.
It seems it was the proton basic program I posted that was altered to picaxe and that was half changed to gcb.
The latest method is just read 0x14 hi byte and 0x15 hi byte for range.
I did try dim distance as word then store 0x14 byte to distance_H and 0x15 byte to distance
but distance was only the lo byte ie 0x15 so distance only went to 255 then back to 0.
I used
You don't dim distance as word or gives compile error.
I don't consider this finished but we have ways to read and write registers now.
You can change the i2c address and use multiple devices.
It reads quite fast. It was the glcd slowing display rate.
It probably does several samples/second. Add 5 then divide by 5.
You used
sub rd_word_verVL53( in VL53Dev as byte, in VL53Addr as byte, out VL53WordVal as word )
Which seemed like Anobiums answer to David Briscoe.
I lost track of the variables being passed so used the constants for the device as the code is no use for anything else it makes sense and is easier IMHO to follow what's going on.
I have given you credit and didn't want the code to get lost over time as it might be as useful to someone else as it was for me. These devices have been in my box of bits too long. All I got was finding the i2c address from a gcb program in demos.
The actual board has what looks like a regulator and DOES work off 5V...not 2.2-3.3V.
As you said, no i2c pull ups needed...I was using 2.2K and 3.3V supply but they must be on the board.
Only moan is short range which might not be a problem for a robot. It drops to 20 mm on over range as you said.
cheers stan
you could make a sub to return any reg value
reg=0x14:getreg
if regval=xx then....
sub getreg
HI2CStart
HI2CSend(0x52)
HI2CSend(reg) ;register to read
HI2CReStart
HI2CSend(0x53) ;set the read flag
HI2CReceive regval, NACK ;read one byte and conclude
HI2CStop
end sub
Way to read all 12 regs to array that doesn't give compile errors
~~~
dim ptr,buffer(13) as byte
' HI2CStart
' HI2CSend(0x52)
' HI2CSend(0x14)
' HI2CReStart
' HI2CSend(0x53) ;indicate a read operation
' For ptr =1 to 12
' HI2CReceive buffer(ptr), ACK
' next
' HI2CReceive buffer(13), NACK
' HI2CStop
~~~
error code is buffer(2)
distance is str(buffer(11)*256+buffer(12))+" mm "
I think the problem you had was using 2 arrays and getting the elements not corresponding to registers. The error with numofbytedata not defined...and couldn't be! got to me....that's why I simplified it...but I never got an answer to the compile error which referred to a line the variable was not on...all very confusing...that's is why I don't pass vars and keep it simple ie global vars dimmed at start...old style,need for speed,if you use a sub only once-put it in line.
which is faster distance_hi256 +distance_lo..... or ... distance_hi256 or distance_lo...or even distance=fnLSL (distance _hi , 8 ) or distance_lo :)
Last edit: stan cartwright 2020-05-25
Looking back to the first post...all the info is there
why the v53l0x is a pain
this https://github.com/GrimbiXcode/VL53L0X-Register-Map
and this may help you, got from arduino website
register addresses from API vl53l0x_device.h (ordered as listed there)
enum
regAddr
{
SYSRANGE_START =
0x00
,
SYSTEM_THRESH_HIGH =
0x0C
,
SYSTEM_THRESH_LOW =
0x0E
,
SYSTEM_SEQUENCE_CONFIG =
0x01
,
SYSTEM_RANGE_CONFIG =
0x09
,
SYSTEM_INTERMEASUREMENT_PERIOD =
0x04
,
SYSTEM_INTERRUPT_CONFIG_GPIO =
0x0A
,
GPIO_HV_MUX_ACTIVE_HIGH =
0x84
,
SYSTEM_INTERRUPT_CLEAR =
0x0B
,
RESULT_INTERRUPT_STATUS =
0x13
,
RESULT_RANGE_STATUS =
0x14
,
RESULT_CORE_AMBIENT_WINDOW_EVENTS_RTN =
0xBC
,
RESULT_CORE_RANGING_TOTAL_EVENTS_RTN =
0xC0
,
RESULT_CORE_AMBIENT_WINDOW_EVENTS_REF =
0xD0
,
RESULT_CORE_RANGING_TOTAL_EVENTS_REF =
0xD4
,
RESULT_PEAK_SIGNAL_RATE_REF =
0xB6
,
ALGO_PART_TO_PART_RANGE_OFFSET_MM =
0x28
,
I2C_SLAVE_DEVICE_ADDRESS =
0x8A
,
MSRC_CONFIG_CONTROL =
0x60
,
PRE_RANGE_CONFIG_MIN_SNR =
0x27
,
PRE_RANGE_CONFIG_VALID_PHASE_LOW =
0x56
,
PRE_RANGE_CONFIG_VALID_PHASE_HIGH =
0x57
,
PRE_RANGE_MIN_COUNT_RATE_RTN_LIMIT =
0x64
,
FINAL_RANGE_CONFIG_MIN_SNR =
0x67
,
FINAL_RANGE_CONFIG_VALID_PHASE_LOW =
0x47
,
FINAL_RANGE_CONFIG_VALID_PHASE_HIGH =
0x48
,
FINAL_RANGE_CONFIG_MIN_COUNT_RATE_RTN_LIMIT =
0x44
,
PRE_RANGE_CONFIG_SIGMA_THRESH_HI =
0x61
,
PRE_RANGE_CONFIG_SIGMA_THRESH_LO =
0x62
,
PRE_RANGE_CONFIG_VCSEL_PERIOD =
0x50
,
PRE_RANGE_CONFIG_TIMEOUT_MACROP_HI =
0x51
,
PRE_RANGE_CONFIG_TIMEOUT_MACROP_LO =
0x52
,
SYSTEM_HISTOGRAM_BIN =
0x81
,
HISTOGRAM_CONFIG_INITIAL_PHASE_SELECT =
0x33
,
HISTOGRAM_CONFIG_READOUT_CTRL =
0x55
,
FINAL_RANGE_CONFIG_VCSEL_PERIOD =
0x70
,
FINAL_RANGE_CONFIG_TIMEOUT_MACROP_HI =
0x71
,
FINAL_RANGE_CONFIG_TIMEOUT_MACROP_LO =
0x72
,
CROSSTALK_COMPENSATION_PEAK_RATE_MCPS =
0x20
,
MSRC_CONFIG_TIMEOUT_MACROP =
0x46
,
SOFT_RESET_GO2_SOFT_RESET_N =
0xBF
,
IDENTIFICATION_MODEL_ID =
0xC0
,
IDENTIFICATION_REVISION_ID =
0xC2
,
OSC_CALIBRATE_VAL =
0xF8
,
GLOBAL_CONFIG_VCSEL_WIDTH =
0x32
,
GLOBAL_CONFIG_SPAD_ENABLES_REF_0 =
0xB0
,
GLOBAL_CONFIG_SPAD_ENABLES_REF_1 =
0xB1
,
GLOBAL_CONFIG_SPAD_ENABLES_REF_2 =
0xB2
,
GLOBAL_CONFIG_SPAD_ENABLES_REF_3 =
0xB3
,
GLOBAL_CONFIG_SPAD_ENABLES_REF_4 =
0xB4
,
GLOBAL_CONFIG_SPAD_ENABLES_REF_5 =
0xB5
,
GLOBAL_CONFIG_REF_EN_START_SELECT =
0xB6
,
DYNAMIC_SPAD_NUM_REQUESTED_REF_SPAD =
0x4E
,
DYNAMIC_SPAD_REF_EN_START_OFFSET =
0x4F
,
POWER_MANAGEMENT_GO1_POWER_FORCE =
0x80
,
VHV_CONFIG_PAD_SCL_SDA__EXTSUP_HV =
0x89
,
ALGO_PHASECAL_LIM =
0x30
,
ALGO_PHASECAL_CONFIG_TIMEOUT =
0x30
Last edit: stan cartwright 2020-05-26