This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Is there some way to determine which processor is being compiled to?

I have several pieces of source which are compiled for a generic 80C31 or a Dallas 80C310. These processors require a different include files for the sfrs. Rather than changing the source code each time I change projects, it would be nice to use an ifdef.

  • so, do that
    #ifdef ...
    #include ...
    #else
    #include ...
    #endif

  • I guess I could have asked the question better. Is there a predefined macro for this purpose?

  • If you create two separate targets you'll be able to set up the defined symbols in the target options and thereby avoid actually having to change any code when you build for the different targets.

    Go to "Targets, groups, files" on the projects menu.

  • As long as you're changing the project file, you can just change the list of source files. You could have a "cpu.h" file that encapsulates all of the relevant differences, and just use a different one in each target. No #define needed.

    Of course, scattering "#if defined()" all over the code will work, too. It's been done that way for decades.

    With the #if strategy, I'd suggest making sure always to include an else clause with a warning to catch errors in defined the CPU type. Something like:

    #if defined(CpuTypeA)
    #elif defined(CpuTypeB)
    #elif defined(CpuTypeC)
    #else
    #error "No known CPU type defined."
    #endif

    If you just leave type C for the #else, any absent #define, redefinition, or typo drops you into the wrong code, which can sometimes create some pretty obscure bugs.

  • As long as you're changing the project file, you can just change the list of source files.

    No, that won't help at all. Because .h files are not source files in this sense. They're never compiled by themselves, so they don't have to appear in the project file lists at all.

    If you want to switch among #included files based on which target is being built, you have to inform the compiler about the target selection in some way that the preprocessor recognizes. That means it can bascially only be a #define, or a change in the include paths. Nothing else can possibly work.

    The only useful improvement possible in this area is to collect as many of those #ifs as you can in a single, central "portability header", so all other files just have to include that header to resolve the majority of the platform specifics.

  • "The only useful improvement possible in this area..."

    Actually there is a really useful improvement that Keil could make here:
    They could make all the project settings available to the source code via Predefined Macros.

    Currently, only the Memory Model is available - via __MODEL__
    So why not the others; eg, __XTAL__, __DEVICE__, __TARGET__, etc, etc.