As I mentioned, I have just beginned programming pic16f877a. I can now work with 7 segment displays. At present I am using ccs compiler. Nothing wrong with that. But i prefer to be a compiler independent programmer. So i simultaneously want to work in other compilers like IAR or Hitechc. I want to know whether the "program statement declaration in compilers" other than ccs will be different? Please guide me how to approach this thing. I would welcome all forms of suggestions. Thanks in advance.
That great that you want to be compiler independent! Unfortunately hitech and CCS compilers for the low end PICs use a lot of compiler specific preprocessor declarations, compiler specific pin access routines, and in the case of CCS compiler specific routines for access core functions such as SPI, I2C, ADC and so on.
It is not possible to write your code to be non-compiler specific without a lot of preprocessor #define, #ifdef, #ifndef and so on to get access to specific parts of what each compiler has on offer. This would make your code unreadable.
The best thing you could aim for is to be IDE independent and use something like eclipse, so at least you are using the same IDE. This will result in losing CCS wizards for setting up core functions, but will give you greater flexibility in using the same IDE.
Another thing to consider is that both hitech and CCS do not have (at least in the past) a true c compiler linker and required you to use "#include myfile.c" which I personally despise ... but that is another story.
I have not commented on the IAR compiler as I have only used CCS and hitech. Both worked ok, but I was never really happy with either after migrating across from the Motorola (now freescale) platform and using the metroworks compiler which was more advanced at the time. The IAR compiler looks good but I have never used it.
If you were using PIC18 parts I would recommend C18 compiler from Microchip. It conforms much more closely to ANSI C than the CCS compiler. I'm not sure about the Hi-Tech compiler since I have not used it. As was said earlier if you really need to make compiler independent code you will need to use lots of pre-compiler directives. I'd recommend taking a look at some of the Microchip example programs that support multiple compilers to get an idea of how it is done.
Unfortunately you will find it is very difficult to find a compiler independent program for a microcontroller. There are several problems, here are just two:
Differences in peripherals, SFR naming, etc (especially over other processors, but even with compilers from the same family), and;
Non-standard features on some compilers like setting bits individually or different structures for invoking assembly code.
The 16F series is very limited in terms of architecture and not really designed to support a C compiler. That is why there is no GCC for it.
Take a look at SDCC. It supports many of the PIC16 and PIC18 devices. GCC supports PIC24 and dsPIC.
The most likely aspects to be compiler-dependent are:
- using single bits (especially in IO ports)
- sizes of integers, and char being signed or unsigned
- funny pointers: C18 differentiates rom and ram pointers :(
- configuration fuses
- busy waiting
My preferred way to handle this is to write macros for these aspects, and have the compiler select the correct macro based on compiler-specific predefined macros. I made an RFM70 library and example applications this way that run on PIC14 (HiTechC), PIC16 (C18) and ARM (GCC).
(update) My RFM70 library is now complete. It supports C on PIC 16F (Hitech compiler), C and C++ on LPC11114 (Cortex) and LPC2148 (ARM7TDMI) (GCC compiler) and Arduino (ATMega128, GCC compiler). This is generated (including doxygen documentation) from the same source by doing some preprocessing in a Python script. Jal support is under development, maybe ProtonBasic will follow. http://www.voti.nl/rfm70