It is very usefull to be able to define constants to direct the way the compiler generates code.A mechanism to allow a flag to be assigned one of several given values. The value of the flag is identified using a symbolic name. The flag can be used just like any other manifest integer constant. The range of values the flag can be assigned is well defined. Only flags defined within the same module may be used. If a flag is undefined compilation stops.
A flag is defined using the "pragma build_config_option" statement.
The Syntax is
pragma build_config_option group_id flag_idwhere group_id is a constant in the range 0 to 255 and flag_id is the symbolic name of the flag. The flag_id may not be defined elsewhere, it is recognised as a manifest constant after its use in the "pragma build_config_option" statement. No two flags may have the same group_id.e.g. pragma build_config_option 1 BLD_OPT1 pragma build_config_option 2 BLD_OPT2 pragma build_config_option 3 BLD_OPT3 // the above defines flags BLD_OPT1, BLD_OPT2 and BLD_OPT3Possible values that can be assigned to the flags are defined as manifest constants of the form:const flag_value = 0xMMGGnnnnwhere MM is the module_id (in the range 0 to 255), GG is the group_id (in the range 0 to 255) and nnnn is the flag value (in the range 0 to 65535)e.g. const BLD_OPT1_1 = 0x00010001 const BLD_OPT1_2 = 0x00010002 const BLD_OPT1_3 = 0x00010003 const BLD_OPT2_1 = 0x00020011 const BLD_OPT2_2 = 0x00020012 const BLD_OPT2_3 = 0x00020013 const BLD_OPT3_1 = 0x00030021 const BLD_OPT3_2 = 0x00030022 const BLD_OPT3_3 = 0x00030023 // the above shows that BLD_OPT1_1, BLD_OPT1_2 and BLD_OPT1_3 // all belong to flag group 1 (because they are defined as 0xMM01nnnn) // the above shows that BLD_OPT2_1, BLD_OPT2_2 and BLD_OPT2_3 // all belong to flag group 2 (because they are defined as 0xMM02nnnn) // the above shows that BLD_OPT3_1, BLD_OPT3_2 and BLD_OPT3_3 // all belong to flag group 3 (because they are defined as 0xMM03nnnn)flags are set a specific value using the "pragma build_config" statement The Syntax ispragma build_config flag_value // where flag value is defined as a manifest constant of the // form (const flag_value = 0xMMGGnnnn)e.g pragma build_config BLD_OPT1_2 pragma build_config BLD_OPT2_3 pragma build_config BLD_OPT3_1 // the above assigns: // the value 0x0002 to flag BLD_OPT1 // the value 0x0013 to flag BLD_OPT2 // the value 0x0021 to flag BLD_OPT3why not simply have an enumerated type and assign that to the flag?e.g. enum BLD_OPT1_type = BLD_OPT1_1, BLD_OPT1_2, BLD_OPT1_3 BLD_OPT1_type BLD_OPT1 = BLD_OPT1_2Because the "pragma build_config_option" and "pragma build_config" statment combination allows values belonging to the same flag to be defined in different files, and allows groups of values to be defined as belonging to different modules.if we define a set of flag values for the 16f628 and a different set of flag values with the same names for the 16f876 we want to be sure that we only use either the 16f628 set or the 16f876 set. All flags set in one program must belong to the same module. We want the compiler to complain if we try to set a flag for the 16f628 with a value specific to the 16f876. enumerated types do not provide this facility.
This section would be held in a header file that is included by all programs. It defines all flags that should be set pragma build_config_option 1 BLD_OPT1 pragma build_config_option 2 BLD_OPT2 pragma build_config_option 3 BLD_OPT3 This section would be held in a header file that is specific to a particular processor (or build). There would be many such header files each with their own copies of some or all of these flag values. The important point is that the module ID for all the flag values in any given header file would be the same and the module ID for each header file would be different. const BLD_OPT1_1 = 0x00010001 const BLD_OPT1_2 = 0x00010002 const BLD_OPT1_3 = 0x00010003 const BLD_OPT2_1 = 0x00020011 const BLD_OPT2_2 = 0x00020012 const BLD_OPT2_3 = 0x00020013 const BLD_OPT3_1 = 0x00030021 const BLD_OPT3_2 = 0x00030022 const BLD_OPT3_3 = 0x00030023 This section would be in the source file. It selects which value to assign to a particular flag pragma build_config BLD_OPT1_2 pragma build_config BLD_OPT2_3 pragma build_config BLD_OPT3_1 Later in the program or embedded within a library function (XCSB or assembler) the option would be checked as if BLD_OPT1 == (BLD_OPT1_1 & 0xffff) then // pragma build_config BLD_OPT1_1 was specified else if BLD_OPT1 == (BLD_OPT1_2 & 0xffff) then // pragma build_config BLD_OPT1_2 was specified else if BLD_OPT1 == (BLD_OPT1_3 & 0xffff) then // pragma build_config BLD_OPT1_3 was specified endif