Introduction
Two or four line character LCD modules with 16 or 20 characters per line have been around for a long time using the Hitachi controller. Firmware used to write to the display can be located at various places on the internet. The older LCD modules can be difficult to view from some angles even with a backlight. OLED character displays have a much brighter display and better viewing angle. Firmware to control an OLED character display is very similar to the firmware for LCD control but there are a few differences, chiefly in the initialization code. OLED displays can be set up for 8-bit or 4-bit control - like LCD displays. This tip provides a Microchip MBLABX
project that contains a full set of functions to control Newhaven OLED character displays in 4-bit mode. The tip discusses how to use the OLED control functions.
There doesn't seem to be much information about using an OLED module in 4-bit mode floating around the internet. That is why this tip is being written.
Using the Code
FrontPanel.zip contains an MBLABX C18 based project. Install MBLABX
from Microchip and then open the FrontPanel.X
project. You won't need C18 to open it in MPLABX
although MPLABX
will identify any missing include files. FrontPanel.c shows which functions to call to initialize the OLED display and write to the display. The project uses an 18F2550 microcontroller running at 32MHz. The 'C' code is fairly generic but would have to be adapted to a different family of microcontrollers. The MicrocontrollerDef.h file defines the microcontroller being used. The WaitFuncs.h file has some macros to define the clock speed and delay functions. The OLEDBusControl
files can be ignored for the purposes of this article. FrontPanel.c contains the 'main
' function.
The OLED display control functions are in the OLED.c source code file. Most of the functions in that file do not need to be invoked. The InitOLED
, SetCursorToStartOfLine
, StringOutzOLED
, StringOutczOLED
, and CusorRowColOLED
functions are probably the only ones needed.
BOOL InitOLED(void)
InitOLED
initializes the module to operate in 4-bit mode. Macros in OLED.h define the lower four bits of PORTA to be the data bus to the module. Macros in OLED.h also provide definitions for the RS, RW, and E control pins to the module. These macros can be altered based on the type of microcontroller and the particular pins used. This function takes no arguments and returns TRUE
if successful.
BOOL SetCursorToStartOfLine(byte byLineNumber)
SetCursorToStartOfLine
sets the OLED cursor to be at the start of a line. Any writes to the module will then begin at the start of this line. The cursor advances automatically with each character written to the display. The argument, byLineNumber
, is a number from 1
to 4
. If the particular module only has two lines, then byLineNumber
should only be 1 or 2. Returns TRUE
if successful.
BOOL StringOutzOLED(byte nLen, byte *pStr)
StringOutzOLED
writes bytes from data RAM memory to the display. It has two arguments - a maximum number of bytes to write and the string
in RAM to write to the display. The function will write the maximum number of bytes or until a NULL
is encountered in the string
. Returns TRUE
if successful.
BOOL StringOutczOLED(byte nLen, const rom byte *pStr)
StringOutczOLED
writes bytes from program memory to the display. It has two arguments - a maximum number of bytes to write and the string
in program memory to write to the display. The function will write the maximum number of bytes or until a NULL
is encountered in the string
. Returns TRUE
if successful.
BOOL CusorRowColOLED(byte byROW, byte byCOL)
CursorRowColOLED
positions the OLED cursor using a row and column specifiers. The byROW
argument can be a value from 1 to 4, based on which OLED module is being used. The byCOL
argument can be a value from 1 to 20 based on which OLED module is being used. For example, if the OLED has 2 lines and 16 characters per line, then byROW
can have a value of 1 or 2. byCOL
can have a value from 1 to 16.
The relevant function calls in FrontPanel.c are shown below. The Messages
array contains the string
s from program memory that can be sent to the display. The main function shows the call to InitOLED
and then the call to set the cursor to the start of a line and then display one of the Messages string
s on that line.
#pragma romdata
const rom byte *Messages[4] = {
"Front Panel Test",
"using new code..",
"Keypress hapn'n ",
"Even More Text ",
};
void main(void)
{.
.
PicRegInit(); AppRegInit();
InitOLED(); .
.
SetCursorToStartOfLine(1); StringOutczOLED(16, Messages[0]); .
.
This code has successfully been used with a Newhaven 2 line by 16 character OLED module.
I just looked at a 4-line Newhaven OLED module data sheet. They are quite a bit different from the 2-line module. This code will only work with the 2 line Newhaven OLED modules.
After a little more looking, Newhaven has some 4-line modules that this code will work with. The 4-line modules with a 16-pin connector will work with this code. The modules with a 20-pin connector will not work with this code.
History
- 24th July, 2015 - Original submission
- 11th August 2015 - Updated to note that the code only works with the Newhaven 2-line OLED module