News:

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

Main Menu

Use of..... Procedure array

Started by joesaliba, May 24, 2023, 05:59 PM

Previous topic - Next topic

joesaliba

Hi,

In the manual, the only reference to array within a procedure is the following: -

Proc MyProc(pMyArray[10] as Word)

How can I use the above?

Lets say I have another array named Packet.

Dim Packet[8] as Byte

How can I pass array Packet or any other byte to the procedure please?

Thank you

Joe

Stephen Moss

I don't recall having passed arrays to a procedure before but I think it would be...

Dim Packet[8] as byte   'Create the Array

Proc MyProc(Packet)   'Call the Procedure & pass it the Array as a parameter

Proc MyProc(pMyArray[8] as Byte) 'Define the start to the procedure and define an 8 byte array (pMyArray) to accept the passed array data
End Proc

top204

#2
There are two ways of passing an array to a procedure, and they are ByVal and ByRef. The ByVal is the default, and is implied if not used before the parameter's name, and it holds a copy of the array passed to it. Byref means the parameter holds the array's address that was sent to it, so it can be accessed with the PtrX commands. Known in technical terms as: "indirectly accessed"

Below is a demo program listing showing the different methods. ByRef also applies to standard variable parameters if required:

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' Demonstrate an array parameter by value and by reference
'
' Written for the Positron8 compiler by Les Johnson.
'
    Device = 18F25K20                               ' Tell the compiler what device to compile for
    Declare Xtal = 16                               ' Tell the compiler what frequency the device will be operating at (in MHz)
'
' Setup USART1
'
    Declare Hserial1_Baud = 9600                    ' Set the Baud rate to 9600
    Declare HRSOut1_Pin = PORTC.6                   ' Set the TX pin
'
' Create any variables for the demo here
'
    Dim bMyArray[10] As Byte = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

'-------------------------------------------------------------------------
' The main program starts here
' Call two procedures to access the elements of a byte array in different ways
' And they will transmit the ASCII contents of the array to a serial terminal
'
Main:
    DisplayArrayV(bMyArray)                         ' Access and display the elements by value

    DisplayArrayI(bMyArray)                         ' Access and display the elements by reference
   

'-------------------------------------------------------------------------------------------------------------
' Access the elements of a byte array parameter by value and transmit them to a serial terminal
' Input     : pArray holds a copy of the array sent
' Output    : None
' Notes     : With the array as the parameter, it will use more RAM, and more time
'
Proc DisplayArrayV(pArray[10] As Byte)
    Dim bIndex As Byte

    HRsoutLn "By Value"
    For bIndex = 0 to 9
        HRsoutLn "Element ", Dec bIndex, " = ", Dec pArray[bIndex]
    Next
EndProc

'-------------------------------------------------------------------------------------------------------------
' Access the elements of a byte array parameter by reference and transmit them to a serial terminal
' Input     : pArrayAdd holds the address of the array sent
' Output    : None
' Notes     : With the address of the array as the parameter, it will not use much RAM, and operate faster
'
Proc DisplayArrayI(ByRef pArrayAdd As Word)
    Dim bIndex As Byte

    HRsoutLn "\rBy Ref"
    For bIndex = 0 to 9
        HRsoutLn "Element ", Dec bIndex, " = ", Dec Ptr8(pArrayAdd++)
    Next
EndProc

The arrays can be any of the valid types supported by the compilers, and the only difference required inside the procedure if passed ByRef is the Ptr type to use. i.e. Ptr8 for 8-bit access, Ptr16 for 16-bit access, Ptr24 for 24-bit access, and Ptr32 for 32-bit access (including 32-bit Floats), and Ptr64 for 64-bit access in the Positron16 compiler. The As Word statement after a ByRef parameter, is so it creates a 16-bit variable to hold the RAM address of the variable sent to the procedure, and because RAM does not go above a 16-bit address, it can be used for all variable parameter types sent as ByRef.

Below is a screenshot of the above program operating in the proteus simulator:

Display_Array_Elements.jpg

joesaliba

Thank you Les for the detailed information provided.

This information should be added to the manual regarding passing array to a procedure.

Stephen was also spot on, unlike me!

Regards

joe