PIC18 Family

Overview

PIC18 is a family of microcontrollers from Microchip. This includes the PIC18C and PIC18F sub-families.

Marketing render of a range of microcontrollers from the Microchip PIC18 family. Image from http://ww1.microchip.com/.
Marketing render of a range of microcontrollers from the Microchip PIC18 family. Image from http://ww1.microchip.com/.

Compilers

The newest compiler for the PIC18 family of microcontrollers (as of July 2016) is the Microchip XC8 compiler. More information an be found on the MPLAB XC Compilers page.

EEPROM

The old-style (now deprecated) way of reading and writing to EEPROM was to use the following macros (defined in  pic18.h):

Note that these are now depreciated and if using MPLAB X, these macros get turned into empty shells which don’t actually do anything (well, unless #if _EEPROMSIZE > 0 && defined(_PLIB) is true, which it is not by default). and they don’t raise any compiler warnings/errors, meaning your EEPROM may not work and you don’t know about it!).

What you can do is forgo the nice API and use direct manipulation instead. I have written the following two functions which read/write to EEPROM using register manipulation, and provide similar functionality to what the deprecated API did:

Writing Default Data To EEPROM On Program

Default data can be written to the EEPROM when programming the PIC18 microcontroller by using the __EEPROM_DATA() macro.

This macro takes 8 single-bye arguments, which writes 8 bytes of EEPROM data at once. This macro starts writing from address 0x0000 and auto-increments by  0x8 on every call.

GPIO

The TRISx registers (where x  is the port letter, e.g. TRISA , TRISB) control the direction of the GPIO pins on that port. They can either be set as inputs or outputs.

SPI

The SPI peripheral is controlled by the SSPCON and  SSPSTAT registers. Note that these registers are also used to configure the I2C peripheral (the SPI and I2C cannot peripherals cannot be used at the same time).

Data is read/written to the SPI bus using the  SSPBUF register.

Posted: June 20th, 2016 at 11:46 am
Last Updated on: July 21st, 2016 at 5:14 pm