Skip to content

This example will demonstrate how to interface the SSD1306 OLED on-board the Curiosity Nano Explorer development board using I2C serial protocol.

License

Notifications You must be signed in to change notification settings

microchip-pic-avr-examples/pic18f56q24-curiosity-nano-explorer-ssd1306-oled

Repository files navigation

Microchip Technologies Inc.

SSD1306 OLED Setup Using the PIC18F56Q24 MCU

This example will demonstrate how to set up the SSD1306 OLED Screen on-board the Curiosity Nano Explorer with a PIC18F56Q24 MCU.

Related Documentation

Software Used

Hardware Used

Setup

The Curiosity Nano Explorer User Guide provides pin mapping information. UART TX, UART RX will be used to communicate between the host PC and PIC18F56Q24. Refer to the UART Peripherals section in the Curiosity Nano Explorer User Guide for more details.
OLED Pin Mapping
I2C SDA, I2C SCL and IO RESET will be used to communicate with the SSD1306 OLED screen. Refer to the OLED Display Module section in the Curiosity Nano Explorer User Guide for more details.
OLED Pin Mapping

How to Run the Example Project

Connect the PIC18F56Q24 Curiosity Nano board to the host PC using a USB-C® cable and mount this setup onto the Curiosity Nano Explorer board, as shown below. hardware setup

On MPLAB X IDE, click File>Open Project>pic18f56q24-curiosity-nano-explorer-ssd1306.X to open the example project.
open project

select project

If the device is connected, the Kit Window should show up on the right side. Click the Make and Program Device button to program the device.
program device

Once the programming the board is completed, as shown in Output window, the SSD1306 OLED display will turn on.
bitmap

To cycle to the next screen state, push the SW0 button on the PIC18F56Q24 Curiosity Nano board.
bitmap

Now, open a terminal application such as Tera Term and select the associated COM port where user input will be echoed on the OLED Display and the terminal application. This example uses the MPLAB Data Visualizer.

Ensure that the baud rate is set to 115200 and the Echo to Screen option is enabled.
datavisualizer

Now click the play button on the associated COM port to start capturing.

playbutton

Click the Send to Terminal button in the window that pops up and then click Close to close the window.
sendtoterminal

Upon entering text, the text entered in the application is echoed on the terminal and OLED display.


helloworld

Push the SW0 button to reset the display and return to the initial screen state.

MCC Setup

To open MCC, click the MCC button:
Opening MCC

Add the necessary resources by going to Project Resources to setup I²C, UART and GPIO pins. Consider the following pin mapping on the PIC18F56Q24 Curiosity to identify the resources:
ExplorerMCP23008

Go to Device Resources to add I2C1_Host and UART2 from Drivers. Once successfully added, it will appear as shown:
Project Resources

This is now shown in the Application Builder tab as well:
Application Builder

UART Settings

  • Select UART2 from the Application Builder tab
  • Make sure Redirect Printf to UART is enabled
  • Enable interrupt driven
  • Set software transmit and receive buffer size to 128
  • Set baud rate to 115200
  • UART2 PLIB can be left at default settings
    UART setup

I²C Settings

  • Select I2C1_Host from Application Builder
  • Set the Requested speed to 400 KHz
  • Select I2C1 from Application Builder
  • Make sure the I²C interrupt is enabled by clicking I2C1 and enabling Interrupt Driven
    I2C setup

    I2C Plib setup

Pins Settings

  • For pins, reference the previous pin mapping, selecting UART2 RX and TX (RB5 and RB4), I2C1 SDA and SCL (RB2 and RB1) and GPIO Button and Reset (RF3 and RF2) pins, respectively
  • Open the Pin Grid View by selecting pins from System in the Application Builder


pin grid view setup

  • Follow the setup for pin naming and settings by selecting Pins from the right-side toolbar:


pins setup

  • Rename RF2 to RESET, set Weak Pullup to enabled

  • Rename RF3 to BUTTON, set Start High to enabled

  • Enable Weak Pullup for UART2's RX pin RB4

  • Click Generate in Project Resources and MCC will generate all the necessary code that will appear under MCC Generated Files.
    generate

SSD1306 Driver and I²C Interfacing

The communication between the PIC18F56Q24 MCU and the SSD1306 OLED display is managed by the driver files SSD1306.h and SSD1306.c. This section provides a brief overview of how they work.

SSD1306.h - The Header File

This file acts as the public interface for the driver. It defines key constants based on the SSD1306 data sheet, such as the I²C slave address (0x3D), screen dimensions, and the hexadecimal codes for various commands (e.g., SSD1306_DISPLAY_ON, SSD1306_SET_CONTRAST_CONTROL). It also declares the function prototypes that main.c uses to control the display, like SSD1306_Init(), SSD1306_WriteString(), and SSD1306_Clear().

SSD1306.c - The Implementation

This file contains the logic for sending information to the display over the I²C bus. The MCU acts as the I²C host, and the SSD1306 controller is the I²C client. All communication is built upon two fundamental operations: sending a command and sending data.

Commands are instructions that configure the display's state, such as setting the contrast, inverting the screen, or moving the cursor (page address).

Data consists of bytes that represent the pixel patterns to be written into the display's Graphic Display Data RAM (GDDRAM). Each bit in a data byte corresponds to a single pixel's state (on or off).

The driver uses the I²C functions generated by MCC (I2C1_Write) to transmit this information. The core routines for this are SSD1306_SendCommand and SSD1306_SendData.

A control byte is sent before every command or data byte. According to the SSD1306 data sheet, a control byte of 0x00 indicates that the next byte is a command, while a control byte of 0x40 indicates that the following byte is data for the GDDRAM.

Here is a snippet from SSD1306.c showing these key functions:

Code Snippet:

// Send I2C Commands
void SSD1306_SendCommand(uint8_t command) {
    uint8_t cmd[] = {SSD1306_COMMAND, command};
    I2C1_Write(SSD1306_I2C_ADDRESS, cmd, sizeof(cmd));
    __delay_us(100);
}
// Send I2C Data
void SSD1306_SendData(uint8_t data) {
    uint8_t d[] = {SSD1306_DATA_CONTINUE, data};
    I2C1_Write(SSD1306_I2C_ADDRESS, d, sizeof(d));
    __delay_us(100);
}

Higher-level functions like SSD1306_WriteString and SSD1306_DrawBitmap are built on top of these. For example, to write a character, the SSD1306_WriteCharacter function looks up the character's 5-byte pattern from the font.h file and calls SSD1306_SendData for each byte in the pattern to draw it on the screen.

Adding Driver Code and Modifying main.c File

  • To add files, go to the path where your project is located and add the respective header files SSD1306.h, font.h and source file SSD1306.c in your directory. Then, right-click in MPLAB X IDE to Add Existing Item... for those files to reflect in the IDE.
    addfiles


    addfiles

  • Under Header Files, add SSD1306.h and font.h header files to your project
  • Under Source Files, add SSD1306.c file and modify/add the main.c to follow the example project's application


main code

Summary

This example project demonstrates how to set up the SSD1306 OLED Screen on-board the Curiosity Nano Explorer with a PIC18F56Q24 MCU. It utilizes GPIO, UART and I²C peripherals on the PIC18F56Q24. The application echoes user input in the terminal and OLED screen upon user entry.

About

This example will demonstrate how to interface the SSD1306 OLED on-board the Curiosity Nano Explorer development board using I2C serial protocol.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •