I2C & SPI Mode | OLED (SSD1306) Interfacing with PIC18F46K22

Опубликовано: 22 Февраль 2025
на канале: Electronics Developer
561
like

-------------------------
📌Introduction
-------------------------
This post shows how to interface PIC18F46K22 microcontroller with SSD1306 OLED (128×64 pixel).
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 I2C mode.CCS C Compiler is used in this project.

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

The SSD1306 display contains a driver chip with the same name (SSD1306), it can communicate with the master device (microcontroller, microprocessor …) over I2C protocol, SPI protocol or 8-bit parallel protocol.
The I2C protocol needs only 2 lines: SDA (serial data) and SCK (serial clock), an additional line is required which is a reset line (RST). The SPI protocol is faster than the I2C protocol but it uses more pins: SCK, SDA, CS (chip select: active low), D/C (data/command) and a rest pin (RST).

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 on OLED

-------------------------
📌 Circuit Design
-------------------------
📌 I2C Mode :
GND, VCC, SCK (serial clock), SDA (serial data), RES (reset), DC (or D/C: data/command) and CS (chip select)

All the grounded terminals are connected together.The PIC18F46K22 microcontroller has 2 hardware I2C modules (MSSP1 and MSSP2 modules).In this project I2C1 module is used with SDA1 on pin RC4 (#23) and SCL1 on pin RC3 (#18). The SDA1 pin of the MCU is connected to the SDA pin of the display and the SCL1 pin of the MCU is connected to the SCL pin of the display.
The reset pin of the display (RES) is connected to pin RD4 (#27) of the microcontroller.Chip select pin of the display (CS) is not connected (not used in I2C mode).

The SSD1306 OLED display DC pin is connected to VDD which means I2C slave address of the device is 0x7A. If the DC pin is connected to ground (GND) then the I2C slave address becomes 0x78.In this project the PIC18F46K22 microcontroller runs with its internal oscillator @ 32 MHz, MCLR pin is configured as an input pin.

📌 SPI Mode :
GND, VCC, SCK (serial clock), SDA (serial data), RES (reset), DC (or D/C: data/command) and CS (chip select).
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 @ 32 MHz, MCLR pin is configured as an input pin.

-------------------------
📌 Tags
-------------------------
#I2C #SPI #OLED

-------------------------
📌 Source Code
-------------------------
To be able to compile project C code with no error, 2 libraries are required
1. GFX_Library.c
2. SSD1306.c

Check out comment section for source code

📌 Tune With us so you never miss any update
-------------------------------------------------------------------------------------------------------------------------------------------------------------
🏷️Telegram : https://t.me/electronicsdeveloper
🏷️Facebook :   / electronicsdeveloper  
🏷️Instagram :   / electronics_developer  
🏷️Tweeter : https://twitter.com/Electronics_Dev?s=09
🏷️LinkedIn :   / electronics-with-shreyash  
🏷️Pinterest :   / electronics_developer  

Copyright Disclaimer under Section 107 of the copyright act 1976
--------------------------------------------------------------------------------------------------------------------------------------------------------------
Copyright Disclaimer under Section 107 of the copyright act 1976, allowance is made for fair use for purposes such as criticism, comment, news reporting, scholarship, and research. Fair use is a use permitted by copyright statute that might otherwise be infringing. Non-profit, educational or personal use tips the balance in favour of fair use.

---------------------------------------------------------------------------------------------------------------------------------------------------------------
About Electronics Developer : YouTube certified creative essentials and asset monetization
---------------------------------------------------------------------------------------------------------------------------------------------------------------