Introduction: Microchip PIC Library to Control a 20 by 4 LCD Over I2C
This Instructable presents a 'C' library for controlling a single 20 by 4 LCD based on the HD44780 LCD controller over I2C using the NXP PCF8574 Remote 8bit IO Expander. The LCD is configured for 4 bit interfacing and can be written to and read from via the microcontroller. In this case the PIC18F2685 running at 4MHz.
To use the source and create the microcontroller circuitry you will need to be reasonably au fait with Microchip microcontrollers, the 'C' programming language and electronics in general.
In short, it's not for the beginner. I have included the source code and MPLABX project to get you up and running quickly. It is provided free to use in what ever way you see fit (just make a mention of me), though it comes unsupported.
I used the following system set up;
- PIC18F2685 (4MHz XTAL). Purchased from MicrochipDIRECT. £5.30
- 20 by 4 2004A LCD based off HD44780. Purchased from Ebay £3.57.
- MPLABX IDE v2.15. Free from Microchip
- Microchip compiler XC8 v1.35. Free from Microchip
- I2C to 1602 convertor board. Purchased from Ebay £1.79
- MPLABX XC8 Peripheral Library. Free from Microchip.
I created the library as part of a project to build a bus pirate for my Scantronic 9800 house alarm. Its an old alarm and I want to add internet and GSM connectivity to it without paying the outrageous UK prices. This is part of the first steps to 'crack' the comms between control box and remote key pad.
As I couldn't find a well written library with all the functionality I wanted I decided to 'roll my own.'
Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.
Step 1: The Hardware
As mentioned earlier I used the PIC18F2685 primarily due to footprint and space constraints in the finished project.
This meant I was strapped for IO given this is a 28 pin PDIP package. As the peripheral comms was not particularly high speed and I wanted to add an EEPROM and an RTC the logical choice was for I2C as the comms to the LCD. Microchip offer an outstanding free Peripheral Library for the 8bit Microcontrollers which made life a lot easier.
I purchased a vanilla 2004A 20 by 4 LCD from Ebay (£3.57)
and a '1602LCD Display IIC/I2C/I/SPI Serial Interface Module Port For Arduino K6' (£1.79). I have no idea how Ebay sellers decide on these titles.
I soldered the two together and wired them up to the microcontroller as shown in the Pics above.
Step 2: The Software
As mentioned earlier I have included a zipped copy of the full source including the MPLABX project this should allow you to try out the code as quickly as possible (all the include paths are relative).
The software comprises 4 main parts;
- The LCD Driver Library : LCD_I2C_PCF8574.c and .h
- The I2C bus access code : I2C_UTILS.c and .h
- The time delay calls : DELAY.c and .h
- The main code : main.c
There are some other files; mcc.c/.h, pin_manager.c/.h. These were added by the Microchip code configurator and I left them in.
I looked at a few libraries (Arduino ones) to get inspiration. In LCD_I2C_PCF8574.c I have added a lot of background and links to where you can get hold of other source, documentation and data on the PIC18F2685, I2C, the LCD and IO expander should you be so inclined. I also added a link to the library I ripped for the character generation. Thanks Mario. This file also contains details on how you may want to customise to your implementation, these are tagged with 'TODO adapt' so you can use the MPLABX task list to grab them.
I took all my details/nomenclature etc. from an Hitachi hard copy LCD manual (yes hard copy, real paper an'all!) I obtained in the early 1980's when we were still printing on flattened trees.
The code in main.c exercises all the functionality of the display including reading DD and CG ram and the busy flag. It should give a good heads up on how to use the library. I included a link to a YouTube Video so you can see it's potential.