This example will demonstrate how to set up the SSD1306 OLED Screen on-board the Curiosity Nano Explorer with a PIC18F56Q24 MCU.
- MPLAB® X IDE 6.20.0 or newer
- MPLAB® XC8 3.00 or newer
- MPLAB® Code Configurator (MCC) 5.7.1 or newer
- Microchip PIC18F-Q Series DFP 1.27.449 or newer
- PIC18F56Q24 Curiosity Nano Evaluation Kit
- Curiosity Nano Explorer
- USB Type-C® cable
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.
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.
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.
On MPLAB X IDE, click File>Open Project>pic18f56q24-curiosity-nano-explorer-ssd1306.X
to open the example 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.
Once the programming the board is completed, as shown in Output window, the SSD1306 OLED display will turn on.
To cycle to the next screen state, push the SW0 button on the PIC18F56Q24 Curiosity Nano board.
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.
Now click the play button on the associated COM port to start capturing.
Click the Send to Terminal button in the window that pops up and then click Close to close the window.
Upon entering text, the text entered in the application is echoed on the terminal and OLED display.
Push the SW0 button to reset the display and return to the initial screen state.
To open MCC, click the MCC button:
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:
Go to Device Resources to add I2C1_Host and UART2 from Drivers. Once successfully added, it will appear as shown:
This is now shown in the Application Builder tab as well:
- 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
- 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
- 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
- Follow the setup for pin naming and settings by selecting Pins from the right-side toolbar:
-
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.
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.
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().
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.
- To add files, go to the path where your project is located and add the respective header files
SSD1306.h
,font.h
and source fileSSD1306.c
in your directory. Then, right-click in MPLAB X IDE to Add Existing Item... for those files to reflect in the IDE.
- Under Header Files, add
SSD1306.h
andfont.h
header files to your project - Under Source Files, add
SSD1306.c
file and modify/add themain.c
to follow the example project's application
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.