Defining array in structure

General discussion on mikroBasic PRO for dsPIC30/33 and PIC24.
Post Reply
Author
Message
jmusselman64
Posts: 31
Joined: 15 May 2018 21:05

Defining array in structure

#1 Post by jmusselman64 » 09 Mar 2020 05:49

I'd like to use a common Structure to define multiple transmit/receive buffers. The only difference between them would be the size of the buffer, defined by a constant.

How would I implement that? In the attached example, the array 'buffer' is defined in the structure with the constant FIFO_SIZE...which would need to vary somehow depending on TX_BUFFER_SIZE or RX_BUFFER_SIZE.
There could be up to 8 FIFO's used...

Thanks much!

Code: Select all

program StructTest

'receive and transmit buffers have different sizes:
const TX_BUFFER_SIZE = 32
const RX_BUFFER_SIZE = 128

'Buffer structure is the same in both RX and TX:
Structure FIFO_s
        dim head as word                  ' index of oldest data in buffer (tail)
        dim tail as word                   ' index of newest data in buffer (head)
        dim num_elements as word             ' number of bytes/words currently in buffer
        dim not_empty_flag as byte volatile  ' this flag is automatically set and cleared by the software buffer
        dim full_flag as byte volatile       ' this flag is automatically set and cleared by the software buffer
        dim ovf_flag as byte volatile        ' this flag is not automatically cleared by the software buffer

'Dimension buffer with TX or RX buffer size constant:
        dim buffer as byte[FIFO_SIZE]     ' FIFO buffer
end structure

'Define the buffers here:
Dim RX_Buffer as FIFO_s
Dim TX_Buffer as FIFO_s

main:

'Assign values to test structure:
RX_Buffer.num_elements = 16
TX_Buffer.head = 3

while(1)
wend

end.

jmusselman64
Posts: 31
Joined: 15 May 2018 21:05

Re: Defining array in structure

#2 Post by jmusselman64 » 13 Sep 2020 16:00

..any takers...?

I really don't want to define 8 different, but essentially identical structures...

Thanks,
Jerry

User avatar
stefan.filipovic
mikroElektronika team
Posts: 1135
Joined: 18 Dec 2018 10:30

Re: Defining array in structure

#3 Post by stefan.filipovic » 14 Sep 2020 14:00

Hi Jerry,

You can try this way:

Code: Select all

'receive and transmit buffers have different sizes:
const TX_BUFFER_SIZE = 32
const RX_BUFFER_SIZE = 128

'Buffer structure is the same in both RX and TX:
Structure FIFO_s
        dim head as word                  ' index of oldest data in buffer (tail)
        dim tail as word                   ' index of newest data in buffer (head)
        dim num_elements as word             ' number of bytes/words currently in buffer
        dim not_empty_flag as byte volatile  ' this flag is automatically set and cleared by the software buffer
        dim full_flag as byte volatile       ' this flag is automatically set and cleared by the software buffer
        dim ovf_flag as byte volatile        ' this flag is not automatically cleared by the software buffer

end structure

structure FIFO_sTx
          'Dimension buffer with TX or RX buffer size constant:
        dim buffer as byte[TX_BUFFER_SIZE]     ' FIFO buffer
        dim fifo as FIFO_s
end structure

structure FIFO_sRx
          'Dimension buffer with TX or RX buffer size constant:
        dim buffer as byte[RX_BUFFER_SIZE]     ' FIFO buffer
        dim fifo as FIFO_s
end structure

'Define the buffers here:
Dim RX_Buffer as FIFO_sRx
Dim TX_Buffer as FIFO_sTx

main:

   'Assign values to test structure:
   RX_Buffer.fifo.num_elements = 16
   TX_Buffer.fifo.head = 3

end.
Kind regards,
Stefan Filipović

jmusselman64
Posts: 31
Joined: 15 May 2018 21:05

Re: Defining array in structure

#4 Post by jmusselman64 » 14 Sep 2020 15:17

Thanks Stefan,

That'll work!..
I don't know why I never thought of including a structure withing a structure. I've seen it done before, too..

Jerry

Post Reply

Return to “mikroBasic PRO for dsPIC30/33 and PIC24 General”