OLED Interface With 8051 - JohnHau/mis GitHub Wiki
https://exploreembedded.com/wiki/OLED_Interface_With_8051
In earlier tutorials, we discussed the interfacing of LCD and GLCD. In this tutorial, we will see how to interfaces an I2C OLED(128x64) with 8051. There are plenty of well written arduino oled libraries(AdaFruit SSD1306, rinkydinkelectronics) for displaying strings, numbers, and logos. They all use a shadow ram buffer of 1024 bytes(128x64 pixels) and support different fonts and graphics. As 8051 has only 128 bytes of RAM we cannot use the shadow RAM buffer and cannot support all the fonts and graphics. First, we will see the frame format for sending the commands and data to OLED. After this, we will discuss few of the OLED commands. Finally, we will be displaying string, numbers on OLED. Displaying of logos/icons will be discussed in the separate tutorial.
void OLED_Init(void) { oledSendCommand(SSD1306_DISPLAY_OFF); oledSendCommand(SSD1306_SET_DISPLAY_CLOCK_DIV_RATIO); oledSendCommand(0x80); oledSendCommand(SSD1306_SET_MULTIPLEX_RATIO); oledSendCommand(0x3F); oledSendCommand(SSD1306_SET_DISPLAY_OFFSET); oledSendCommand(0x0); oledSendCommand(SSD1306_SET_START_LINE | 0x0); oledSendCommand(SSD1306_CHARGE_PUMP); oledSendCommand(0x14); oledSendCommand(SSD1306_MEMORY_ADDR_MODE); oledSendCommand(0x00); oledSendCommand(SSD1306_SET_SEGMENT_REMAP | 0x1); oledSendCommand(SSD1306_COM_SCAN_DIR_DEC); oledSendCommand(SSD1306_SET_COM_PINS); oledSendCommand(0x12); oledSendCommand(SSD1306_SET_CONTRAST_CONTROL); oledSendCommand(0xCF); oledSendCommand(SSD1306_SET_PRECHARGE_PERIOD); oledSendCommand(0xF1); oledSendCommand(SSD1306_SET_VCOM_DESELECT); oledSendCommand(0x40); oledSendCommand(SSD1306_DISPLAY_ALL_ON_RESUME); oledSendCommand(SSD1306_NORMAL_DISPLAY); oledSendCommand(SSD1306_DISPLAY_ON);
OLED_Clear(); /* Clear the complete LCD during init */
}
Example Code Below is sample code to display the Strings at different cursor positions. /*************************************************************************************************** ExploreEmbedded Copyright Notice
- File: main.c
- Version: 16.0
- Author: ExploreEmbedded
- Website: http://www.exploreembedded.com/wiki
- Description: sample program to demonstrate the usage of SSD1306 I2C OLED library to display string, numbers, graphs and logos This code has been developed and tested on ExploreEmbedded boards. We strongly believe that the library works on any of development boards for respective controllers. Check this link http://www.exploreembedded.com/wiki for awesome tutorials on 8051,PIC,AVR,ARM,Robotics,RTOS,IOT. ExploreEmbedded invests substantial time and effort developing open source HW and SW tools, to support consider buying the ExploreEmbedded boards. The ExploreEmbedded libraries and examples are licensed under the terms of the new-bsd license(two-clause bsd license). See also: http://www.opensource.org/licenses/bsd-license.php EXPLOREEMBEDDED DISCLAIMS ANY KIND OF HARDWARE FAILURE RESULTING OUT OF USAGE OF LIBRARIES, DIRECTLY OR INDIRECTLY. FILES MAY BE SUBJECT TO CHANGE WITHOUT PRIOR NOTICE. THE REVISION HISTORY CONTAINS THE INFORMATION RELATED TO UPDATES. Permission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is hereby granted, provided that this copyright notices appear in all copies and that both those copyright notices and this permission notice appear in supporting documentation. **************************************************************************************************/ #include "oled_i2c.h"
int main() { OLED_Init(); OLED_SetCursor(2,0); //Set cursor at 2nd-line 0th-Position OLED_DisplayString("Oled Interface");
OLED_SetCursor(4,20); //Set cursor at 4th-line 20th-Position
OLED_DisplayString("With 8051 From");
OLED_SetCursor(6,30); //Set cursor at 6th-line 30th-Position
OLED_DisplayString("Exploreembedded");
while(1);
}