Arrayread problem


Closed Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2010
    Posts
    19

    Default Arrayread problem

    It's late so I'm probably missing something right in front of me.
    I'm using a pic18f4331 and I can't seem to be able to read a byte array with 3 segments into a word variable. I'm reading a keypad to load the array and then I want the value entered in the keypad in one number. Since I'm stuck I wrote the basic idea to test in this code but to no avail. Please help.

    HN VAR BYTE[3]
    POS VAR word
    I VAR BYTE

    'I've loaded HN simulating keypad entry with the number 321

    MAIN:
    HN[0] = 3
    HN[1] = 2
    HN[2] = 1

    'The for loop works by reading back each segment showing they are loaded in the right position

    FOR I = 0 TO 2
    LCDOUT $FE,$1,DEC HN[I]
    PAUSE 2000
    NEXT I


    'Here is where I'm stuck. I can't seem to read the entire array out to one variable. I want the number
    ' 321 in the POS variable. Maybe there is a better way to load keystrokes into one variable for an accumulated
    ' number.

    ARRAYREAD HN,[DEC POS]
    LCDOUT $FE,$C0,"VALUE:",DEC POS
    PAUSE 2000
    GOTO MAIN
    END

  2. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    2,388


    Did you find this post helpful? Yes | No

    Default Re: Arrayread problem

    I might be wrong but try
    ARRAYREAD HN,[DEC3 POS]

  3. #3
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,520


    Did you find this post helpful? Yes | No

    Default Re: Arrayread problem

    Hi,
    When you use the DEC modifier in the LCDOUT statement (or HSEROUT or SEROUT etc) it converts the binary value to ASCII. When you use it the ARRAYREAD (or HSERING or SERIN etc) it does the opposit, ie convert the ASCII representation of a number consisting of one or more digits (stored in the array in this case) into a binary number. For that to work in your particular example you need to load the array like
    Code:
    HN[0] = "3"
    HN[1] = "2"
    HN[2] = "1"
    Then, to read it you can use Richards example but it'll only work if there's always 3 characters in the array. If the length differs you need to make sure that there's something NOT coresponding to the ASCII value of a digit or "-" after the last digit by, for example, clearing the array before loading a value to it.

    /Henrik.

  4. #4
    Join Date
    Jan 2010
    Posts
    19


    Did you find this post helpful? Yes | No

    Default Re: Arrayread problem

    Hmmm plot thickens. In my main program I got it to work today using ARRAYREAD ARRAY,[DEC VARIABL] AND THEN LCDOUT $FE,$1, DEC VARIABLE... The strange thing is that it would work the first time around in the menu I made. Then when you try and enter another number into the array it wouldn't work. So I added a FOR loop at the beginning of the code to load the array with "0" that seemed to clear out the array and now it works every time. But in the example I gave above which is no different then my main code I can't get it to work. Although I have it working it still bothers me as to why. Thanks for your help.

  5. #5
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,520


    Did you find this post helpful? Yes | No

    Default Re: Arrayread problem

    Hi,
    I can't see how you've got it working if you don't load the array with the ASCII values of the numbers you later want in the variable. Here's a piece of testcode I just wrote:
    Code:
    BufferLength CON 16
    
    Array VAR BYTE[BufferLength]
    Variable1 VAR WORD
    Variable2 VAR WORD
    i VAR WORD
    Test VAR BYTE
    
    Pause 2500
    
    HSEROUT["Start...",13,13]
    
    Start:
        CLEAR
        Test = 1
        ArrayRead Array,[DEC Variable1]
        ArrayRead Array,[Variable2]
        GOSUB PrintIt
    '----------------------------------------------------------------------------------------------
        CLEAR                           ' Make sure we clear the array (and every other variable)
        Test = 2
        Array[0] = 1                    ' Populate array with binary value
        Array[1] = 2
        Array[2] = 3
        ArrayRead Array,[DEC Variable1] ' Try to read the array into variable using DEC modifier
        ArrayRead Array,[Variable2]
        GOSUB PrintIt                   ' Display results
    '----------------------------------------------------------------------------------------------
        CLEAR                           ' Make sure we clear the array (and every other variable)
        Test = 3
        Array[0] = "1"                  ' Populate array with ASCII representation of out value...49
        Array[1] = "2"                  ' 50
        Array[2] = "3"                  ' 51
        ArrayRead Array,[DEC Variable1] ' Try read the array into variable using DEC modifier.
        ArrayRead Array,[Variable2]
        GOSUB PrintIt                   ' Display results
    '----------------------------------------------------------------------------------------------
        Test = 4                        ' This time we DON'T clear the array
        Array[0] = 5                    ' Load the array with binary values
        Array[1] = 6
        'Array[2] = 7                   ' And we purposly  don't populate array[2]
        ArrayRead Array,[DEC Variable1] ' Try read the array into variable using DEC modifier.
        ArrayRead Array,[Variable2]
        GOSUB PrintIt                   ' Display results
    '----------------------------------------------------------------------------------------------
        Test = 5                        ' This time we DON'T clear the array either
        Array[0] = "5"                  ' But load the first to locations with ASCII representations of the value 56
        Array[1] = "6"
        ' Array[2] = 7                  ' Again, we don't populate array[2]
        ArrayRead Array,[DEC Variable1] ' Try read the array into variable using DEC modifier.
        ArrayRead Array,[Variable2]
        GOSUB PrintIt                   ' Display results
    '----------------------------------------------------------------------------------------------
        Test = 6                        ' We still don't clear the array
        Array[0] = "8"                  ' But load it with the ASCII representation of 89
        Array[1] = "9"
        Array[2] = 0                    ' This time we pad a null to the end though.
        ArrayRead Array,[DEC Variable1] ' Try read the array into variable using DEC modifier.
        ArrayRead Array,[Variable2]
        GOSUB PrintIt                   ' Display results
    '----------------------------------------------------------------------------------------------
        Test = 8                        ' We still don't clear the array
        Array[0] = "4"                  ' But load it with the ASCII representation of 47
        Array[1] = "7"
        Array[2] = 65                   ' This time we pad a value not corresponding to a numeric digit.
        ArrayRead Array,[DEC Variable1] ' Try read the array into variable using DEC modifier.
        ArrayRead Array,[Variable2]
        GOSUB PrintIt
    '----------------------------------------------------------------------------------------------
        Test = 7                       ' We still don't clear the array
        Array[0] = 49                  ' But load it with the ASCII representation of 1234
        Array[1] = 50
        Array[2] = 51
        Array[3] = 52
        ArrayRead Array,[DEC Variable1] ' Try read the array into variable using DEC modifier.
        ArrayRead Array,[Variable2]
        GOSUB PrintIt
    '----------------------------------------------------------------------------------------------
    
    Pause 10
    END 
    
    PrintIt:
        HSEROUT["Test ", DEC Test, ":",13]
        For i = 0 to (BufferLength-1)
            HSEROUT[DEC Array[i], ","]
        NEXT
        HSEROUT [13]
        HSEROUT ["Variable 1:", DEC Variable1,13]
        HSEROUT ["Variable 2:", DEC Variable2,13,13]
    RETURN
    Let's examine the output of each of the tests:
    Code:
    Test 1:
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    Variable 1:8
    Variable 2:0
    Here, all the array locations are null/zero. I don't know why the ArrayRead routine with the DEC modifier returns 8 but I guess the routine is actually using the Variable1 (in this case) internally and when it aborts it happens to have the value 8 in it. It sort of must be that since we DO clear it prior to this routine.

    Code:
    Test 2:
    1,2,3,0,0,0,0,0,0,0,0,0,0,0,0,0,
    Variable 1:8
    Variable 2:1
    Again, the ArrayRead with the DEC modifier fails since the values in the Array aren't ASCII code for any digits.

    Code:
    Test 3:
    49,50,51,0,0,0,0,0,0,0,0,0,0,0,0,0,
    Variable 1:123
    Variable 2:49
    Now ArrayRead with DEC works because this time we loaded the Array with values corresponding the ASCII characters/digits that we later try and read with ArrayRead and the DEC modifier.

    Code:
    Test 4:
    5,6,51,0,0,0,0,0,0,0,0,0,0,0,0,0,
    Variable 1:3
    Variable 2:5
    This time we didn't clear the Array prior to reloading it. But since the first value isn't a valid ASCII code for a digit ArrayRead with DEC fails. And this time with another "random" number than in Test 1.

    Code:
    Test 5:
    53,54,51,0,0,0,0,0,0,0,0,0,0,0,0,0,
    Variable 1:563
    Variable 2:53
    This time we only "reloaded" the first two locations but since the third location already contained a valid ASCII code for a numering digit the DEC modifier "included" that when reading the array - as expected of course.

    Code:
    Test 6:
    56,57,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    Variable 1:89
    Variable 2:56
    Here we change the first two locations and "manually" padded a zero/null at the end. ArrayRead with the DEC modifier does its job.

    Code:
    Test 8:
    52,55,65,0,0,0,0,0,0,0,0,0,0,0,0,0,
    Variable 1:47
    Variable 2:52
    (Yes, got them in the wron order). This time the third location in the array isn't null/zero but it still works because it's not a valid ASCII code for a numeric digit.

    Code:
    Test 7:
    49,50,51,52,0,0,0,0,0,0,0,0,0,0,0,0,
    Variable 1:1234
    Variable 2:49
    And again it works fine when array contains ASCII data.


    As you can see, in order for ARRAYREAD to work with the DEC modifier the array must containf the ASCII codes for the numeric value - otherwise it won't work. Obviously I don't know what your main code looks like but hopefully you'll be able to figure out why it works (or parhaps why you think it works).

    /Henrik.

Similar Threads

  1. ArrayRead on longer arrays
    By amgen in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 21st September 2011, 16:07
  2. ArrayRead question
    By Charles Linquis in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 24th August 2011, 18:32
  3. Arrayread,arraywrite
    By suded in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 13th November 2009, 08:36
  4. Replies: 8
    Last Post: - 11th November 2004, 20:08

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts