• PicŪ Basic


  • String array load And retrieve

    These two macros enable you to manipulate strings in a new way. Using Str_Array_In & Str_Array_Out you can now treat a string as an array and place sections of text into the middle of them.

    The example below will Dim a large String and then place new data into it. A temp string is loaded with the Text and a converted variable tagged onto the end. This temp string is then inserted into the main string; the whole process is then repeated 10 times.

    Lastly these 10 sections are read back and displayed on the terminal

    This file will run in ISIS on the PIC18_ALCD_VHB board

    As usual include the file "String_Array_InOut.inc" at the start of your code, then use as required.

    '
    ' Two macros that simulate a String array load and retrieve
    ' The macros use NO variable RAM other that SFR registers, and use no Call Stack space.
    '
    ' For 18F devices only. Using Proton+ Version 3.03 Onwards.
    '
    ' This program can be tested using the PIC18_ALCD_VHB file in the ISIS simulator.
    '
    Include "Proton18_4.inc" ' Use the Proton Dev board for the demonstration

    Dim Index1 As Byte ' Temporary byte used for demo loop
    Dim TempString As String * 10 ' Temporary string used by demo loop
    '
    ' Create a string large enough for all the characters
    '
    ' The values shown below will create an array of 10 strings each holding 10 characters
    Symbol Dimension_1 = 10 ' Ten Strings in the array
    Symbol Dimension_2 = 10 ' Each holding 10 characters
    Reminders = OFF
    Warnings = OFF
    Dim String1 As String * (Dimension_1 * Dimension_2) ' Create a string large enough to hold all the characters
    Warnings = On
    Symbol String1_Length = 10 ' Holds the length of each string within the array

    '--------------------------------------------------------------------------------------
    ' Simulate a String array String1[Index1] = CHARS_IN
    ' The algorithm for accessing the elements is: -
    ' FSR0 = String_Name
    ' PROD = Index1 * String_Length
    ' FSR0 = FSR0 + PROD

    ' Syntax: -
    ' Str_Array_Out String_Name , String_Length , [Index1] , CharactersIn

    ' Where: -
    ' String_Name is the name of the string array in question
    ' String_Length is the individual string lengths within the array
    ' Index1 is the index value of the string array
    ' CharactersIn is the characters to place into the array

    Str_Array_Out Macro- String_Name, String_Length, Index_1, CharactersIn

    #if(PRM_COUNT != 4) ; Ensure the correct amount of parameters
    #error "Incorrect amount of parameters for Str_Array_Out macro. 4 Parameters required"
    ExitM
    #endif
    #if(PRM_1 != String) ; Ensure the first parameter is a string
    #error "Parameter 1 must be a String for Str_Array_Out macro"
    ExitM
    #endif
    Asm-
    #if((PRM_3 == NUM8) || (PRM_3 == NUM16) || (PRM_3 == NUM32))
    movlw Index_1
    #endif
    #if((PRM_3 == Byte) || (PRM_3 == Word) || (PRM_3 == DWord))
    #if(Index_1 <= BANKA_END)
    movf Index_1,w
    #else
    movff Index_1,WREG
    #endif
    #endif
    EndAsm-
    #if((PRM_2 != NUM8) && (PRM_2 != NUM16) && (PRM_2 != NUM32))
    #error "String LENGTH must be an unsigned constant value for Str_Array_Out macro"
    #endif
    Asm-
    lfsr 0,String_Name ; Point FSR0L/H to the address of the string array
    mullw String_Length ; \
    movf PRODL,W ; |
    addwf FSR0L,F ; Calculate position of string array within main String
    movf PRODH,W ; |
    addwfc FSR0H,F ; /
    ;
    ; FSR0 is now pointing to the correct string within the array based on Index_1 and String_Length
    ;
    #if((PRM_4 == CHAR) || (PRM_4 == LABEL)) ; Is the parameter a quoted string of characters or a label?
    movlw Low CharactersIn ; \
    movwf TBLPTRL ; Point TBLPTRL/H to the code memory holding the string of characters
    movlw High CharactersIn ; |
    movwf TBLPTRH ; /
    clrf EECON1 ; \
    bsf EECON1,EEPGD ; / Setup for read from code memory
    movlw String_Length ; Create loop for all characters in string array part
    tblrd*+ ; Read then increment code memory pointer
    movf TABLAT,F ; \
    bz $ + 10 ; / Exit if a NULL found
    movff TABLAT,POSTINC0 ; Place the character from code memory to RAM
    decfsz WREG,F ; Next character
    bra $ - 12 ; Until all characters transfered
    #endif
    #if(PRM_4 == String) ; Is the parameter another string ?
    lfsr 1,CharactersIn ; Point FSR1L/H to the address of the source string
    movlw String_Length ; Create loop for all characters in string array part
    movf INDF1,F ; \
    bz $ + 10 ; / Exit if a NULL found
    movff POSTINC1,POSTINC0 ; Place the character from source string to dest string
    decfsz WREG,F ; Next character
    bra $ - 10 ; Until all characters transfered
    #endif
    #if((PRM_4 == Word) || (PRM_4 == DWord))
    movff CharactersIn,FSR0L ; \
    movff CharactersIn + 1,FSR0H ; / Point FSR1L/H to the address of the source string
    movlw String_Length ; Create a loop for all the characters in the string array part
    movf INDF1,F ; \
    bz $ + 10 ; / Exit if a NULL found
    movff POSTINC1,POSTINC0 ; Place the character from source string to dest string
    decfsz WREG,F ; Next character
    bra $ - 10 ; Until all characters transfered
    #endif
    #if((PRM_4 == NUM8) || (PRM_4 == NUM16) || (PRM_4 == NUM32))
    #if(CharactersIn == 0)
    clrf INDF0
    #else
    #if(CharactersIn == 255)
    setf INDF0
    #else
    movlw CharactersIn
    movwf INDF0
    #endif
    #endif
    #endif
    #if(PRM_4 == Byte)
    movff CharactersIn,INDF0
    #endif
    EndAsm-
    EndM
    '--------------------------------------------------------------------------------------
    ' Simulate a String array Dest_String = Source_String [Index1]
    ' The algorithm for accessing the elements is: -
    ' FSR0 = String_Name
    ' PROD = Index1 * String_Length
    ' FSR0 = FSR0 + PROD

    ' Syntax:
    ' Dest_String = Str_Array_In String_Name , String_Length , [Index1]

    ' Where: -
    ' Dest_String is the destination string to hold the string extracted from the array
    ' ARRAY_NAME is the name of the array in question
    ' String_Length is the individual string lengths within the array
    ' Index1 is the index value of the string array

    Str_Array_In Macro- String_Name, String_Length, Index_1

    #if(PRM_COUNT != 3)
    #error "Incorrect amount of parameters for Str_Array_In macro. 3 Parameters required"
    ExitM
    #endif
    #if(PRM_1 != String)
    #error "Parameter 1 must be a String for Str_Array_In macro"
    ExitM
    #endif
    Asm-
    #if((PRM_3 == NUM8) || (PRM_3 == NUM16) || (PRM_3 == NUM32))
    movlw Index_1
    #endif
    #if((PRM_3 == Byte) || (PRM_3 == Word) || (PRM_3 == DWord))
    #if(Index_1 <= BANKA_END)
    movf Index_1,w
    #else
    movff Index_1,WREG
    #endif
    #endif
    EndAsm-
    #if((PRM_2 != NUM8) && (PRM_2 != NUM16) && (PRM_2 != NUM32))
    #error "String LENGTH must be an unsigned constant value for Str_Array_In macro"
    #endif
    Asm-
    lfsr 1,String_Name ; Point FSR1L/H to the address of the string array
    mullw String_Length ; \
    movf PRODL,W ; |
    addwf FSR1L,F ; Calculate position of string array within main String
    movf PRODH,W ; |
    addwfc FSR1H,F ; /
    ; FSR1 is now pointing to the correct string within the array based on Index_1 and String_Length
    EndAsm-
    EndM
    '
    '-------------------------------------------------------------------
    ' The Main Program Loop Starts Here
    ' Test the macros by loading then retrieving values

    DelayMS 100 ' Wait for things to stabilise
    Clear ' Clear all RAM before we start

    HSerOut ["LOADING STRING ARRAY",13,13]
    For Index1 = 0 To Dimension_1 - 1 ' Create a loop for all the strings within the array
    TempString = "String " + Str$(DEC2 Index1)' Load a string with characters
    Str_Array_Out String1,String1_Length,[Index1], TempString ' Place the string into the string array
    HSerOut [TempString,13] ' Display the string loaded into the array
    Next

    DelayMS 600 ' Pause for drama

    HSerOut ["--------------------",13,13]
    HSerOut ["READING STRING ARRAY",13,13]
    For Index1 = 0 To Dimension_1 - 1 ' Create a loop for all the strings within the array
    TempString = Str_Array_In String1,String1_Length,[Index1] ' Retreive a string from the string array
    HSerOut [TempString,13] ' Display the results serially
    Next

    Stop
    contributed by Les Johnson.
  • Recent Activity

    teo-16018

    Microsoft security essentials blues

    Thread Starter: shantanu@india

    Hi, A strange thing happened just now.My Windows 7 Micrsoft Sercurity Essentials suddenly gave a warning that prplus.exe(Compiler) is a potential...

    teo Today, 19:23 Go to last post
    barak-1433

    SPI mssp

    Thread Starter: picprotonguy

    Just ordered Digital to Analog IC MCP4822 with SPI interface and immediately opened proton manual. I'm very disappointed, it looks like Proton...

    barak Today, 22:50 Go to last post
    David Barker-251

    UMC bootloader 18F46K22

    Thread Starter: shunt010

    Hi all. I am trying to build a UMC bootloader file for the 18F46K22, and it doesn't appear to work. It returns (in the error log) Error ...

    David Barker Today, 11:20 Go to last post