PDA

View Full Version : Global Constant



JonnyMac
07-15-2009, 10:45 PM
Is it possible to create a master constant that will trickle down through all objects under the "top object"? Specifically, I'd like to set the project frequency (_clkfreq) in the top object and have it apply to everything underneath that has a timing requirement (without editing the child objects).

Rayman
07-15-2009, 11:07 PM
I don't think so, at least not with the Prop Tool...

But, I suppose you could have an object with all your constants in it and just include that object in all your files...

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
My Prop Info&Apps: ·http://www.rayslogic.com/propeller/propeller.htm

Mike Green
07-15-2009, 11:48 PM
I've done what Rayman suggests. In BoeBotBasic, there's a "BB_definitions.spin" file that's included as an object in everything. It contains basic constant definitions for I/O pins, work areas. It also includes a couple of very small routines used in most I/O drivers. Only one copy of the compiled code is included in the program because of how the Propeller Tool combines identical code. This definitions file also includes the master definitions for _xinfreq and _clkmode.

RossH
07-16-2009, 05:54 AM
I also use the method Rayman mentions. But there is one more thing you have to do, and that's redeclare the constant in the program that uses it. This is because there are some places in SPIN (I forget exactly where at the moment - maybe in PASM code in a DAT block) that won't accept the object#value syntax.

For example:

Constants_File.spin said...

CON
My_Constant=1




Program_File.spin said...

OBJ
Constants: "Constants_File.spin"

CON
My_Constant= Constants#My_Constant

...



The once-off overhead of the few bytes required to import the constants file is usually worth it.

Ross.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Catalina - a FREE C compiler for the Propeller - see Catalina (http://forums.parallax.com/showthread.php?p=795326)

JonnyMac
07-16-2009, 06:15 AM
Thanks, Ross, that was it. I wasn't quite following Mike's suggestion but now it makes perfect sense.

Phil Pilgrim (PhiPi)
07-16-2009, 06:25 AM
Jon,

Is there some reason you need specific access to a user-defined _clkfreq instead of using the already-global clkfreq contained in long[0]?

-Phil