News:

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

Main Menu

HRSOUT with Array of bytes

Started by Amateurtje, Jul 10, 2021, 09:51 AM

Previous topic - Next topic

Amateurtje

Hello,

I was always sending fixed amounts of data via HRSOUT.

I wold like to use same setup, but, Now, I would like to send an array of bytes (sendbuffer) that is max size 900 but a lot of times smaller.
I would like to send only the relevant part of the buffer. the part is untill  size a (dim a as word).

The array will contain a lot of 0 values... therfore I need to fix the amount of the buffer that will be send..

My idea was to use:

HRSOut Str sendbuffer \ a
Unfortunatly,that does not work: Must be a constant value for Str length.

Anybody a good idea to overcome this (except making a select case with 900 entries).....

top204

#1
Now that the Positron8 compiler has procedures, it is not always required to use the original modifier directives.

Below is a simple demo with a procedure to do as you require. It reads from position X within a buffer array sent to it, and serially transmits X bytes from that position onwards. The same type of mechanism can also be used to transmit Word, Long, Dword and Float arrays, and receive into Byte, Word, Long, Dword or Float arrays.

The key part of it is the ByRef directive parameter, that loads a variable with the RAM address of the variable sent to it, not the value contained within it. Then the Ptr8 function reads an 8-bit value from the array with an auto increment of the address.

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' A demo program to serially transmit X amount of bytes from X position within a byte array using a procedure
'
' Written by Les Johnson for the Positron BASIC compiler
'
    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 a buffer array for the demo and place some values into it
'
    Dim BufferArray[100] As Byte = {00, 01, 02, 03, 04, 05, 06, 07, 08, 09,
                                    10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
                                    20, 21, 22, 23, 24, 25, 26, 27, 28, 29}

'---------------------------------------------------------
' The main program starts here
'
Main:
    SendBuffer(BufferArray, 5, 10)          ' Transmit 10 bytes from position 5 of BufferArray
   
'---------------------------------------------------------
' Transmit bytes from a buffer array
' Input     : pArrayAddr holds the address of the byte array
'           : pStart holds the starting index of the array to transmit (0 to x)
'           : pAmount holds the amount of bytes to transmit
' Output    : None
' Notes     : Uses the HRsout command to transmit the bytes
'
Proc SendBuffer(ByRef pArrayAddr As Word, pStart As Word, pAmount As Word)
    Dim Byteout As Byte
   
    pArrayAddr = pArrayAddr + pStart        ' Find the starting index address of the bytes to transmit
    Repeat                                  ' Create a loop
        Byteout = Ptr8(pArrayAddr++)        ' Read a byte from the buffer array
        HRSOut Byteout                      ' Transmit the byte read from the array
        Dec pAmount                         ' \
    Until pAmount = 0                       ' / Loop until the amount of bytes are transmitted
EndProc

To see the efficiency of the compiler, just look at the assembler code produced by the procedure, considering it is using 16-bit position and amount parameter variables:

F1_000044 equ $ ; in [TEST_18F25K20.BAS] Proc SendBuffer(ByRef pArrayAddr As Word, pStart As Word, pAmount As Word)
SendBuffer
F1_000047 equ $ ; in [TEST_18F25K20.BAS] pArrayAddr = pArrayAddr + pStart
    movf SendBufferpStart,W,1
    addwf SendBufferpArrayAddr,F,1
    movf SendBufferpStartH,W,1
    addwfc SendBufferpArrayAddrH,F,1
F1_000048 equ $ ; in [TEST_18F25K20.BAS] Repeat
_lbl__4
F1_000049 equ $ ; in [TEST_18F25K20.BAS] Byteout = Ptr8(pArrayAddr++)
    movff SendBufferpArrayAddrH,FSR1LH
    movff SendBufferpArrayAddr,FSR1L
    movff POSTINC1,SendBufferByteout
    infsnz SendBufferpArrayAddr,F,1
    incf SendBufferpArrayAddrH,F,1
F1_000050 equ $ ; in [TEST_18F25K20.BAS] HRsout Byteout
    movf SendBufferByteout,W,1
    rcall __hrsout1__
F1_000051 equ $ ; in [TEST_18F25K20.BAS] Dec pAmount
    decf SendBufferpAmount,F,1
    movlw 0
    subwfb SendBufferpAmountH,F,1
_lbl__6
F1_000052 equ $ ; in [TEST_18F25K20.BAS] Until pAmount = 0
    movf SendBufferpAmountH,W,1
    iorwf SendBufferpAmount,W,1
    bnz _lbl__4
_lbl__5
F1_000053 equ $ ; in [TEST_18F25K20.BAS] EndProc
    return 0 ; EndProc

Not bad is it? :-)

gpauk

Yes - now write that in C and weep at the amount of code... :)

Amateurtje

Hi,

It great .Works like a charm... Thanks a lot.... I should start using more procedures...  like in vb.net.. great...

For 18F46k22, a big array of 400 elemenst isn't a problem, isn't it? the manual says it is oke but in the past i remember having some strange behaviour with it..


PS, diferent issue.. i get all kind of strange behaviour with the Undo function in the editor... complete pieces of code can dissapear....

normnet

Quote from: Amateurtje on Jul 11, 2021, 07:55 PM...PS, diferent issue.. i get all kind of strange behaviour with the Undo function in the editor... complete pieces of code can dissapear....

Please inform us of which editor you are using?

TimB


The issue I have found with the build in editor is that you can do a ctrl+z with no issues but never do a ctrl+y.

The later WILL destroy your data!

top204

#6
Unfortunately, the IDE that is installed with the compilers is by a third party and I do not have access to its source code. Remember... The IDE and the actual compilers are seperate entities and the compilers do not care what IDE calls them as long as their command lines are correct.

John and Norm's IDEs are excellent and highly recommended, and are constantly maintained.

There are no problems with large arrays and never has been since they were added to the compilers, many years ago. The 18F and enhanced 14-bit core devices have linear RAM when accessed indirectly, so no problems occur.

Because of the fragmented RAM on standard, and now obsolete, 14-bit core devices, large arrays should be avoided because the compiler has to use subroutines to join the fragmented RAM address' together, and that takes time and code space. However, it was the first compiler for the PIC microcontrollers to allow large arrays. :-)

Amateurtje

I use the Mecanique proton IDE 2.0.3.3.  Already for many years.. I thought there were never problems with the undo and redo functions, but lately I had a couple of times half the program was gone and could not be retrieved.....

Where can we get the IDE of John and Norm? Isn't it better to supply these with the compiler? (or is that something political....?)

About the large arrays, I seem to remeber something and that is why I cut some arrays in pieces in the past... maybe something else was wrong....I wil know soon while I used a big array again.. maybe it is also time to review my old code.... (although, pffffff) ;D

Stephen Moss

Quote from: Amateurtje on Jul 13, 2021, 10:59 PMWhere can we get the IDE of John and Norm?
You might wat to check out the 3rd Party IDE section of the Forum to get up to date with them first.
Fine Line - Link and instructions for download are in the first post.
Positron Studio - as I recall you have to create an account at the website and log in to download.