Vous êtes sur la page 1sur 5

Microprocessor Interfacing Fall 2004 jeg

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:

#define LOW_BYTE(x) ((unsigned char)(x)&0xFF)


#define HIGH_BYTE(x) ((unsigned char)(x>>8)&0xFF)
#define LOW_WORD(x) ((unsigned int)(x)&0xFFFF)
#define HIGH_WORD(x) ((unsigned int)(x>>16)&0xFFFF)i

The next set of macros allows one to insert selective assembly code instructions in your C
program. These are defined as:

#defineCLRWDT( ) asm(" clrwdt")


#defineNOP( ) asm(" nop")
#defineRESET( ) asm(" reset")
#define SLEEP( ) asm(" sleep)ii

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))

#define __IDLOC(w) asm("\tpsect idloc,class=IDLOC");\


asm("\tglobal\tidloc_word"); \
asm("idloc_word:"); \
asm("\tirpc\t__arg," ___mkstr(w)); \
asm("\tdb 0f&__arg&h"); \
asm("\tendm")iii

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:

#if EEPROM_SIZE > 256


#defineEEPROM_READ(addr) \
(EEADRH=(((addr)>>8)&0xFF),EEADR=((addr)&0xFF), \

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

#if EEPROM_SIZE > 256


#defineEEPROM_WRITE(addr, value) \
wait_on_wr(); \
EEADRH=(((addr)>>8)&0xFF);EEADR=((addr)&0xFF); \
EEDATA=(value); \
EECON1&=0x3F; \
CARRY=0;if(GIE)CARRY=1;GIE=0; \
WREN=1;EECON2=0x55;EECON2=0xAA;WR=1; \
EEIF=0;WREN=0; \
if(CARRY)GIE=1
#else
#defineEEPROM_WRITE(addr, value) \
wait_on_wr(); \
EEADR=((addr)&0xFF); \
EEDATA=(value); \
EECON1&=0x3F; \
CARRY=0;if(GIE)CARRY=1;GIE=0; \
WREN=1;EECON2=0x55;EECON2=0xAA;WR=1; \
EEIF=0;WREN=0; \
if(CARRY)GIE=1
#endif

2
Microprocessor Interfacing Fall 2004 jeg

/* macro Flash operations*/


/* erasing a flash program memory row*/
#if defined(SMALL_DATA)
#define FLASH_ERASE(addr) \
TBLPTRU=0;\
TBLPTR=(far unsigned char *)addr; \
EECON1|=0x94;EECON1&=0xBF; \
CARRY=0;if(GIE)CARRY=1;GIE=0;\
EECON2=0x55;EECON2=0xAA;WR=1; \
asm("\tNOP"); \
if(CARRY)GIE=1
#else
#define FLASH_ERASE(addr) \
TBLPTR=(far unsigned char *)addr; \
EECON1|=0x94;EECON1&=0xBF; \
CARRY=0;if(GIE)CARRY=1;GIE=0;\
EECON2=0x55;EECON2=0xAA;WR=1; \
asm("\tNOP"); \
if(CARRY)GIE=1
#endifiv

The functions for reading and writing to the EEPROM and Flash program memory are
declared as:

/* read/write EEPROM data memory */


extern unsigned char eeprom_read(unsigned int address);
extern void eeprom_write(unsigned int address,unsigned char data);

/* read/write/erase sections of program memory */


extern unsigned char flash_read(unsigned long addr);
extern void flash_write(far unsigned char *,unsigned char,far unsigned char *);
extern void flash_erase(unsigned long addr);

/* read/write device configuration registers */


extern unsigned int config_read(unsigned char reg_no);
extern void config_write(unsigned char reg_no, unsigned int dataword);

/* read factory-programmed device ID code */


extern unsigned int device_id_read(void);

/* read/write to user ID regs */


extern unsigned char idloc_read(unsigned char reg_no);
extern void idloc_write(unsigned char reg_no,unsigned char data);

/* general purpose EE/flash init sequence used by above functions */

3
Microprocessor Interfacing Fall 2004 jeg

extern void initiate_write(void);


extern void wait_on_wr(void);v

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:

/* Accurate read/write macros for 16-Bit timers */


/*** please note, the timer needs to be enabled ***
*** to handle 16-Bit read/write operations for ***
*** these routines to be of benefit ***/
#define T1RD16ON T1CON|=0x80
#define T3RD16ON T3CON|=0x80
#define WRITETIMER0(x) TMR0H=(x>>8);TMR0L=(x&0xFF)
#define WRITETIMER1(x) TMR1H=(x>>8);TMR1L=(x&0xFF)
#define WRITETIMER3(x) TMR3H=(x>>8);TMR3L=(x&0xFF)
#define READTIMER0 (TMR0)
#define READTIMER1 (TMR1)
#define READTIMER3 (TMR3)vi

The last set of macros is used to enable and disable interrupt for critical sections of code.
These macro are defined as:

/* Global Interrupt Enable */


#ifndef ei
#defineei( ) ((GIE = 1),(GIEL = 1)) // Interrupts of Hi/Lo Priority or
Peripheral interrupts
#endif ei

/* Global Interrupt Disable */


#ifndef di
#definedi( ) ((GIE = 0),(GIEL = 0)) // Interrupts of Hi/Lo Priority or
Peripheral interrupts
#endif divii

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.

#if defined(_18F242) || defined(_18F252) || defined(_18F442) || defined(_18F452)


#include <pic18fxx2.h>
#endifviii

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

Vous aimerez peut-être aussi