How Can I Prevent Misuse of My Code?

First, many devices with flash program memory allow all or part of this memory to be write protected. The device Configuration bits need to be set correctly for this to take place; see Configuration Bit Access and your device data sheet.

Second, you can prevent third-party code being programmed at unused locations in the program memory by filling these locations with a value rather than leaving them in an unprogrammed state. You can choose a fill value that corresponds to an instruction or set all the bits so values cannot be further modified (consider what will happen if your program somehow reaches and starts executing from these filled values).

The compiler’s HEXMATE utility (see Hexmate) has the capability to fill unused locations and can be requested using a command-line driver option (see Fill Option). As HEXMATE only works with HEX files, this feature is only available when producing HEX/COF file outputs (as opposed to binary, for example), which is the default operation.

And last, if you wish to make your library files or intermediate p-code files available to others but do not want the original source code to be viewable, then you can obfuscate the files using the -mshroud option (see Shroud Option).