One Program, Any BS2-family Module (Conditional Compilation)
Jon Williams
Posts: 6,491
I frequently get PM'd asking me·how to move a program from one BS2 module to another and my answer is always the same: Start with Conditional Compilation (C/C).· C/C is a process used by the BASIC Stamp Editor to conditionally compile sections of code based on the value of a given symbol.· One of the ways we can use C/C is to adjust for the BASIC Stamp we're about to download to.· Before compiling and downloading, the Stamp Editor checks the module we're actually connected to in order to verify it matches our $STAMP setting, if not, we're given the opportunity to change the target of abort the download.
The important thing to understand about C/C is that only the portions of code that match the target symbol·get compiled and downloaded.· When developing a program we often insert DEBUG statements to check progress.· When everything is working we rip them out.· With C/C we can "rip them out" without actually removing any code -- this lets us put them back just as easily.·
To define a manual C/C symbol we do something like this:
#DEFINE DebugMode = 1··· ' set to 0 to disable
... and later in our program we would do this:
· #IF DebugMode #THEN
··· DEBUG "The value of idx is", DEC idx, CR
· #ENDIF
Note the # symbol that indicates C/C instructions.
Now, how do we use this in code so that we can change Stamp modules easily?· We can use the $STAMP symbol in C/C structures.· Here's an example that traps the program if the correct commands are not available on the given module:
· #IF ($STAMP < BS2P) #THEN
··· #ERROR "BS2p or later required for this program."
· #ENDIF
In this case the #ERROR·command raises an alert dialog in the editor to let the user know that the current program uses BS2p-family statements and the program can't be run on the current module.
Many commands, especially serial baudmode values, simply require adjustment based on the module.· Since this is the area where users seem to have the most trouble going module-to-module, I've built the following C/C structure into my default template.
#SELECT $STAMP
· #CASE BS2, BS2E, BS2PE
··· T1200······ CON···· 813
··· T2400······ CON···· 396
··· T4800······ CON···· 188
··· T9600······ CON···· 84
··· T19K2······ CON···· 32
··· TMidi······ CON···· 12
··· T38K4······ CON···· 6
· #CASE BS2SX, BS2P
··· T1200······ CON···· 2063
··· T2400······ CON···· 1021
··· T4800······ CON···· 500
··· T9600······ CON···· 240
··· T19K2······ CON···· 110
··· TMidi······ CON···· 60
··· T38K4······ CON···· 45
· #CASE BS2PX
··· T1200······ CON···· 3313
··· T2400······ CON···· 1646
··· T4800······ CON···· 813
··· T9600······ CON···· 396
··· T19K2······ CON···· 188
··· TMidi······ CON···· 108
··· T38K4······ CON···· 84
#ENDSELECT
In this case C/C is used to set the values of a bunch of constants that are useful for serial communications.· SERIN/SEROUT are not the only areas that benefit from C/C; these instructions differ from module-to-module:
· COUNT
· DTMFOUT
· FREQOUT
· GET
· PULSIN
· PULSOUT
· PUT
· PWM
· RCTIME
· SERIN
· SEROUT
·
You'll find examples in the manual and help file the demonstrate the use of C/C to accommodate the module-to-module differences when using these instructions.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Post Edited (Jon Williams (Parallax)) : 2/9/2006 5:11:38 PM GMT
The important thing to understand about C/C is that only the portions of code that match the target symbol·get compiled and downloaded.· When developing a program we often insert DEBUG statements to check progress.· When everything is working we rip them out.· With C/C we can "rip them out" without actually removing any code -- this lets us put them back just as easily.·
To define a manual C/C symbol we do something like this:
#DEFINE DebugMode = 1··· ' set to 0 to disable
... and later in our program we would do this:
· #IF DebugMode #THEN
··· DEBUG "The value of idx is", DEC idx, CR
· #ENDIF
Note the # symbol that indicates C/C instructions.
Now, how do we use this in code so that we can change Stamp modules easily?· We can use the $STAMP symbol in C/C structures.· Here's an example that traps the program if the correct commands are not available on the given module:
· #IF ($STAMP < BS2P) #THEN
··· #ERROR "BS2p or later required for this program."
· #ENDIF
In this case the #ERROR·command raises an alert dialog in the editor to let the user know that the current program uses BS2p-family statements and the program can't be run on the current module.
Many commands, especially serial baudmode values, simply require adjustment based on the module.· Since this is the area where users seem to have the most trouble going module-to-module, I've built the following C/C structure into my default template.
#SELECT $STAMP
· #CASE BS2, BS2E, BS2PE
··· T1200······ CON···· 813
··· T2400······ CON···· 396
··· T4800······ CON···· 188
··· T9600······ CON···· 84
··· T19K2······ CON···· 32
··· TMidi······ CON···· 12
··· T38K4······ CON···· 6
· #CASE BS2SX, BS2P
··· T1200······ CON···· 2063
··· T2400······ CON···· 1021
··· T4800······ CON···· 500
··· T9600······ CON···· 240
··· T19K2······ CON···· 110
··· TMidi······ CON···· 60
··· T38K4······ CON···· 45
· #CASE BS2PX
··· T1200······ CON···· 3313
··· T2400······ CON···· 1646
··· T4800······ CON···· 813
··· T9600······ CON···· 396
··· T19K2······ CON···· 188
··· TMidi······ CON···· 108
··· T38K4······ CON···· 84
#ENDSELECT
In this case C/C is used to set the values of a bunch of constants that are useful for serial communications.· SERIN/SEROUT are not the only areas that benefit from C/C; these instructions differ from module-to-module:
· COUNT
· DTMFOUT
· FREQOUT
· GET
· PULSIN
· PULSOUT
· PUT
· PWM
· RCTIME
· SERIN
· SEROUT
·
You'll find examples in the manual and help file the demonstrate the use of C/C to accommodate the module-to-module differences when using these instructions.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Post Edited (Jon Williams (Parallax)) : 2/9/2006 5:11:38 PM GMT
Comments
Please for the experienced programmers that are trying to teach young people about programming PLEASE include COMMENTS to codes.
It seem to be hard, but it would make life easier!
Come on its, not like any person can use stamps,· SO Keep give out the money, I bet that you could post better replies if you tried harder to sell the product?????
All the best.
Post Edited (davids) : 4/22/2006 12:51:31 PM GMT
I have no idea what you mean about giving out the money.
Remember that we have an entire "Stamps In Class" series that you can use for teaching young people.· We've had a lot of success with it and I'm sure you can too.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
········ I think this is cheap shot on Jon. I have used a lot of his code in many applications
and I found it very easy to use in all cases.
·J. H. Rollins.