PIC Assembler Code


When I put information about my GPS SD card data logger on these web pages there was a lot of interest from people who wanted to see the source code. As most of them seemed to know it was possible to read and write SD cards with a PIC. But as I had found earlier the full source code to do it is not available for free to download.

At the time I had not made a decision about what to do with the code that I was using. I was glad that people were interested in it, but I also knew that there was not a big collection of free, fully working PIC code available for download. What is available is bits and pieces in peoples projects and in Microchip application notes. I didn't find any of these easy to understand or use in my projects which is why I started from scratch.

Code Description

What I have written is intended to be easy to read with comments, ASCII art diagrams of waveforms and instruction counts. I wanted something that would be easy to understand when I re-read it again months later. This also means that it is split across a number of include files. It is written to be easy to use and re-use which means that it isn't as fast as it could be in some places. Most of the code is not written with particular regard to optimising speed or reducing code size. Where one is preferred over the other it is normally speed that beats instruction count. Minimising the number of temporary variables is important too though.

I have avoided the use of interrupts as well which means for example that the RS232 code waits for incoming data and doesn't use an interrupt to start capturing it. There are also no macros so that everything is implemented as functions. The configuration of the options (like pins to use, PIC device clock, baud rate) are controlled by constants that need to be defined before the file is included.


The collection that is available for download here is a subset of what I have written. It includes library functions rather than complete applications in most cases.

Delay Functions

This set of functions provide exact time delays. These are measured in milliseconds, microseconds and instruction cycles depending on which function is called.

The functions are written to work for a variety of clock speeds but the principle is the same if a different clock speed is used.

EEPROM Functions

This set of functions allow reading and writing to the EEPROM within the PIC device. They take care of the different methods which need to be used for the different PIC device types. Currently these functions have been confirmed for the 16F675 and 16F819 devices.


These functions implement receive and transmit RS232 (8N1 - 8 bits, no parity, 1 stop bit) functions. They will operate easily at 57600 baud with a PIC device clocked at 4 MHz. The receive functions optionally implement hardware flow control of the RTS line to ensure that the sender knows when it is safe to send (removes the need for interrupts in general). No special hardware is required, just 1 input pin for RXD, 1 output pin for TXD and one output pin for RTS (optional) all of which must be in the same port.

Various combinations of PIC clock speed and baud rate are provided.


There are two sets of I2C functions, one that just uses bit-banging and provides a master-only interface and one that uses the SSP port available on some PIC devices and provides slave and master interfaces.

The first, simplest, set of functions implement full control over the I2C bus with the ability to send start, stop and restart bits as well as transmit and receive bytes of data as a master device. No special hardware is required, just 1 input/output pin for SDA and 1 input/output pin for SCL both of which must be in the same port.

The second, more complicated, set of functions implement both master and slave interfaces but require the use of the SSP port to do this. The master functions allow the same operations as the earlier I2C code but they also allow the SSP implementation of a slave device to send and receive data.

Both sets of functions are designed to operate with 4 MHz or 8 MHz clocks and aim for a 100 kHz I2C bit rate. If a different speed clock is used then a different I2C speed will be seen.


The SPI functions again use the SSP port on the PIC device and allow for initialising the port and sending and receiving bytes of data. These are quite simple functions since all of the work is done by the SSP port.

MMC / SD Card

These functions use the SPI functions to read and write to MMC cards and SD cards in SPI mode. The functions implemented are CMD0, CMD1, CMD8, CMD17, CMD24, CMD55 and ACMD41 from the SD card instruction set. These instructions are to switch to MMC mode, initialise the card, read a block (512 bytes) and write a block (512 bytes). There are no functions for file systems (probably impossible without at least a 512 byte RAM buffer) and no support for the High Capacity SD cards (above 2 GB).


These PS/2 functions implement both the "host" and "device" ends of the PS/2 mouse and keyboard serial protocol as well as a monitor function. The PS/2 protocol is similar to RS232 in that the data is sent serially, LSB first with a start bit, parity bit and stop bit. The "host" end of the link is always in control but the clock comes from the "device".


These functions implement both receive and transmit functions for several different types of Infra-Red remote controls. In all cases that the functions implement these use a 38 kHz carrier wave (26 micro-second period) that is turned on and off for periods of between 400 micro-seconds and 4500 micro-seconds.

I have implemented the protocols used by the remote controls that I had available. Where possible I have identified these remote control devices with published data formats, but some remain unidentified.


These functions allow controlling the PCD8544 IC that is used as the LCD display driver in several Nokia mobile phones. The code has been written and tested on a Nokia 3210 phone display, the 3310 should also be supported. The LCD has a resolution of 84x48 pixels in monochrome and accepts only graphical commands. There are two sets of functions provided, the first is general purpose access to the IC in the LCD and the second provides character based access to the display, providing a 16x6 character display. The first set of functions allow initialising the PIC connections and the LCD IC and low level setting of the control pins. There are also functions to reset the display and set the mode (normal or inverse). The interface to the IC is SPI based and a bit-banging method is used (although the SSP port would also work). The second set of functions treat the 84x48 pixel display as a 16x6 character display. Functions are provided to clear the display, set the row and column positions and draw characters.

Project Code

All of the projects on this web-site are included in the source code.

With each project is included circuit diagrams, layout diagrams, PIC source code and compiled hex file as well as miscellaneous test programs written in C source code.

Important Information

Before you download the source code for these PIC functions it is important to understand what is available for download here.


The assembler functions that I am making available for download are copyrighted by me. They are not public domain which would allow you to do anything that you like with them. You can copy them only if you follow the terms of the license which I have applied to them.

Most PIC software available for download has no copyright notice attached to it which makes it difficult to know if it can be copied and used. By selecting a well defined and well used license the conditions for copying the code are clear.


This software is covered by the same license as the other software that I have released on these web pages. This is the same license that is used for the a lot of free and open source software like the Linux operating system kernel and most of the applications for it.

The license is the Gnu General Public License (Version 2). The full text of the GPL v2 as applied to this software is available in the file called COPYING that is available here or in the download.



This is only software, it cannot check the electrical connections to the PIC device or the properties of the components that you connect to it. Using this software is not a substitute for knowing what you are doing electrically.

These functions all worked for me on the PIC devices that I used when I wrote them. They may not work for you because you use a different compiler, or a different PIC device, or a different clock speed.


The file to download is the latest version dated 2010-09-19.


Initial release of code. Contains delays, RS232, I2C, SPI and MMC/SD card functions.
Updated release of code. Contains previous code plus library code for EEPROM, PS/2, PCD8544 LCD and project code for GPS SD card logger.
Updated release of code. Contains previous code plus library code for Infra-Red transmit/receive and DS1307 I2C functions and project code for temperature recorder, Infra-Red transmitter and RS232 converters (I2C, SPI, IR & PS/2).
Same contents as 2009-08-01 but with some bug fixes for bank switching and an extra constraint on bank used for I2C temporary variable.

File Format

The download file is in compressed (gzipped) tar file format which is the most common archive format used on Linux/UNIX systems. It should be readable by WinZip if you use Microsoft Windows.

The files within the download are all in plain text with UNIX line endings. If you use them on Microsoft Windows you will find that not all software will display them correctly (notepad won't for example).

The files that contain the source code are all in assembler. I cannot help if you are trying to interface them to other code written in C because I don't know how to do that.

Usage Instructions

When you have uncompressed the archive you will see a directory tree containing the files. There is a general README text file at the top level and more detailed README files explaining how to use some of the particular functions.

PIC Programming and Circuit Design on Linux

I do all of my PIC programming and circuit design on Linux, using free software all of which is available under the same GPL license that I am using for these functions.
PIC Compilation
gpasm which is part of the gputils set of tools.
PIC Simulation
PIC Programming (software)
PIC Programming (hardware)
melabs EPIC parallel port programmer.
Circuit Design
gschem for schematics and pcb for PCB design.