• Welcome to Proton BASIC Community. Please login or sign up.
 
Oct 18, 2021, 04:59 AM

News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33


avatar_Craig

PLEASE HEEELP Being Driven Mad ?

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

Previous topic - Next topic

Craig

Sep 21, 2021, 08:40 PM Last Edit: Sep 22, 2021, 07:20 AM by 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:
http://protoncompilers.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

Sep 21, 2021, 09:31 PM #2 Last Edit: Sep 21, 2021, 09:40 PM by trastikata
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

Sep 22, 2021, 06:33 AM #4 Last Edit: Sep 22, 2021, 07:17 AM by Craig
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

Sep 23, 2021, 11:01 AM #8 Last Edit: Sep 23, 2021, 11:15 AM by See_Mos
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

Sep 23, 2021, 11:32 AM #9 Last Edit: Sep 23, 2021, 11:35 AM by Craig
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

Sep 23, 2021, 12:47 PM #10 Last Edit: Sep 23, 2021, 12:59 PM by trastikata
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

Sep 25, 2021, 08:16 AM #12 Last Edit: Sep 25, 2021, 08:21 AM by Craig
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

Sep 30, 2021, 02:38 PM #13 Last Edit: Sep 30, 2021, 02:46 PM by See_Mos
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