Macro Examples

Here are a couple of example macros which were built using the wizard.

 

Add two Words

Enhanced Alpha Clear Screen

 

Add Two Words

This first Macro example uses the GoSub example in the Proton Help File to add 2 words together.

 

' Add_Word (W1, W2), Result

' Will add W1 to W2 and return sum in Result.

 

Add_Word MACRO P1, P2

#IF (PRM_COUNT >  2)

       #ERROR "Add_Word - Too many parameters"

#ELSE

       #IF (PRM_COUNT <  2)

              #ERROR "Add_Word - Too few parameters"

       #ELSE

              #IF (PRM_1 != WORD) && (PRM_1 != NUM16) && (PRM_1 != NUM8) && (PRM_1 != NUM32)

                     #ERROR "Add_Word - W1(Param 1) should be a Word variable or number"

              #ENDIF

              #IF (PRM_2 != WORD) && (PRM_2 != NUM16) && (PRM_2 != NUM8) && (PRM_2 != NUM32)

                     #ERROR "Add_Word - W2(Param 2) should be a Word variable or number"

              #ENDIF

              #IF (PRM_1 == WORD)

                     WORD_WORD P1, ADD_WRD1

              #ENDIF

              #IF (PRM_1 == NUM16)

                     NUM_WORD P1, ADD_WRD1

              #ENDIF

              #IF (PRM_1 == NUM8)

                     NUM_WORD P1, ADD_WRD1

              #ENDIF

              #IF (PRM_1 == NUM32)

                     NUM_WORD P1, ADD_WRD1

              #ENDIF

              #IF (PRM_2 == WORD)

                     WORD_WORD P2, ADD_WRD2

              #ENDIF

              #IF (PRM_2 == NUM16)

                     NUM_WORD P2, ADD_WRD2

              #ENDIF

              #IF (PRM_2 == NUM8)

                     NUM_WORD P2, ADD_WRD2

              #ENDIF

              #IF (PRM_2 == NUM32)

                     NUM_WORD P2, ADD_WRD2

              #ENDIF

              GoSub ADD_THEM

       #ENDIF

       #IF (Add_Word_RETURN != 1)

              #ERROR "Add_Word -  Mandatory return parameter missing"

       #ELSE

              #IF (RETURN_TYPE != WORD)

                     #ERROR "Add_Word - Return variable should be a Word variable"

              #ENDIF

              #IF (RETURN_TYPE == WORD)

                     WORD_WORD ADD_WRD1, RETURN_VAR

              #ENDIF

       #ENDIF

#ENDIF

ENDM

 

All the above code was produced by the Macro generator.  It has placed the first argument into ADD_WRD1 and the second argument into ADD_WRD2, checked the data types were compatible and ensured there is a return parameter.

 

Note the error messages the code generates – if an error occurs during assembly these will be reported back in the IDE.

 

All you need to do now is to write the underlying function that the macro has to perform.

 

Dim ADD_WRD1 as WORD     ' Create two uniquely named variables
Dim ADD_WRD2 as WORD

ADD_THEM:  

ADD_WRD1 = ADD_WRD1 + ADD_WRD2   ' Add the values together
Return

 

Note the use of GoSub to call the function – this is necessary because there is a return value.

Enhanced Alpha Clear Screen

This example shows the use of optional Arguments. 

 

For users still using an alphanumeric LCD this provides a useful enhanced alternative to the standard Proton CLS command.  When invoked with no parameters it will perform a standard Proton CLS, however, if you add a line number argument it will clear just that line, if you add a second argument (Position) it will clear from that position to the end of the line leaving the cursor at the beginning of the clear.

 

' CLL {Line, {Pos}} Command

     With no arguments same as proton CLS

'      With 1 argument (Line), clears line specified in argument

'      With 2 arguments (Line, Pos), clears to end of Line from Position

 

CLL MACRO P1, P2

#IF (PRM_COUNT >  2)

       #ERROR "CLL - Too many parameters"

#ELSE

       #IF (PRM_COUNT == 0)

'      Add additional code for no params here .......

              CLS    ' Here is the additional code added after the generator has produced the outline

       #ELSE

              #IF (PRM_1 != BYTE) && (PRM_1 != NUM8) && (PRM_1 != NUM16) && (PRM_1 != NUM32)

                     #ERROR "CLL - Line(Param 1) should be a Byte variable or number"

              #ENDIF

              #IF (PRM_2 != BYTE) && (PRM_2 != NUM8) && (PRM_2 != NUM16) && (PRM_2 != NUM32)

                     #ERROR "CLL - Pos(Param 2) should be a Byte variable or number"

              #ENDIF

              #IF (PRM_1 == BYTE)

                     BYTE_BYTE P1, LCD_IntB

              #ENDIF

              #IF (PRM_1 == NUM8)

                     NUM_BYTE P1, LCD_IntB

              #ENDIF

              #IF (PRM_1 == NUM16)

                     NUM_BYTE P1, LCD_IntB

              #ENDIF

              #IF (PRM_1 == NUM32)

                     NUM_BYTE P1, LCD_IntB

              #ENDIF

              #IF (PRM_2 == BYTE)

                     BYTE_BYTE P2, LCD_IntA

              #ENDIF

              #IF (PRM_2 == NUM8)

                     NUM_BYTE P2, LCD_IntA

              #ENDIF

              #IF (PRM_2 == NUM16)

                     NUM_BYTE P2, LCD_IntA

              #ENDIF

              #IF (PRM_2 == NUM32)

                     NUM_BYTE P2, LCD_IntA

              #ENDIF

              GoSub LCD_CLL

       #ENDIF

#ENDIF

ENDM

 

Dim LCD_IntB As Byte

Dim LCD_IntA As Byte

 

LCD_CLL:

    Print At LCD_IntB, LCD_IntA, Rep " "\LCD_NoChars - LCD_IntA + 1

    Cursor LCD_IntB, LCD_IntA

Return

 

Note – This macro requires a constant LCD_NoChars to be defined to reflect your hardware configuration.  E.g. Symbol LCD_NoChars = 16 for a 16 character display.