PIC Development

Tested on FreeBSD 13.0

Prerequisites

sdcc is a C compiler used for microprocessors. It states that PIC microprocessors are unmaintained, but I've found it to be pretty reliable so far (take this with a grain of salt, I'm no expert). The port can be found under:

lang/sdcc

Page 75 of the sdcc user manual lists the supported PIC devices. Header files can be found under /usr/local/share/sdcc

For programming the MCU, I've found pk2cmd to work well with the Microchip PICKit2 programmer (or Chinese clones), but there's no port for FreeBSD anymore. The Makefile won't install files properly, so we have some extra work to do afterwards:

$ git clone https://github.com/psmay/pk2cmd.git
$ cd pk2cmd/pk2cmd
# gmake freebsd install clean
# mv /usr/share/pk2/PK2DeviceFile.dat /usr/local/bin
# rm -rf /usr/share/pk2

Supported devices for pk2cmd are listed here.

Detecting and programming the MCU

/!\ AUTO-DETECT WARNING: Always select the device family BEFORE connecting the chip to limit the maximum VPP that the PICkit 2 applies to the chip. Otherwise you risk damaging the chip and spending an inordinate amount of time trying to resolve issues with what is now a damaged chip.

To avoid damage to chips by VPP voltage levels above that rated for the chip the recommended method for auto detection is:

The better option is to avoid auto-detect altogether and specify the chip part number (eg PIC16F877A).

Assuming you've wired the MCU and the programmer correctly, hook up the programmer to your computer and check the chip is blank. If not erase the chip.

If any of the following pk2cmd commands fail, make sure everything really is wired properly:

$ pk2cmd -P PIC16F877A -C
Device is blank
Operation Succeeded

Compile your source code. The target executable is the .hex file sdcc will output. Replace pic14 and 16f877a with the appropriate names for your device:

$ sdcc --use-non-free -mpic14 -p16f877a main.c

Erase the PIC (if the blank check failed) and flash the new code. Again, use the appropriate names:

$ pk2cmd -P PIC16F877A -E
$ pk2cmd -P PIC16F877A -X -M -F main.hex

If all went well, you should get an output similar to this:

PICkit 2 Program Report
23-1-2022, 21:01:29
Device Type: PIC16F877A

Program Succeeded.

Operation Succeeded


CategoryHowTo

Microcontrollers/PIC (last edited 2022-01-23T22:55:49+0000 by TrevorRoydhouse)