News:

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

Main Menu

Fill in identical values with $define.

Started by atomix, Jul 20, 2022, 04:42 PM

Previous topic - Next topic

atomix

I decided to stretch my brains and see if this can be done using the preprocessor.

Les made a really powerful preprocessor.

Device 18F46K22
Declare Xtal = 64

'----------------------------------------------------------------------------
$define RPT_Value(count,value)      _  RPT_Pre(count,value)    RPT_Init    RPT_Loop    RPT_Out
$define RPT_Init                    $define RPT_Loop  RPT_Inc  RPT_Check  RPT_Loop
$define RPT_Pre(count,value)        $defeval RPT_End count      $defeval RPT_Val value      $defeval RPT_Cnt $eval 0      $undef RPT_Out
$define RPT_Inc                    $defeval RPT_Cnt $eval RPT_Cnt+1    $define RPT_Count(P1) $eval P1 RPT_Cnt
$define RPT_Check                  $if RPT_Cnt <= RPT_End                      '
                                        $ifdef RPT_Out                          '
                                            $defeval RPT_Out RPT_Out, RPT_Val  '
                                        $else                                  '
                                            $defeval RPT_Out RPT_Val            '
                                        $endif                                  '
                                    $else                                      '
                                        $define RPT_Loop                        '
                                        $undef RPT_Count                        '
                                    $endif
'----------------------------------------------------------------------------

PRODH = ERead MyData1 0
PRODH = ERead MyData2 0

MyData1 EData RPT_Value( 5, "work")
MyData2 EData RPT_Value(33, 0)

End

MyCData1: CData RPT_Value(7, "work")
MyCData2: CData RPT_Value(4, RPT_Count)
MyCData3: CData RPT_Value(4, RPT_Count(5-))
MyCData4: CData RPT_Value(4, RPT_Count(-1+))
MyCData5: CData RPT_Value(4, RPT_Count(50+))
MyCData6: CData RPT_Value(4, RPT_Count(~))

top204

I'm glad you like it.

It follows, as closely as I could, a standard C and C++ preprocessor because that is what I am used to. I could not use the "#" character for the preprocessor because that is what the assembler also uses for its preprocessor and I brought the assembler's preprocessor into the language when I first wrote the compiler, for more advanced users. So I opted for the $ characters for it.

Looking at preprocessor code when it is created in a program can be confusing, but when used in the program itself, it can make things a lot easier. I use them for peripherals inside include files, so a program will have something like "Timer1_Prescaler(cPrecale_1_2)", and the preprocessor's code will alter the timer SFR inline and not have to call a procedure or subroutine.








atomix

I updated an example in the first post, now it can also generate numbers with different settings.