Two dimensional arrays?


Closed Thread
Results 1 to 13 of 13
  1. #1
    Join Date
    Nov 2008
    Posts
    48

    Default Two dimensional arrays?

    Is there a possibility to define 2 dimensional arrays in PBP, e.g. data(i,k) ?
    Regards,
    Ralf

  2. #2
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Not directly, but you can do the same thing programmatically.

    Say it's 8x8 ...
    Code:
    MyArray  VAR BYTE[64]
    
    A = MyArray(i*8+k)
    Indexes are 0-7.
    DT

  3. #3
    Join Date
    Jan 2009
    Location
    Miami, Florida USA
    Posts
    644


    Did you find this post helpful? Yes | No

    Default

    Hi,

    Well, I was thinking about starting a thread in the "PBP Wish List" about two dimensional arrays in PBP, but Darrel's approach would do just the same. Is is a clever way of mapping a two-dimensional space into a one-dimensional one. This is probably the way how other computer languages internally handle a multi-dimensional array.

    Robert

  4. #4
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default Ok, Everyone break out their Butterfly Nets. :)

    Code:
    ; -- Working with 2D arrays ---
    
    <font color="#000000"><b>Width      </b><font color="#008000"><b>CON </b></font><font color="#800000"><b>8                      </b></font><font color="#0000FF"><b><i>; Number of bytes in a Row
    </i></b></font><b>Length     </b><font color="#008000"><b>CON </b></font><font color="#800000"><b>10                     </b></font><font color="#0000FF"><b><i>; Number of Rows in the array
    ;---------------------------------------------------------------------------
    </i></b></font><b>ArraySize  </b><font color="#008000"><b>CON </b></font><b>Width </b>* <b>Length         </b><font color="#0000FF"><b><i>; Total size of the array
    </i></b></font><b>MyArray    </b><font color="#008000"><b>VAR BYTE</b></font>[<b>ArraySize</b>]        <font color="#0000FF"><b><i>; The Array
    
    </i></b></font><b>Idx        </b><font color="#008000"><b>VAR BYTE                   </b></font><font color="#0000FF"><b><i>; Index, points to 1D array locations
    </i></b></font><b>Xidx       </b><font color="#008000"><b>VAR BYTE                   </b></font><font color="#0000FF"><b><i>; 2D column Index
    </i></b></font><b>Yidx       </b><font color="#008000"><b>VAR BYTE                   </b></font><font color="#0000FF"><b><i>; 2D row Index
    </i></b></font><b>ArrayData  </b><font color="#008000"><b>VAR BYTE                   </b></font><font color="#0000FF"><b><i>; holding register for array data
    
    ;----[Get a value from the 2D array]----------------------------------------
    </i></b></font><b>Get2D</b>:
        <b>Idx</b>=(<b>Yidx</b>*<b>Width</b>+<b>Xidx</b>) <font color="#008000"><b>MIN </b></font>(<b>ArraySize</b>-<font color="#800000"><b>1</b></font>) <font color="#0000FF"><b><i>; find Index, limit to ArraySize 
        </i></b></font><b>ArrayData </b>= <b>MyArray</b>(<b>Idx</b>)                <font color="#0000FF"><b><i>; retrieve value from the array
    </i></b></font><font color="#008000"><b>RETURN
    
    </b></font><font color="#0000FF"><b><i>;----[Put a value in the 2D array]------------------------------------------
    </i></b></font><b>Put2D</b>:
        <b>Idx</b>=(<b>Yidx</b>*<b>Width</b>+<b>Xidx</b>) <font color="#008000"><b>MIN </b></font>(<b>ArraySize</b>-<font color="#800000"><b>1</b></font>) <font color="#0000FF"><b><i>; find Index, limit to ArraySize 
        </i></b></font><b>MyArray</b>(<b>Idx</b>) = <b>ArrayData                </b><font color="#0000FF"><b><i>; store the value in the array
    </i></b></font><font color="#008000"><b>RETURN
    
    </b></font><font color="#0000FF"><b><i>;----[Set entire array to 0's]----------------------------------------------
    </i></b></font><b>Clear2D</b>:
        <font color="#008000"><b>FOR </b></font><b>Idx </b>= <font color="#800000"><b>0 </b></font><font color="#008000"><b>TO </b></font><b>ArraySize</b>-<font color="#800000"><b>1
            </b></font><b>MyArray</b>(<b>Idx</b>) = <font color="#800000"><b>0
        </b></font><font color="#008000"><b>NEXT </b></font><b>Idx
    </b><font color="#008000"><b>RETURN
    
    </b></font><font color="#0000FF"><b><i>;----[Turn the Subroutines into Functions]----------------------------------
    </i></b></font><font color="#008000"><b>ASM
    </b></font><font color="#000080">#Get2D macro X, Y
        MOVE?BB   X, _Xidx                      </font><font color="#0000FF"><b><i>; copy users X value to Xidx
        </i></b></font><font color="#000080">MOVE?BB   Y, _Yidx                      </font><font color="#0000FF"><b><i>; copy users Y value to Yidx
        </i></b></font><font color="#000080">L?CALL    _Get2D                        </font><font color="#0000FF"><b><i>; call the Get subroutine
      </i></b></font><font color="#000080">endm                                      </font><font color="#0000FF"><b><i>; value is returned in ArrayData
    </i></b></font><font color="#000080">#define Get2D(X, Y)  #Get2D X, Y            </font><font color="#0000FF"><b><i>; allows paretheses in macro
    ;-----------------------
    </i></b></font><font color="#000080">#Put2D macro X, Y                           </font><font color="#0000FF"><b><i>; value should be in ArrayData
        </i></b></font><font color="#000080">MOVE?BB   X, _Xidx                      </font><font color="#0000FF"><b><i>; copy users X value to Xidx
        </i></b></font><font color="#000080">MOVE?BB   Y, _Yidx                      </font><font color="#0000FF"><b><i>; copy users Y value to Yidx
        </i></b></font><font color="#000080">L?CALL    _Put2D                        </font><font color="#0000FF"><b><i>; call the Put subroutine
      </i></b></font><font color="#000080">endm
    #define Put2D(X, Y)  #Put2D X, Y            </font><font color="#0000FF"><b><i>; allows paretheses in macro
    ;-----------------------
    </i></b></font><font color="#000080">#Clear2D macro
        L?CALL    _Clear2D                      </font><font color="#0000FF"><b><i>; call the Clear subroutine
      </i></b></font><font color="#000080">endm
    #define Clear2D()  #Clear2D
    </font><font color="#008000"><b>ENDASM
    
    </b></font>
    Code:
    <font color="#0000FF"><b><i>;____[The Main Program]_____________________________________________________
    
    </i></b></font><b>X        </b><font color="#008000"><b>VAR BYTE </b></font><b>SYSTEM
    Y        </b><font color="#008000"><b>VAR BYTE </b></font><b>SYSTEM
    
    Main</b>:
        @ <b>Clear2D                       </b><font color="#0000FF"><b><i>; clear the array
        
        </i></b></font><font color="#008000"><b>FOR </b></font><b>Y </b>= <font color="#800000"><b>0 </b></font><font color="#008000"><b>TO </b></font><b>Length</b>-<font color="#800000"><b>1           </b></font><font color="#0000FF"><b><i>; fill the array with data
          </i></b></font><font color="#008000"><b>FOR </b></font><b>X </b>= <font color="#800000"><b>0 </b></font><font color="#008000"><b>TO </b></font><b>Width</b>-<font color="#800000"><b>1
              </b></font><b>ArrayData </b>= <b>X             </b><font color="#0000FF"><b><i>; value to put in array
              </i></b></font>@ <b>Put2D</b>(<b>X</b>,<b>Y</b>)
          <font color="#008000"><b>NEXT </b></font><b>X
        </b><font color="#008000"><b>NEXT </b></font><b>Y
        
        </b><font color="#008000"><b>FOR </b></font><b>Y </b>= <font color="#800000"><b>0 </b></font><font color="#008000"><b>TO </b></font><b>Length</b>-<font color="#800000"><b>1           </b></font><font color="#0000FF"><b><i>; Read data back and send to serial port
          </i></b></font><font color="#008000"><b>FOR </b></font><b>X </b>= <font color="#800000"><b>0 </b></font><font color="#008000"><b>TO </b></font><b>Width</b>-<font color="#800000"><b>1
              </b></font>@ <b>Get2D</b>(<b>X</b>,<b>Y</b>)
              <font color="#008000"><b>HSEROUT </b></font>[<font color="#008000"><b>HEX2 </b></font><b>ArrayData</b>,<font color="#FF0000">&quot;, &quot;</font>]
          <font color="#008000"><b>NEXT </b></font><b>X
          </b><font color="#008000"><b>HSEROUT </b></font>[<font color="#800000"><b>13</b></font>,<font color="#800000"><b>10</b></font>]
        <font color="#008000"><b>NEXT </b></font><b>Y 
        </b><font color="#008000"><b>HSEROUT </b></font>[<font color="#800000"><b>13</b></font>,<font color="#800000"><b>10</b></font>]
    
        <font color="#008000"><b>PAUSE </b></font><font color="#800000"><b>3000
    </b></font><font color="#008000"><b>GOTO </b></font><b>Main</b>
    Code:
    [color=#0000FF][b][i];____[Working with 2D arrays]_____________________________________________________
    Width      CON 8                      ; Number of bytes in a Row
     Length     CON 10                     ; Number of Rows in the array
     ;---------------------------------------------------------------------------
     ArraySize  CON Width * Length         ; Total size of the array
     MyArray    VAR BYTE[ArraySize]        ; The Array
     
    Idx        VAR BYTE                   ; Index, points to 1D array locations
     Xidx       VAR BYTE                   ; 2D column Index
     Yidx       VAR BYTE                   ; 2D row Index
     ArrayData  VAR BYTE                   ; holding register for array data
     
    ;----[Get a value from the 2D array]----------------------------------------
     Get2D:
         Idx=(Yidx*Width+Xidx) MIN (ArraySize-1) ; find Index, limit to ArraySize 
        ArrayData = MyArray(Idx)                ; retrieve value from the array
     RETURN
     
    ;----[Put a value in the 2D array]------------------------------------------
     Put2D:
         Idx=(Yidx*Width+Xidx) MIN (ArraySize-1) ; find Index, limit to ArraySize 
        MyArray(Idx)=ArrayData                ; store the value in the array
     RETURN
     
    ;----[Set entire array to 0's]----------------------------------------------
     Clear2D:
         FOR Idx = 0 TO ArraySize-1
             MyArray(Idx)=0
         NEXT Idx
     RETURN
     
    ;----[Turn the Subroutines into Functions]----------------------------------
     ASM
     #Get2D macro X, Y
         MOVE?BB   X, _Xidx                      ; copy users X value to Xidx
         MOVE?BB   Y, _Yidx                      ; copy users Y value to Yidx
         L?CALL    _Get2D                        ; call the Get subroutine
       endm                                      ; value is returned in ArrayData
     #define Get2D(X, Y)  #Get2D X, Y            ; allows paretheses in macro
     ;-----------------------
     #Put2D macro X, Y                           ; value should be in ArrayData
         MOVE?BB   X, _Xidx                      ; copy users X value to Xidx
         MOVE?BB   Y, _Yidx                      ; copy users Y value to Yidx
         L?CALL    _Put2D                        ; call the Put subroutine
       endm
     #define Put2D(X, Y)  #Put2D X, Y            ; allows paretheses in macro
     ;-----------------------
     #Clear2D macro
         L?CALL    _Clear2D                      ; call the Clear subroutine
       endm
     #define Clear2D()  #Clear2D
     ENDASM
     
    
    Code:
    ;____[The Main Program]_____________________________________________________
    
    X        VAR BYTE SYSTEM
    Y        VAR BYTE SYSTEM
    
    Main:
        @ Clear2D                       ; clear the array
        
        FOR Y = 0 TO Length-1           ; fill the array with data
          FOR X = 0 TO Width-1
              ArrayData = X             ; value to put in array
              @ Put2D(X,Y)
          NEXT X
        NEXT Y
        
        FOR Y = 0 TO Length-1           ; Read data back and send to serial port
          FOR X = 0 TO Width-1
              @ Get2D(X,Y)
              HSEROUT [HEX2 ArrayData,&quot;, &quot;]
          NEXT X
          HSEROUT [13,10]
        NEXT Y 
        HSEROUT [13,10]
    
        PAUSE 3000
    GOTO Main
    Last edited by ScaleRobotics; - 3rd July 2012 at 13:30.
    DT

  5. #5
    Join Date
    Nov 2008
    Posts
    48


    Did you find this post helpful? Yes | No

    Default

    Hi Darrel,
    great idea, i will test this.
    BTW:
    1. What is maximum size of an array (i use 18F6722)? I would need two array's
    with a width of 10 bytes and a length of max 256!
    2. The length of the array should be dynamically, means that after configuring
    the SW via RS232, the controller will be reseted and the array length will be redefined.
    But this is not possible in runtime, or? Only at compile-time !?
    Regards,
    Ralf

  6. #6
    Join Date
    Nov 2008
    Posts
    48


    Did you find this post helpful? Yes | No

    Default

    Silly question, i only have 3936 bytes in a 6722....

  7. #7
    Join Date
    Mar 2010
    Posts
    40


    Did you find this post helpful? Yes | No

    Default

    dear darrel,

    may i know the ASM part is for? is that if i using PBP language ni need the ASM part? and when i compile the it have error on "@ Put2D (X,Y)" "Illegal character (()"

  8. #8
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Are you using MPASM for the assembler?

    It won't work with the default PM assembler.
    <br>
    DT

  9. #9
    Join Date
    Mar 2010
    Posts
    40


    Did you find this post helpful? Yes | No

    Default

    i using mplab and PICBASIC-PRO2.4.....and where i should i put the ASM file..

    ;----[Turn the Subroutines into Functions]----------------------------------
    ASM
    #Get2D macro X, Y
    MOVE?BB X, _Xidx ; copy users X value to Xidx
    MOVE?BB Y, _Yidx ; copy users Y value to Yidx
    L?CALL _Get2D ; call the Get subroutine
    endm ; value is returned in ArrayData
    #define Get2D(X, Y) #Get2D X, Y ; allows paretheses in macro
    ;-----------------------
    #Put2D macro X, Y ; value should be in ArrayData
    MOVE?BB X, _Xidx ; copy users X value to Xidx
    MOVE?BB Y, _Yidx ; copy users Y value to Yidx
    L?CALL _Put2D ; call the Put subroutine
    endm
    #define Put2D(X, Y) #Put2D X, Y ; allows paretheses in macro
    ;-----------------------
    #Clear2D macro
    L?CALL _Clear2D ; call the Clear subroutine
    endm
    #define Clear2D() #Clear2D
    ENDASM
    Last edited by fokus_1116; - 24th March 2010 at 03:46.

  10. #10
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by fokus_1116 View Post
    i using mplab and PICBASIC-PRO2.4.....and where i should i put the ASM file..
    Exactly where it was.
    It's part of the program.

    However, it's likely that it won't work with PBP 2.40.
    And I'm not about to look and see why.

    Buy the upgrade.
    2.40 is at least 10 years old.
    <br>
    DT

  11. #11
    Join Date
    Mar 2010
    Posts
    40


    Did you find this post helpful? Yes | No

    Default

    thank for fast reply....now it can work..thank....

  12. #12
    Join Date
    Mar 2010
    Posts
    40


    Did you find this post helpful? Yes | No

    Default

    dear sir,

    i dont why i use the array code that you post early compile no error..but when download to the pic16f873a it like stuck at somewhere at the initial array..below is my code...
    Code:
    INCLUDE "modedefs.bas"
    
    
    @ __config _HS_OSC & _WDT_OFF & _LVP_OFF & _CP_OFF
    ;----------------------------------------------------------------
    DEFINE OSC 20
    DEFINE LCD_DREG PORTA 'LCD Data line
    DEFINE LCD_DBIT 0
    DEFINE LCD_RSREG PORTB 'RS bit
    DEFINE LCD_RSBIT 5
    DEFINE LCD_EREG PORTB 'E bit
    DEFINE LCD_EBIT 4
    DEFINE LCD_RWREG PORTB 'RW bit
    DEFINE LCD_RWBIT 6
    DEFINE LCD_BITS 4
    DEFINE LCD_LINES 2
    DEFINE LCD_CHARS 20
    DEFINE LCD_COMMANDUS 2000
    DEFINE LCD_DATAUS 50
    LCDOUT $FE,1,"8"
    pause 500
    ;---------------------------------------------------------------
    VinTXD VAR PORTC.7 'AD0 TXD VDIP2 connect RX
    VinRXD VAR PORTC.6 'AD1 RXD VDIP2 connect TX
    FlowIn VAR PORTC.0 'AD2 RTS
    T VAR PORTC.0 'push button
    D VAR PORTC.1 ' PUSH BUTTON FOR DISPLAY
    LCDOUT $FE,1,"9"
    pause 500
    ;----------------------------------------------------------------
        ; -- Working with 2D arrays ---
    
    Width      CON 8                      ; Number of bytes in a Row
    Length     CON 12                     ; Number of Rows in the array
    ;---------------------------------------------------------------------------
    ArraySize  CON Width * Length         ; Total size of the array
    MyArray    VAR BYTE[ArraySize]        ; The Array
    
    Idx        VAR BYTE                   ; Index, points to 1D array locations
    Xidx       VAR BYTE                   ; 2D column Index
    Yidx       VAR BYTE                   ; 2D row Index
    ArrayData  VAR BYTE                   ; holding register for array data
    
    ;----[Get a value from the 2D array]----------------------------------------
    Get2D:
        Idx=(Yidx*Width+Xidx) MIN (ArraySize-1) ; find Index, limit to ArraySize 
        ArrayData = MyArray(Idx)                ; retrieve value from the array
    RETURN
    
    ;----[Put a value in the 2D array]------------------------------------------
    Put2D:
        Idx=(Yidx*Width+Xidx) MIN (ArraySize-1) ; find Index, limit to ArraySize 
        MyArray(Idx) = ArrayData                ; store the value in the array
    RETURN
    
    ;----[Set entire array to 0's]----------------------------------------------
    Clear2D:
        FOR Idx = 0 TO ArraySize-1
            MyArray(Idx) = 0
        NEXT Idx
    RETURN
    
    ASM
    #Get2D macro X, Y
        MOVE?BB   X, _Xidx                      ; copy users X value to Xidx
        MOVE?BB   Y, _Yidx                      ; copy users Y value to Yidx
    //    L?CALL    _Get2D                        ; call the Get subroutine
      endm                                      ; value is returned in ArrayData
    #define Get2D(X, Y)  #Get2D X, Y            ; allows paretheses in macro
    
    #Put2D macro X, Y                           ; value should be in ArrayData
        MOVE?BB   X, _Xidx                      ; copy users X value to Xidx
        MOVE?BB   Y, _Yidx                      ; copy users Y value to Yidx
        L?CALL    _Put2D                        ; call the Put subroutine
      endm
    #define Put2D(X, Y)  #Put2D X, Y            ; allows paretheses in macro
    
    #Clear2D macro
        L?CALL    _Clear2D                      ; call the Clear subroutine
      endm
    #define Clear2D()  #Clear2D
    ENDASM
    ;----------------------------------------------------------------
    ;a var BYTE[12]
    
    FILE1 VAR BYTE[12]
    FILE2 VAR BYTE[12]
    FILE3 VAR BYTE[12]
    ;X VAR BYTE
    
    LCDOUT $FE,1,"20"
    pause 500
    CRET VAR BYTE
    CRET = $0D
    ;----------------------------------------------------------------
    StartDisk:
    pause 3000
    HIGH VinRXD
    
    SEROUT2 VinRXD,84,["ECS",13]
    LCDOUT $FE,1, "1"
    pause 2500
    PAUSEUS 1000
    HIGH VinRXD
    PAUSEUS 3000
    SEROUT2 VinRXD,84,["IPA",13]
    LCDOUT $FE,1, "2"
    pause 2500
    HIGH VinRXD
    PAUSEUS 1000
    SEROUT2 VinRXD,84,["A:",13]
    LCDOUT $FE,1, "3"
    pause 2500
    HIGH VinRXD
    PAUSEUS 1000
    SEROUT2 VinRXD,84,["DIR",13]
    LCDOUT $FE,1, "4"
    'pause 2500
    PAUSEUS 1000
    ;------------------------------------------------------------
    X        VAR BYTE SYSTEM
    Y        VAR BYTE SYSTEM
    LCDOUT $FE,1,"11"
    pause 500
    ;----------------------------------------------------------------
    
    ;------------------------------------------------------------
    WAIT1
    IF FLOWIN = 1 THEN WAIT1
    
    LCDOUT $FE,1, "5"
    @ Clear2D                       ; clear the array
        FOR Y = 0 TO Length-1
        SERIN2 VinTXD,84,[WAIT(CRET),STR FILE1\12 \CRET]           ; fill the array with data
          FOR X = 0 TO Width-1
              ArrayData =FILE1[X]             ; value to put in array
              @ Put2D(X,Y)
          NEXT X
        NEXT Y
    
    ;--------------------------------------------------------------------------------
    PAUSE 1000
    FOR Y = 0 TO Length-1        
          FOR X = 0 TO Width-1
              @ Get2D(X,Y)
          NEXT X
        LCDOUT $FE,1,ArrayData
    pause 500
        NEXT Y 
    goto StartDisk
    Last edited by ScaleRobotics; - 21st November 2010 at 16:10. Reason: added code tags

  13. #13
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    I think you just need to jump over the subroutines with a GOTO.
    <br>
    DT

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