News:

Let's find out together what makes a PIC Tick!

Main Menu

PLEASE HEEELP Being Driven Mad ?

Started by Craig, Sep 21, 2021, 08:40 PM

Previous topic - Next topic

Craig

Hello
Pc 18F47K40 @ 64Mhz Positron8 4.0.0.9
I am trying to Index into an Array to Extract its Elements but, am out of ideas. I have only one I2C Sensor connected and am only running it for testing purposes, It is working correctly as it gives out all the correct readings on an LCD Display and using HRSOut to a PC and Is referred to as "VL:".
I have run various Array Filling Sequence but I cannot Load all the elements of a Word Array. I have run the Assembler F2 after Compilation and looks correct {See Below in the Saved PDF File}.

I am out of ideas! The program below works perfectly and prints the correct VL: Distance in mm but,
when I try and pull the elements out of the Array >>  Distance = BufferArray[11] It gives me the Wrong Info.
You can see on the printout of the Attached file from the Terminal Program that it is giving the Correct VL:927 value this is the Distance in mm
and then the 249917841581454 is supposed to be the Extracted Elements of the Array they Should all be 927 927 927 927 etc. NOT this strange number.
As Can Clearly be seen from the Attached file that number 249917841581454 NEVER Changes Ever, Even though the VL: Number keeps changing as the Laser Detects different objects at different distances. This number should Match the VL: Number and change constantly with the VL: Number.

The Issue seems to be when the Data Is pushed into an Array Like; BufferArray[11] from a LIVE SENSOR Like an (I2C VL53L0X Sensor)
As an Example and then trying to Extract the Individual Elements out of the Array!!

______________________________________________________________________________________________
I Can SETUP A TEST PROGRAM LIKE THIS AND EVERYTHING WORKS AS INTENDED!!!!

' Create an array variable

    Dim WordArray[10] As Word = 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000
 
    HRSOutLn Dec WordArray#0            ' Transmit the decimal value of an element
    HRSOutLn Dec WordArray#1            ' Transmit the decimal value of an element

  And It Will Print out 1000, 2000, Etc.....
'______________________________________________________________________________________________


The Bottom Line is how do I fix this Massive problem??


See The Attached File: Laser Test.PDF

'---------------------------------------------------------------------------
The TEST Program Below is as Per Les's Thread to Extract Elements out of an Array:
https://protoncompiler.com/index.php/topic,486.msg3241.html#msg3241
'===========================================================================

Laser Test.pdf
Laser Test1.pdf

Declare HRSOut_Pin = PORTC.6        ' TX
Declare HRSIn_Pin = PORTC.7        ' RX
Declare Hserial_Baud = 19200        ' Set baud rate to 19200_RS232
Declare Hserial_RCSTA = %10010000  ' "90 HexDecimal" Enable serial port and continuous receive RS232
Declare Hserial_TXSTA = %00100100

Declare HSCL_Pin = PORTC.3        VL53L0x TOF Sensor
Declare HSDA_Pin = PORTC.4        VL53L0x TOF Sensor

Include "PPS_18FxxK40.inc"                           
Include "Software_I2C.inc"

' Setup the VL53L0x - TOF Lazer Unit
' VL53L0x Registers Write and Read
'-----------------------------------
Symbol W_ADDRESS = $52
Symbol R_ADDRESS = $53
'-----------------------------------
Dim DatoVL As Byte
Dim DatoSensor[12] As Byte
Dim Distance As Word 
Dim DistFlag As Byte           
Dim devError As Byte
Dim SPAD As Float
Dim Signal As Word
Dim Ambient As Word
Dim DisRest As Word            ' VL53L0X "DISTANCE" (mm)to(cm)
Dim BufferArray[12] As Word

iPPSInput(cIn_Fn_MSSP2DAT, cIn_Pin_RP25)
iPPSInput(cIn_Fn_MSSP2CK, cIn_Pin_RP26)
iPPSOutput(cOut_Pin_RP22, cOut_Fn_TX1CK1)         
iPPSInput(cIn_Fn_RX1DT1, cIn_Pin_RP23) 

'=========================================           
Proc SendBuffer(ByRef pArrayAddr As Word, pStart As Word, pAmount As Word)
Dim Byteout As Word
pArrayAddr = pArrayAddr + pStart        ' Find the starting index address of the bytes to transmit
Repeat                                  ' Create a loop
Byteout = Ptr8(pArrayAddr++)        'Ptr8 Read a word from the buffer array
HRSOut Dec Byteout                  ' Transmit the word read from the array
Dec pAmount                        ' \
Until pAmount = 0                      ' / Loop until the amount of bytes are transmitted
EndProc
'=========================================

VL53Lx:

'System Range Start
HBusOut W_ADDRESS,[$00,$01]
DelayMS 5                                     

'Range status
HBusOut W_ADDRESS,[$14]
DelayMS 5                                     

' Read The 12 Bit Raw Data Of The VL53L0x Sensor:
HBusIn R_ADDRESS,[Str DatoSensor]

'-----------------------------------------------------------------------------------------------
' info of sensor VL53L0x:
'===============================================================================================
BufferArray[11] = DatoSensor[10]*256 + DatoSensor[11]
Distance = BufferArray[11]

DistFlag = 115          ' Flag Set to Show we have Loaded a Measurement into Distance Variable

'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]

SendBuffer(BufferArray, 0, 7)          ' Transmit 8 bytes from position 0 of BufferArray
Print At 1,6,"D: ",Dec Distance,"  "  ' Print The Distance in mm on LCD Display       
HRSOut "  VL: ", Dec3 Distance,"  "  ' PC TESTING show on PC {Laser Test.PDF}

Return

Stop
'--------------------------------------------------------------------------------------------H&S MAIN_18F47K40_VL53L0X ARRAY Test .txt

Yasin

Quote from: Craig on Sep 21, 2021, 08:40 PM.......
.......
.......
Dim BufferArray[12] As Word

'-----------------------------------------------------------------------------------------------
' info of sensor VL53L0x:
'===============================================================================================
BufferArray[99] = DatoSensor[10]*256 + DatoSensor[11]
Distance = BufferArray[99]


I could not fully understand the problem because my English is poor. I'm sorry for this. But there is a critical error in the code sample. I marked it in red. Non-element index number is selected.

trastikata

#2
Edit:
This line is not clear ... how do you fill the byte array with 12bit data, and how do you address individual elements in the array?

' Read The 12 Bit Raw Data Of The VL53L0x Sensor:
HBusIn R_ADDRESS,[Str DatoSensor]

Craig

Thank you Yasin
It was very late Last night when I posted the code the 99 was supposed to be 11 it is just a Typo.
The code is setup as BufferArray[11], I re-edited the post correcting it as Not to confuse anyone.
Regards
Craig

Craig

#4
Hello Trastikata

The Sensor fills DatoSensor using this command >>> HBusIn R_ADDRESS,[Str DatoSensor]
Then the distance is pushed into; BufferArray[11] = DatoSensor[10]*256 + DatoSensor[11]

I have used this to use Les's code to then extract the elements in the BufferArray[11].

I have always used; Distance = DatoSensor[10]*256 + DatoSensor[11] which also works
and then loaded Distance back into an Array and then extracted The various element out of it.

The Sensor is working correctly and gives out all the right readings but, My Problem is trying
to load all the elements of the Array. It will Always fill the first position of the Array with the correct reading but,
after that it either loads 0 or some other funny readings into the rest of the Elements of the array.

There seems to be an issue in how the Array Elements are being Indexed into Ie. Position 0 then Position 1 then Position 2 and so on
until All the Elements of the Array are filled up with Measurements.

regards
Craig

Gabi

Hi Craig
try replacing the Ptr8 to Ptr16, see if any change.

Repeat                                  ' Create a loop
        Byteout = Ptr16(pArrayAddr++)        '<<<< Ptr8 Read a word from the buffer array
        HRSOut Dec Byteout                  ' Transmit the word read from the array
        HRSOut " "
GL & 73
YO4WM

Craig

Hello Gabi

I have tried it as a ptr16 but, it just prints a longer number but still does the same.
The Issue is that all the Array Elements Should be filled up INDIVIDUALLY with all the data from the sensor.
Even If I try something simple like, I still only get it Filling the First Element Correctly:

HBusIn R_ADDRESS,[Str DatoSensor]                ' Used to Fill DatoSensor from VL53L0x

Distance = DatoSensor[10]*256 + DatoSensor[11]   ' Push Elements 10 and 11 into Word Variable Distance

DISTArray1 = Distance                            ' Push Data Held in Distance Variable into Each INDIVIDUAL Element of DISTArray1


 Repeat
  DISTArray1[ABCnt] = Distance                               
  HRSOut "DISTArray1: ", Dec3 DISTArray1[ABCnt],"  "         ' Send Each INDIVIDUAL Element of the Array to PC to view                         
  Inc ABCnt
 Until ABCnt = 5   
 If ABCnt = 5 Then ABCnt = 0
'---------------------------------------------------------------



Regards
Craig

Craig

Goodness Does No One have a I2C type sensor to see if they can Load it's Data Directly Into a Word Array while Indexing
it into each Element of the Word Array and Then Read Out Each Element of the Array Individually, while checking to see
if the Data is what is expected?

I have two types of TOF (Laser) I2C Sensors Which are both working & Reading Correctly, one is using Hardware Bus and the other Software but,
Both sensors have the same issue when it comes to Loading the Individual Elements into a Word Array.

The following Should work but, Only loads the First Element Correctly;
 
DISTArray1 = Distance   ' Push Data Held in Distance Word Variable into Each INDIVIDUAL Element of a 6 Element Array Called DISTArray1

 Repeat
  DISTArray1[ABCnt] = Distance                               
  HRSOut "DISTArray1: ", Dec3 DISTArray1[ABCnt],"  "         ' Send Each INDIVIDUAL Element of the Array to PC to view                         
  Inc ABCnt
 Until ABCnt = 5   
 If ABCnt = 5 Then ABCnt = 0

Regards
Craig


See_Mos

#8
I have just ordered one to experiment with.

Are you using any of the connections other than the I2C bus and power?

Can you post the full code please?

Craig

#9
Hi Stephen

I have nothing else on those I2C pins  besides the VL53L0x TOF Sensor but, Yes It is a whole PCB with other devices.
What I did is Switch Off  everything else and am Running a MAX 3232 Chip to Pickup the RS232 out to PC.
and have an LCD 2x16 Hitachi type connected for testing.

The Puzzeling Thing is that it works Perfectly and is very accurate and the Distance can be seen on the LCD and the
PC Via RS232 but, There seems to be an issue where it Does Not Index Correctly into all the Elements of the Array.

What I will do Later today is Connect one to a small PCB with a PIC all by itself and see If it indexes correctly or Not.
Will Post later when done.

Thanks for the Help Stephen Much appreciated, at this stage I want to pull my hair out!!
Regards
Craig


trastikata

#10
Quote from: Craig on Sep 22, 2021, 06:33 AMThe Sensor fills DatoSensor using this command >>> HBusIn R_ADDRESS,[Str DatoSensor]

Hello,

'Range status
HBusOut W_ADDRESS,[$14]
DelayMS 5                                     

' Read The 12 Bit Raw Data Of The VL53L0x Sensor:
HBusIn R_ADDRESS,[Str DatoSensor]

my point was that the sensor outputs only 1 byte data from address 0x14, which is the status byte and not the results address.

I skimmed through the Arduino library (couldn't find actual registers map - ST actually published an API instead of) and it seems that the initialization of and reading from the device is quite complex, a lot of registers have to be written which I see you are omitting? 

Regards

Stephen Moss

I wrote the small test program below and it simulates fine so I don't think there is fundamental issue with addressing array elements.
Dim Array_count As Byte

'##### Configure Arrays #####
Dim Sample_Array[10] As Word
Dim Buffer_Array[10] As Word

'##### Fill Sample array #####
For Array_count = 0 To 9
Sample_Array[Array_count] = Array_count
Next

'##### Check array has been filled #####
Print At 1,1,"Array Values"
For Array_count = 0 To 9
Print At 2,1, Dec Sample_Array[Array_count]
DelayMS 1000
Next

Main:
Print At 1,1,"Buffer Array"
Buffer_Array[3] = Sample_Array[8]*256 + Sample_Array[9]
Print At 2,1, Dec Buffer_Array[3]
GoTo Main 'Loop forever

Probably not a particularly relevant point but to ensure correct order of precedence you might want to change Distance = DatoSensor[10]*256 + DatoSensor[11] to Distance = (DatoSensor[10]*256) + DatoSensor[11]

Maybe the reason only the first element in the array is being populated is that there is a problem with the str command filling it, looking at the manual you may be able to use...
QuoteHbusin Control, { Address }, [ Variable {, Variable...} ]
as an alternative. So perhaps try...
HBusIn R_ADDRESS,[DatoSensor[0], DatoSensor[1], DatoSensor[2]] instead of HBusIn R_ADDRESS,[Str DatoSensor] and see if that makes any differnce.

Craig

#12
Thanks Very Much Stephen
Will look into it tomorrow again but, I think you are right with how the string is being filled, what I found was if I load a for loop where the Raw data is filling then I could get it to load all the elements
But, your way looks simpler and will work faster, thanks very much. will test tomorrow.

Kind Regards
Craig

See_Mos

#13
The first sensor that I bought from Ebay was faulty, the second has just arrived.

I am using a 40 by 4 character display to show the values.  The first thing is that the code in post #1 has a misplaced 'Return' right at the end.

First in the procedure I replaced HRSOut Dec Byteout with Print Dec3 Byteout," " and all of the values were zero.

Next directly after HBusin I added a loop to show the values were being put into the array.  The values were no longer zero.

HBusIn R_ADDRESS,[Str DatoSensor]
Print At 1,1
For index = 0 To 8
Print Dec3 DatoSensor[index]," "
Next

I next commented out the above and the procedure call and added more loops

'SendBuffer(BufferArray, 0, 7)          ' Transmit 8 bytes from position 0 of BufferArray
Print At 1,1
For index = 0 To 4
Print Dec3 DatoSensor[index]," "
Next
Print At 2,1
For index = 5 To 9
Print Dec3 DatoSensor[index]," "
Next
Print At 3,1
For index = 10 To 14
Print Dec3 DatoSensor[index]," "
Next

Print At 4,1,"D: ",Dec4 Distance  ' Print The Distance in mm on LCD Display

Finally I commented out the above and added

Ambient = DatoSensor[8]*256 + DatoSensor[9]

Print At 1,1,"SPAD     ",Dec2 SPAD      ' Float value
Print At 2,1,"Signal   ",Dec5 Signal    ' Word value
Print At 3,1,"Ambient  ",Dec5 Ambient   ' Word value
Print At 4,1,"Distance ",Dec4 Distance  ' Word value

So it appears that the problem is within the procedure.



See_Mos

That part was easy,  I downloaded some of the stuff from ST, Arduino and Polulu in the hope of finding out more about the device and the purpose of the string of bytes but trying to understand it all is hurting my old head!

Maybe tomorrow I will have a better day!

Craig

Thank you Stephan and See_Mos for all your Efforts and Help I appreciate It very much,
These are very interesting and Useful Little Devices.

What I tried was this and it loads all the elements of the Array perfectly.

' Read The 12 Bit Raw Data Of The VL53L0x Sensor:

    HBusIn R_ADDRESS,[Str DatoSensor]

    For L = 0 To 11
     BufferArray[L] = (DatoSensor[10]*256) + DatoSensor[11] 
     Distance =  BufferArray[L]
     HRSOut "BUFj: ",Dec3 BufferArray[L]," "   
     Print At 1,1, Dec3 BufferArray[L]," "                         
    Next L

When I have a bit of spare time in the week I will Spend a bit of time playing with the Device and Have a closer look at things.

Regards
Craig

chris_cb_uk

@Craig just reviving this post, did you have success in the end with this sensor? I have a project where it would be useful for knowing distances around an object.
Do you mind sharing the working code element from the above which works?

Craig

Hi Chris
The original code was done by MTH an old forum user, what I found was an issue loading into BufferArray[J] which now works fine if put into a loop
as below!

I was using a Pic 18F47K40 @ 16Mhz

    Dim J As Byte
    Dim DatoVL As Byte
    Dim DatoSensor[12] As Byte
    Dim Distance As Word
    Dim DistL As Word
    Dim DistFlag As Byte                            ' Flag Used to See When VLX53L0x Has been filled with a Distance to go to TX
    Dim devError As Byte
    Dim SPAD As Float
    Dim Signal As Word
    Dim Ambient As Word
    Dim DisRest As Word                             ' Load VL53L0X "DISTANCE" from (mm) into (cm) Held in "DisRest"

'=======================================================================================
' Setup the VL53L0x - I2C
'---------------------------------------------------------------------------------------
    Declare Hbus_Bitrate = 400
    Declare HSCL_Pin = PORTC.3                              ' (WHITE) Symbol SCL (OUTPUT)- VL53L0x TOF Sensor
    Declare HSDA_Pin = PORTC.4                              ' (BLUE)  Symbol SDA (INPUT) - VL53L0x TOF Sensor

'=========================================================================================
' Setup the VL53L0x - TOF Lazer Unit "EAST/POLOLU"
' VL53L0x Registers Write and Read
'-----------------------------------------------------------------------------------------
    Symbol W_ADDRESS = $52
    Symbol R_ADDRESS = $53

'-----------------------------------------------------------------------------------------------
' VL53L0X TOF Sensor
' XSHUNT Pin Must Go (HIGH +5V) To Enable Sensor or (LOW 0V) To Switch Sensor OFF
'===============================================================================================
    VL53Lx:
'System Range Start
    HBusOut W_ADDRESS,[$00,$01]
    DelayMS 5

'Range status
    HBusOut W_ADDRESS,[$14]
    DelayMS 5

' Read The 12 Bit Raw Data Of The VL53L0x Sensor:
    HBusIn R_ADDRESS,[Str DatoSensor]


  For J = 0 To 11
        BufferArray[J] = (DatoSensor[10]*256) + DatoSensor[11]   ' Fill All 12 Elements of the BufferArray With Distance Measurements from VL53L0X
        DistL = BufferArray[J]                                   ' Load BufferArray J Elements into DistL Variable
        Distance =  DistL                                        ' Push DistL Variable into the Distance Variable
      ' HRSOut "BUFj: ",Dec3 BufferArray[J]," "                  ' Switch On For TESTING " Be Careful it Slows Everything Down"
      ' HRSOut "Distance: ",Dec distance," "                     ' Switch On For TESTING " Be Careful it Slows Everything Down"
      ' HRSOut "DistL: ",Dec DistL," "                           ' Switch On For TESTING " Be Careful it Slows Everything Down"
  Next J

'-----------------------------------------------------------------------------------------------
' Resolve Sensor Error List:
'===============================================================================================
 ''devError = DatoSensor[0] & 0x78 >> 3 ' // Check for errors
 ''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!"
 '''Print At 3,10,"Dist: ",hex2 devError," "

'-----------------------------------------------------------------------------------------------
' Resolve info of sensor VL53L0x:
'===============================================================================================
'If devError = $00 or devError = $06 update Distance
'If devError = $00 Or devError = $06 Then
'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]

' Display Output Data:
    Print At 1,6,"D: ",Dec Distance,"  "                                                        ' Print Distance in mm
    HRSOut "VL: ", Dec3 Distance,"  "                                                           ' PC TESTING
 
 Return

'-------------------------------------------------------------------
' Read VL53L0x Sensor Info
'-------------------------------------------------------------------
 Info_VL53:

'IDENTIFICATION REVISION ID
    HBusIn R_ADDRESS,$C2,[DatoVL]
                                                               

'IDENTIFICATION MODEL ID
    HBusIn R_ADDRESS,$C0,[DatoVL]
 
'PRE RANGE CONFIG VCSEL PERIOD
    HBusIn R_ADDRESS,$50,[DatoVL]

'FINAL RANGE CONFIG VCSEL PERIOD
    HBusIn R_ADDRESS,$70,[DatoVL]

Return


chris_cb_uk

@Craig many thanks i will digest this and get my head around it. I had purchased a VL53L1X module but struggled to find any details out about its addresses etc as St only released apis and information is very sketchy.

joesaliba

Quote from: chris_cb_uk on Feb 10, 2022, 09:46 PM@Craig many thanks i will digest this and get my head around it. I had purchased a VL53L1X module but struggled to find any details out about its addresses etc as St only released apis and information is very sketchy.

Waiting for my VL53L1x module. Following this thread and progress.

Regards

Joe