-
Notifications
You must be signed in to change notification settings - Fork 3
Coding projects to be compatible between PIC18F452 and PIC18F4520
You may want to make your projects compatible between both the minimal PIC18F4520 board and the PICDEM 2 PLUS (PIC18F452) board, so that you don't have to make two different files for all your code (or alternatively, so that you don't have to test your code exclusively on one board type and not the other). Of course, sometimes this can prove to be quite an effort anyway, especially if you are using different ports and pins (depending on which board you are using) in the same code section.
Remember that there is a migration guide pdf made by Microchip which is up on the MTRX3700 website. The guide details the differences between the two chips.
The compiler automatically defines certain symbols based on which microprocessor you have selected in your project settings in MPLAB. To make use of this, simply add these precompiler directives:
#if defined(__18F452)
// Do stuff specific to the PICDEM board (more specifically, the PIC18F452 chip)
#elif defined(__18F4520)
// Do stuff specific to the minimal board (more specifically, the PIC18F4520 chip)
#endif
#if defined(__18F452)
#include <p18f452.h>
#elif defined(__18F4520)
#include <p18f4520.h>
#else
#error Unknown processor!
#endif
There is actually already a header file which does exactly the code example above does (which I found by going through the C18 library code and looking at "xlcd.h"). That file is "p18cxxx.h", so all you need to do to include your processor specific header file is:
#include "p18cxxx.h"
The possibilities are endless in what you can do. For example, the LCD pins that the PICDEM board uses might be very different to the ones that you end up using on the minimal board. In that case, you might want to do something like this:
#if defined(__18F452)
/* DATA_PORT defines the port to which the LCD data lines are connected */
#define DATA_PORT PORTD
#define TRIS_DATA_PORT TRISD
#elif defined(__18F4520)
/* DATA_PORT defines the port to which the LCD data lines are connected */
#define DATA_PORT PORTB
#define TRIS_DATA_PORT TRISB
#endif
so that you've only branched using the precompiler directive at one point in your code, rather than at every single line where you need to access the LCD pins.
Remember that you will still need to manually configure your project settings to match which board you are using every time you switch.