Académique Documents
Professionnel Documents
Culture Documents
Goodies in PIC18.H
The all kinds of useful macros defined in PIC18.H .The first set of macros allows one to
access bytes within a word and a word within a long. The macros are defined as:
The next set of macros allows one to insert selective assembly code instructions in your C
program. These are defined as:
There is a group of macro for defining the ID location and the configuration words. Both
CONFIG and IDLOC start with a double underscore. Also note when used the macro
should be followed with a semicolon. This not what the PIC-18 C User’s Guide tells you.
These macros are defined as:
#define___mkstr1(x) #x
#define___mkstr(x) ___mkstr1(x)
#define__CONFIG(n, x) asm("\tpsect config,class=CONFIG");\
asm("global config_word" ___mkstr(n)); \
asm("config_word" ___mkstr(n)":"); \
asm("\torg ("___mkstr(n)"-1)*2"); \
asm("\tdw "___mkstr(x))
There is a group of macros for reading and writing the EEPROM and the program Flash.
Note there are also function calls, which perform the same action. The difference is that
the macros are defined with all upper-case letters and the functions are declared with all
lower-case letters. The macros are defined as:
1
Microprocessor Interfacing Fall 2004 jeg
wait_on_wr(), \
CARRY=GIE,GIE=0, \
EECON1&=0x3F,RD=1, \
(EEDATA)); \
if(CARRY)GIE=1
#elif defined(_18F242) || defined(_18F252) || defined(_18F442) ||
defined(_18F452) || \
defined(_18F4320) || defined(_18F1220) || defined(_18F1320)
#define EEPROM_READ(addr) \
(EEADR=(addr),\
wait_on_wr(), \
CARRY=GIE,GIE=0,\
EECON1&=0x3F,RD=1, \
EEADR=EEDATA,(EEADR)); \
if(CARRY)GIE=1
#else
#define EEPROM_READ(addr) \
(EEADR=(addr),\
wait_on_wr(), \
CARRY=GIE,GIE=0,\
EECON1&=0x3F,RD=1, \
(EEDATA)); \
if(CARRY)GIE=1
#endif
2
Microprocessor Interfacing Fall 2004 jeg
The functions for reading and writing to the EEPROM and Flash program memory are
declared as:
3
Microprocessor Interfacing Fall 2004 jeg
One must remember to read and write Timer 0, Timer 1, and Timer 3 in the 16-bit mode
that the high and low bytes must be accessed in the proper byte order. The following
macro will perform these actions:
The last set of macros is used to enable and disable interrupt for critical sections of code.
These macro are defined as:
The rest of the header file is dedicated to including the processor specific header file
based on the processor specified in MPLAB IDE. As an example if the PIC18F452 is
selected, the MPLAB IDE calls the compiler with _18FXX2 defined. For the following
preprocessor statements we can see that the header file PIC18FXX2.H will then be
included.
i
Copyright HI-TECH Software Incorporated 2004
4
Microprocessor Interfacing Fall 2004 jeg
ii
Copyright HI-TECH Software Incorporated 2004
iii
Copyright HI-TECH Software Incorporated 2004
iv
Copyright HI-TECH Software Incorporated 2004
v
Copyright HI-TECH Software Incorporated 2004
vi
Copyright HI-TECH Software Incorporated 2004
vii
Copyright HI-TECH Software Incorporated 2004
viii
Copyright HI-TECH Software Incorporated 2004