Tuesday, January 9, 2024

PIC16F887 SH1106 I2C OLED Display Example using XC8

Overview

An SH1106 is a 132x64 LED display driver with controller. This chip support 8-bit 6800-series parallel interface, 8-bit 8080-series parallel interface, Serial Peripheral Interface (SPI), and Inter Integrated Circuit (I2C) bus interface. It usually come with an 1.3 inches 128x64 OLED display module with an SPI or an I2C interface. 

PIC16F887 SH1106 I2C OLED Display Example using XC8
PIC16F887 OLED Display

Using this module with an Arduino is very popular. Some programmer creates a simple game console using this display. Arduino platform has a lot of libraries and graphic for this LCD module. 

PIC16F887 SH1106 I2C OLED Display Example using XC8

PIC16F887 SH1106 I2C OLED Display Example using XC8

PIC16F887 SH1106 I2C OLED Display Example using XC8
1.3 Inches 1106 128x64 IIC OLED Module

PIC16F887 SH1106 I2C OLED Display Example using XC8
1.3 Inches 1106 128x64 IIC OLED Module

 

Without Arduino we can check the LCD Wiki website for programming and interfacing with this device using different micro-processor.

PIC16F887 SH1106 I2C OLED Display Example using XC8
For full specification please check product datasheet. The LCD is mounted on a PCB with a ready to use I2C interface. Its supply voltage is 5VDC by default.

Here we will use a two-wire I2C bidirectional bus interface. This module has an on-board 10kΩ pull-up resistors. Its I2C write address is 0x78 by default. This chip could handle up to 40kHz serial clock frequency. But I use only 200kHz with PIC16F887 I2C module to prevent it from being halted.

PIC16F887 SH1106 I2C OLED Display Example using XC8
I2C System Configuration

Using only two-wire data bus the controller can communicate with multiple devices. This I2C display is writable and readable.

PIC16F887 SH1106 I2C OLED Display Example using XC8

The micro-processor writes command or data to this display. It begin by an I2C start bit, device address (eg. 0x78), Acknowledge bit (A), Control Byte, Data Byte, and an I2C stop bit. 

Data/Command (D/C) bit of the control byte determine between display RAM data and command as it's shown in the picture above.

Display RAM Data can also be read from this display module.

To use it properly we need to set its display commands using a command set below.

  1. Set Low Column Address: (00H - 0FH)
  2. Set Higher Column Address: (10H - 1FH)
  3. Set Pump voltage value: (30H - 33H)
  4. Set Display Start Line: (40H - 7FH)
  5. Set Contrast Control Register: (Double Bytes Command), The Contrast Control Mode Set (81H), Contrast Data Register Set (00H - FFH)
  6. Set Segment Re-map: (A0H - A1H)
  7. Set Entire Display OFF/ON: (A4H - A5H)
  8. Set Normal/Reverse Display: (A6H - A7H)
  9. Set Multiplex Ration: (Double Bytes Command), Multiplex Ration Mode Set: (A8H), Multiplex Ration Data Set: (00H - 3FH).
  10. Set DC-DC OFF/ON: (Double Bytes Command), DC-DC Control Mode Set: (ADH), DC-DC ON/OFF Mode Set: (8AH - 8BH).
  11. Display OFF/ON: (AEH - AFH)
  12. Set Page Address: (B0H - B7H)
  13. Set Common Output Scan Direction: (C0H - C8H)
  14. Set Display Offset: (Double Bytes Command), Display Offset Mode Set: (D3H), Display Offset Data Set: (00H - 3FH)
  15. Set Display Clock Divide Ratio/Oscillator Frequency: (Double Bytes Command), Divide Ratio/Frequency Mode Set: (D5H), Divide Ratio/Oscillator Frequency Data Set: (00H - FFH).
  16. Set Dis-Charge/Pre-Charge Period: (Double Bytes Command), Pre-charge Period Mode Set: (D9H), Dis-charge/Pre-charge Period Data Set: (00H - FFH)
  17. Set Common pads hardware configuration: (Double Bytes Command), Common Pads Hardware Configuration Mode Set: (DAH), Sequential/Alternative Mode Set: (02H - 12H).
  18. Set VCOM Deselect Level: (Double Bytes Command), VCOM Deselect Level Mode Set: (DBH), VCOM Deselect Level Data Set: (00H - FFH).
  19. Read-Modify-Write: (E0H)
  20. End: (EEH)
  21. NOP: (E3H)
  22. Write Display Data
  23. Read Status
  24. Read Display Data

For a fully explained text, you can check its datasheet. These command are summerized in a command table below.

PIC16F887 SH1106 I2C OLED Display Example using XC8
Command Table 1

PIC16F887 SH1106 I2C OLED Display Example using XC8
Command Table 2

We can see its user's manual include programming tutorial to get used to this display controller.

PIC16F887 MPLABX IDE and XC8 Programming

An 8-bit PIC16F887 is suitable for this display controller since it has a high speed I2C module inside. I configure this module with a clock frequency of 200kHz. The program will send a 8x16 character text to the display.

  1. /*
  2.  * File: main.c
  3.  * Author: Admin
  4.  *
  5.  * Created on January 8, 2024, 8:50 PM
  6.  */
  7.  
  8. #include <xc.h>
  9. #include "config.h"
  10. #include "sh1106.h"
  11.  
  12. #define _XTAL_FREQ 20000000UL
  13.  
  14. void main(void) {
  15. display_init();
  16. __delay_ms(100);
  17. display_clear(0);
  18. display_text_8x16(0,0,"SH1106 I2C OLED");
  19. display_text_8x16(0,1,"And PIC16F887");
  20. display_text_8x16(0,2,"Programming With");
  21. display_text_8x16(0,3,"MPLABX IDE XC8");
  22. __delay_ms(5000);
  23. display_clear(0);
  24. while(1){
  25. display_text_8x16(0,0,"PIC16F887 SH1106");
  26. __delay_ms(2500);
  27. uint8_t h=15;
  28. for(uint8_t i=0;i<10;i++) {
  29. display_char_8x16(h,1,'0'+i);
  30. h+=10;
  31. }
  32. __delay_ms(2500);
  33. h=0;
  34. for(uint8_t i=0;i<14;i++) {
  35. display_char_8x16(h,2,'A'+i);
  36. h+=10;
  37. }
  38. __delay_ms(2500);
  39. h=0;
  40. for(uint8_t i=0;i<14;i++) {
  41. display_char_8x16(h,3,'N'+i);
  42. h+=10;
  43. }
  44. __delay_ms(10000);
  45. display_clear(255);
  46. __delay_ms(10000);
  47. display_clear(0);
  48. __delay_ms(1000);
  49. }
  50. return;
  51. }
  52.  

Only text fonts requires a lot of program space. To get additional fonts and graphic we can add an SPI or an I2C EEPROM or even a Flash memory to store those data.

PIC16F887 SH1106 I2C OLED Display Example using XC8
Protues Simulation
Protues has an LCD module for SSD1306. It's very similar to the SH1106. However simulating this module in Protues is slower than a physical hardware.


PIC16F887 SH1106 I2C OLED Display Example using XC8
Running Program

PIC16F887 SH1106 I2C OLED Display Example using XC8
Running Program


 

Click here to download its source file.



1 comment:

  1. Hi, I get compiling errors. I am using PIC886
    make -f nbproject/Makefile-default.mk SUBPROJECTS= .build-conf
    make -f nbproject/Makefile-default.mk dist/default/debug/I2C_SH1106_PIC886.X.debug.elf
    make[2]: Entering directory 'D:/I2C_SH1106_PIC886/I2C_SH1106_PIC886.X'
    make[2]: *** [nbproject/Makefile-default.mk:95: build/default/debug/_ext/135371093/i2c.p1] Error 1
    make[2]: *** Waiting for unfinished jobs....
    "C:\Program Files\Microchip\xc8\v2.50\bin\xc8-cc.exe" -mcpu=16F886 -c -D__DEBUG=1 -mdebugger=pickit3 -mdfp="C:/Program Files/Microchip/MPLABX/v6.20/packs/Microchip/PIC16Fxxx_DFP/1.6.156/xc8" -fshort-double -fshort-float -maddrqual=ignore -xassembler-with-cpp -mwarn=-3 -Wa,-a -DXPRJ_default=default -msummary=-psect,-class,+mem,-hex,-file -ginhx32 -Wl,--data-init -mno-keep-startup -mno-osccal -mno-resetbits -mno-save-resetbits -mno-download -mno-stackcall -mno-default-config-bits -gdwarf-3 -mstack=compiled:auto:auto -o build/default/debug/_ext/135371093/i2c.p1 C:/Users/jay/Downloads/i2c_sh1106_2.X/i2c_sh1106_2.X/i2c.c
    ::: error: (2047) 24-bit floating point types are not supported when compiling in C99
    (908) exit status = 1
    make[2]: Leaving directory 'D:/I2C_SH1106_PIC886/I2C_SH1106_PIC886.X'
    make[2]: Entering directory 'D:/I2C_SH1106_PIC886/I2C_SH1106_PIC886.X'
    "C:\Program Files\Microchip\xc8\v2.50\bin\xc8-cc.exe" -mcpu=16F886 -c -D__DEBUG=1 -mdebugger=pickit3 -mdfp="C:/Program Files/Microchip/MPLABX/v6.20/packs/Microchip/PIC16Fxxx_DFP/1.6.156/xc8" -fshort-double -fshort-float -maddrqual=ignore -xassembler-with-cpp -mwarn=-3 -Wa,-a -DXPRJ_default=default -msummary=-psect,-class,+mem,-hex,-file -ginhx32 -Wl,--data-init -mno-keep-startup -mno-osccal -mno-resetbits -mno-save-resetbits -mno-download -mno-stackcall -mno-default-config-bits -gdwarf-3 -mstack=compiled:auto:auto -o build/default/debug/_ext/135371093/main.p1 C:/Users/jay/Downloads/i2c_sh1106_2.X/i2c_sh1106_2.X/main.c
    make[2]: *** [nbproject/Makefile-default.mk:103: build/default/debug/_ext/135371093/main.p1] Error 1
    ::: error: (2047) 24-bit floating point types are not supported when compiling in C99
    (908) exit status = 1
    make[2]: Leaving directory 'D:/I2C_SH1106_PIC886/I2C_SH1106_PIC886.X'
    make[2]: Entering directory 'D:/I2C_SH1106_PIC886/I2C_SH1106_PIC886.X'
    "C:\Program Files\Microchip\xc8\v2.50\bin\xc8-cc.exe" -mcpu=16F886 -c -D__DEBUG=1 -mdebugger=pickit3 -mdfp="C:/Program Files/Microchip/MPLABX/v6.20/packs/Microchip/PIC16Fxxx_DFP/1.6.156/xc8" -fshort-double -fshort-float -maddrqual=ignore -xassembler-with-cpp -mwarn=-3 -Wa,-a -DXPRJ_default=default -msummary=-psect,-class,+mem,-hex,-file -ginhx32 -Wl,--data-init -mno-keep-startup -mno-osccal -mno-resetbits -mno-save-resetbits -mno-download -mno-stackcall -mno-default-config-bits -gdwarf-3 -mstack=compiled:auto:auto -o build/default/debug/_ext/135371093/sh1106.p1 C:/Users/jay/Downloads/i2c_sh1106_2.X/i2c_sh1106_2.X/sh1106.c
    ::: error: (2047) 24-bit floating point types are not supported when compiling in C99
    (908) exit status = 1
    make[2]: Leaving directory 'D:/I2C_SH1106_PIC886/I2C_SH1106_PIC886.X'
    make[2]: *** [nbproject/Makefile-default.mk:111: build/default/debug/_ext/135371093/sh1106.p1] Error 1
    make[1]: *** [nbproject/Makefile-default.mk:85: .build-conf] Error 2
    make: *** [nbproject/Makefile-impl.mk:39: .build-impl] Error 2

    BUILD FAILED (exit value 2, total time: 262ms)

    ReplyDelete

Search This Blog

Labels

25AA010A (1) 8051 (7) 93AA46B (1) ADC (30) Analog Comparator (1) Arduino (15) ARM (6) AT89C52 (7) ATMega32 (56) AVR (57) CCS PICC (28) DAC (1) DHT11 (2) Display (105) Distance Sensor (3) DS18B20 (3) dsPIC (2) dsPIC30F1010 (2) EEPROM (5) Environment Sensor (4) esp8266 (1) I2C (29) Input/Output (67) Interrupt (19) Keil (5) Keypad (10) LCD (47) Master/Slave (1) MAX7221 (1) MCP23017 (5) MCP23S17 (4) Meter (3) MikroC (2) Motor (15) MPLABX (71) Nokia 5110 LCD (3) OLED (2) One-Wire (6) Oscillator (8) PCB (6) PCD8544 (3) PCF8574 (5) PIC (107) PIC12F (2) PIC16F628A (2) PIC16F630 (1) PIC16F716 (3) PIC16F818 (10) PIC16F818/819 (2) PIC16F84A (15) PIC16F876A (1) PIC16F877A (9) PIC16F88 (1) PIC16F887 (60) PIC18 (19) PIC18F1220 (4) PIC18F2550 (3) PIC18F4550 (12) PWM (11) RTC (8) Sensor (10) SH1106 (1) Shift Register (11) Shift Registers (3) SPI (24) STM32 (6) STM32 Blue Pill (6) STM32CubeIDE (6) STM32F103C8T6 (6) SysTick (3) temperature sensor (11) Thermometer (21) Timer/Counter (31) TM1637 (2) UART (7) Ultrasonic (4) Voltmeter (7) WDT (1) XC16 (2) XC8 (94)