Writing Bare Metal C-Code for PIC

The following section focuses on how to write C-code for the PIC16 and PIC18 microcontroller families. The examples describe how to make the code highly readable and portable between different PIC16 and PIC18 devices. The examples can also be used as a guideline on how to write code that is easy to verify and maintain.

PIC registers are located in dedicated and continuous blocks in the memory space and can be seen as encapsulated units. This reflects on the way that the registers are accessed when coding in C. Registers are encapsulated using C unions, in which all the bits and bit fields are encapsulated using C structs. A register can be accessed as a whole using the macro declaration of the register or it can be accessed using the union declaration of the register. A bit field can be accessed similarly to the register.

This document introduces a naming convention and register access method that is compliant with the PIC header files. This provides readability and portability to the codes written in C-code.