News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

PROBLEM Accessing Individual Elements In ARRAY ?

Started by Craig, Sep 16, 2021, 07:47 PM

Previous topic - Next topic

Craig

Hello

Can someone Please explain to me where I am going Wrong? 

Positron8: 4.0.0.9

I have obviously shortened the code to keep the thread short!

Dim LArray1[6]    as Word 
Dim LDist          as Word  ' Global Variable
Dim LResult1      as Word  ' Global Variable
Dim LResult2      as Word  ' Global Variable


THIS Does NOT Work:----I Should be able to Print Element 1 and Element 4 of LArray1 and Access them as Individual Variables ????
This Just Prints Any Old Junk!! and causes Problems!!!
HRSOut "LARY1: ", Dec3 LArray1[1],"  ",LARY4: ",Dec3 LArray1#4,"  "




But, If I do the following then it works and prints everything correctly & I can see ALL the Sensor Data Correctly:

LArray1 = LDist              ' Loads Array info from LDist(Sensor Data) into LArray1

Repeat
 HRSOut "LARY1: ", Dec2 LArray1[ABCnt],"  "    ' Print Each Element In The Array - LArray1
 Inc ABCnt 
Until ABCnt =  5

'===========================================================================================

HOW Do I Access the Individual Elements of a WORD Array as if i Use Anything like this then It Just Causes Problems and Causes GHOST Data.
I have tried putting them in a Loop and Run through the Array Length as seen above but, the problem seems to be when
using LArray1[1]  or  LArray1#4 to try and Access the INDIVIDUAL Elements!!

 LResult1 = LArray1[1]     or 
 LResult2 = LArray1#4

I need to be able to access Individual Elements of a WORD ARRAY so that I can use them later to do Comparisons?


Regards
Craig











trastikata

Quote from: Craig on Sep 16, 2021, 07:47 PMThis Just Prints Any Old Junk!! and causes Problems!!!
HRSOut "LARY1: ", Dec3 LArray1[1],"  ",LARY4: ",Dec3 LArray1#4,"  "

There is a problem with the syntax - misplaced comma.

normnet

#2
I'm not sure but you may try copying the individual element of the array to a temporary (non array) variable and utilize it in your HRSOut.  Sometimes more lines of code is good and perhaps may even be faster as it may be somewhat simpler for the compiler.

Craig

Thanks trastikata
The Comma is just a Typo when I copied the code across so No that is not the Problem!

Thanks Norm I have tried copying the Single Array Element which is Supposed to act as an Individual Variable to a Temporary Word Variable
But, When you are trying this then there seems to be an issue with accessing a single Array Element ie using [1] or #1 in a WORD Array.
It does Work but, outputs ODD or False Readings.

The HRSout is just for Debugging and testing my problem is that I have to be able to access these single Elements which have been copied to non Array Word Variables
to do comparisons and then pass the new values over.

I wonder if this isn't a Problem in the Compiler with Single Elements in WORD Array Variables [] or # ? 

TimB

Hi

Sorry if I did not read it correctly

In your example you have Dim LArray1[6]    as Word

Then try and read it as a byte with Dec3 LArray1#4,

If you want to get individual elements us

Myvar = LArray1[Varx]   
Where Varx = 0-5
Or define them in advance

Dim LArray1[6]    as Word
Dim ArrayWord1 as word at LArray1#0
Dim ArrayWord2 as word at LArray1#2
Dim ArrayWord3 as word at LArray1#4
Dim ArrayWord4 as word at LArray1#6
Dim ArrayWord5 as word at LArray1#8
Dim ArrayWord6 as word at LArray1#10

My syntax may be wrong but it's about right

I would though get into the habit of looking at the asm produced to check its working the way you expect

Craig

Thanks Tim

It seems to work better with assigning them in advance as per your suggestion, at least this way I am getting a fixed value in some of the Array elements and can work with that.
I will try tomorrow to index them in again in a different sequence and see how that works but, it is working much better than before.
Thanks for the help much appreciated!
Kind Regards

Craig

top204

#6
Each element of an array is assigned to the size of the array itself, so the contents of the array's elements will be transmitted correctly. For example, the program below:


'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
    Device = 18F25K20
    Declare Xtal = 16
'
' Setup USART1
'
    Declare Hserial1_Baud = 9600            ' Setup the Baud rate of USART1
    Declare HRSOut1_Pin = PORTB.6           ' Setup the pin used for USART1 TX
'
' Create some variables
'  
    Dim WordArray[10] As Word = 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000
    Dim bIndex As Byte
  
    For bIndex = 0 To 9                     ' Create a loop for the array's elements
        HRSOutLn Dec WordArray[bIndex]      ' Transmit the decimal value of an element
    Next                                    ' Close the loop

Produces, on a serial terminal, the texts:

1000
2000
3000
4000
5000
6000
7000
8000
9000
10000


Because each element contains a 16-bit integer value, and each element is assigned as a Word type variable.

Even if each element of the array is accessed individually. For example:

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
    Device = 18F25K20
    Declare Xtal = 16
'
' Setup USART1
'
    Declare Hserial1_Baud = 9600            ' Setup the Baud rate of USART1
    Declare HRSOut1_Pin = PORTB.6           ' Setup the pin used for USART1 TX
'
' 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
    HRSOutLn Dec WordArray[2]            ' Transmit the decimal value of an element
    HRSOutLn Dec WordArray[3]            ' Transmit the decimal value of an element
    HRSOutLn Dec WordArray[4]            ' Transmit the decimal value of an element
    HRSOutLn Dec WordArray[5]            ' Transmit the decimal value of an element
    HRSOutLn Dec WordArray[6]            ' Transmit the decimal value of an element
    HRSOutLn Dec WordArray[7]            ' Transmit the decimal value of an element
    HRSOutLn Dec WordArray[8]            ' Transmit the decimal value of an element
    HRSOutLn Dec WordArray[9]            ' Transmit the decimal value of an element

The text produced is:

1000
2000
3000
4000
5000
6000
7000
8000
9000
10000


Because each element is a 16-bit integer variable.

Or another way of accessing the elements individually:

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
    Device = 18F25K20
    Declare Xtal = 16
'
' Setup USART1
'
    Declare Hserial1_Baud = 9600            ' Setup the Baud rate of USART1
    Declare HRSOut1_Pin = PORTB.6           ' Setup the pin used for USART1 TX
'
' 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
    HRSOutLn Dec WordArray#2            ' Transmit the decimal value of an element
    HRSOutLn Dec WordArray#3            ' Transmit the decimal value of an element
    HRSOutLn Dec WordArray#4            ' Transmit the decimal value of an element
    HRSOutLn Dec WordArray#5            ' Transmit the decimal value of an element
    HRSOutLn Dec WordArray#6            ' Transmit the decimal value of an element
    HRSOutLn Dec WordArray#7            ' Transmit the decimal value of an element
    HRSOutLn Dec WordArray#8            ' Transmit the decimal value of an element
    HRSOutLn Dec WordArray#9            ' Transmit the decimal value of an element

Produces the correct texts because the individual elements are treated as individual Word type variable.

If you look at the assembler code for the above example, it can be seen that each element is treated as a Word type:

F1_000025 equ $ ; in [TEST_18F25K20.BAS] HRsoutLn Dec WordArray#0
    movlw 4
    movwf BPFH,0
    clrf GEN4H,0
    movff WordArray#0H,PP2H
    movff WordArray#0,PP2
    rcall __dec__ASCII__out
    movlw 13
    rcall __hrsout1__

See that it is placing both high and low bytes of the array's element into the system variables PP2 and PP2H, then transmitting their content via the "__dec__ASCII__out" library subroutine? The value placed into system variable BPFH tell the subroutine what it is going to send the ASCII decimal value from. i.e. LCD, USART1, USART2, I2C etc, and the value held in GEN4H tells the subroutine if any blanking is to be perfromed on the ASCII value. i.e. Amount of digits, and a 0 in GEN4H tells the subroutine, no blanking.

Craig

Thank you Les for your Very informative description of the Arrays and also your Explanation of the Assembly code Created. The F2 Key command is absolutely brilliant and works so well to see what is going on.

What I found was that specific Array was not loading the High Byte. When I Re_Declared it, it fixed that and showed both the Low & High Bytes correctly.
I can only think the reason was that for Neatness I had spaced the; Dim LArray1[10]       as Word .When I Did the Following;  Dim LArray1[10]  as Word    ,with No Space it was sorted.

However the Problem still persisted where it would Not load all of the Elements of the Array Correctly, I then noticed that I had declared;
Declare Stack_Size = 20
After Switching the Declare OFF It worked Perfectly as intended and Fills the Array Elements Absolutely Perfectly. For this Sensor which loads the Arrays
I was using the I2CIn & I2COut Commands, so not sure why the Stack_Size Declare would cause all these Problems as I was not putting anything into it.
I know it can overflow and cause Problems but, I did not realize this was possible if it is Declared and Not being Used?

The Device I am using is 18F47K40 @ 16Mhz with Positron8 - 4.0.0.9 If this is of any help for anyone having similar issues in the future!

Thank you again Les for always helping and giving so much support.

Kind Regards
Craig