PIC MCU with SSD1306 OLED – SPI Mode Example | mikroC Projects

This post shows how to interface PIC18F46K22 microcontroller with SSD1306 OLED (128×64 pixel).
The SSD1306 is a monochrome display which means it has only one color (white, blue, yellow …).
This display communicates with the master device over I2C mode, SPI mode or 8-bit parallel mode.
This topic shows how to use the SSD1306 OLED in SPI mode.
The compiler used in this project is mikroElektronika mikroC PRO for PIC.

SPI: Serial Peripheral Interface.
I2C (or IIC): Inter-Integrated Circuit.

The SSD1306 OLED which I’m going to use in this project is shown below (back view), the default mode is SPI which can be changed to I2C by removing the resistors R3 and placing the resistors R1 & R8 (as written on the board). Note that resistance of R1 = R3 = R8 = 0 ohm.

SSD1306 OLED 128x64

To see how to use the SSD1306 OLED display module in I2C mode, visit this post:
PIC MCU with SSD1306 OLED – I2C Mode Example | mikroC Projects

Hardware Required:

  • PIC18F46K22 microcontroller   —->  datasheet
  • SSD1306 OLED display
  • 5V source
  • Breadboard
  • Jumper wires

PIC18F46K22 with SSD1306 OLED display circuit (4-wire SPI mode):
Example circuit diagram is shown below.

The SSD1306 OLED display module shown in the circuit diagram has 7 pins (from left to right):
GND, VCC, SCK (serial clock), SDA (serial data), RES (reset), DC (or D/C: data/command) and CS (chip select).

The SSD1306 display board is supplied with 5V where GND is connected to circuit ground and VCC is connected to +5V.

PIC18F46K22 SSD1306 OLED SPI mode circuit

All the grounded terminals are connected together.

The PIC18F46K22 microcontroller has 2 hardware SPI modules (MSSP1 and MSSP2 modules).
In this project SPI1 module is used with SCK1 on pin RC3 (#18) and SDO1 (MOSI) on pin RC5 (#24). SCK1 and SDO1 pins of the PIC18F46K22 MCU are respectively connected to SCK and SDA pins of the SSD1306 OLED display module.

The other pins of the display module which are RES, DC and CS are respectively connected to PIC18F46K22 pins RD4 (#27), RD5 (#28) and RD6 (#29).

SCK: Serial Clock.
SDO: Serial Data-Out, synonym for MOSI.
MOSI: Master-Out Slave-In.

In this project the PIC18F46K22 microcontroller runs with its internal oscillator @ 16 MHz, MCLR pin is configured as an input pin.

PIC18F46K22 with SSD1306 OLED display C code (4-wire SPI mode):
The following C code is for mikroC PRO for PIC compiler, it was tested with version 7.6.0.

To be able to compile project C code with no error, 2 libraries are required:
The first library is a driver for the SSD1306 OLED display, its full name (with extension) is SSD1306.c, download link is below:
SSD1306 OLED display library for mikroC compiler

The second library is graphics library, its full name is GFX_Library.c, download link is the one below:
Graphics library for mikroC compiler

after the download of the 2 library files, add both of them to project folder.

In this example, the SSD1306 OLED display is connected to hardware SPI1 (MSSP1 module) of the PIC18F46K22 microcontroller. Hardware SPI2 module (MSSP2) or mikroC software SPI library (built-in) also can be used.
To use hardware SPI1 module the following line have to be defined (before #include “SSD1306.c”):
#define SSD1306_HARD_SPI1

To use mikroC software SPI library just define the following line:
#define SSD1306_SOFT_SPI
In software SPI we’ve to configure the pins used (MISO, MOSI and SCK) by mikroC software SPI library as shown in the help file.
Also the configuration should be initialized in the main code (always before initializing the display) with this line:
Soft_SPI_Init();
The clock frequency of the software SPI library is fixed to 20 kHz (slow!).

If the display module is connected to hardware SPI2 (MSSP2 module) then we’ve to define this line:
#define SSD1306_HARD_SPI2
and hardware SPI2 module must be initialized before the initialization of the SSD1306 OLED display (for example SPI2_Init();).

The SSD1306 OLED display library supports three types depending on screen size (number of pixels): 128×64, 128×32 and 96×16. The default one is 128×64.
If the display used is 128×32 then the line below has to be added before #include “SSD1306.c”:
#define SSD1306_128_32
and if the display used is 96×16 then use this definition:
#define SSD1306_96_16

Hints:
The 2 library files are included in the main code as shown below:

The line below allows us to use hardware SPI1 module as mentioned above:

Other required pins (RST, DC and CS) are defined as shown below:

The display is initialized as shown below:

Rest of code is described through comments.

Full mikroC code:

Protoboard hardware circuit:

PIC18F46K22 with SSD1306 OLED SPI mode interfacing

Proteus simulation:
This example works with Proteus simulation software since it (Proteus) contains the SSD1306 OLED library. The video below shows the simulation result:

Proteus simulation file download link is below, use version 8.6 or higher to open it:
PIC18F46K22 + SSD1306 OLED SPI mode

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.