📜 ⬆️ ⬇️

Connecting oled display with controller SSD1306 to STM32 via I2C

Many probably know about such small cheap (less than $ 3) OLED displays, which can be found in a huge range on ebay or aliexpress. There are many different articles on the Internet on how to connect these displays to Arduino and other MCs, but for the STM32f10x it is difficult to find even a library. Therefore, I decided to write this article.

This display has a resolution of 128x64 pixels and the controller SSD1306 and is connected to the microcontroller via the I2C interface.



For STM32, a library was found for this display , but it was for the f4xx series - it was necessary to modify it for f10x.
')
Source files of the library modified by me can be found here .
ssd1306_i2c.c
ssd1306_i2c.h
Interface for working with I2C
ssd1306.c
ssd1306.h
Library to work with the display. Represents methods for drawing on the display, outputting text, and outputting everything to oled.
fonts.c
fonts.h
Fonts for displaying text on the screen. There are three fonts, but you can create any of your own using this program or analogs.

The wiring diagram is extremely simple:
Vcc+ 3.3V. Allowable voltage - from 3.3V to 5V
GNDGND
SCLPB6
SDAPB7

image

To work with the library you need to connect the header file:

#include "ssd1306.h" 

And before use, initialize:

 SSD1306_Init(); 

Now you can draw something:

 SSD1306_GotoXY(0, 44); //    0;44.   ,  . SSD1306_Puts("Hello, habrahabr!!", &Font_7x10, SSD1306_COLOR_WHITE); //      "Font_7x10"  . SSD1306_DrawCircle(10, 33, 7, SSD1306_COLOR_WHITE); //     10;33   7  

Everything that we drew now is in the buffer in the RAM of the MK, in order to display everything on the display it is necessary to call:

 SSD1306_UpdateScreen(); 

After that, our display will be updated and will display an inscription and a circle. After calling SSD1306_UpdateScreen (), the buffer in the MC is not reset by itself, so new pictures will be on top of the previous ones, for reset, you can fill everything in black:

 SSD1306_Fill(SSD1306_COLOR_BLACK); 

All library functions:

 uint8_t SSD1306_Init(); // SSD1306_UpdateScreen(); //       SSD1306_ToggleInvert(); //      SSD1306_Fill(SSD1306_COLOR_t Color); //    SSD1306_DrawPixel(uint16_t x, uint16_t y, SSD1306_COLOR_t color); //   SSD1306_GotoXY(uint16_t x, uint16_t y); //    SSD1306_Putc(char ch, FontDef_t* Font, SSD1306_COLOR_t color); //  h    SSD1306_Puts(char* str, FontDef_t* Font, SSD1306_COLOR_t color); //  str    SSD1306_DrawLine(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, SSD1306_COLOR_t c); //  SSD1306_DrawRectangle(uint16_t x, uint16_t y, uint16_t w, uint16_t h, SSD1306_COLOR_t c); //  SSD1306_DrawFilledRectangle(uint16_t x, uint16_t y, uint16_t w, uint16_t h, SSD1306_COLOR_t c); //  SSD1306_DrawTriangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t x3, uint16_t y3, SSD1306_COLOR_t color); // SSD1306_DrawCircle(int16_t x0, int16_t y0, int16_t r, SSD1306_COLOR_t c); //  r SSD1306_DrawFilledCircle(int16_t x0, int16_t y0, int16_t r, SSD1306_COLOR_t c); //  

Available fonts, but you can also add your own, including Russian ones:


The display works pretty quickly (FPS around 14-18) at I2C 400kHz (450 is also no problem, but did not risk, but slows down at 500) without any problems.

Used CooCox IDE. The finished project can be downloaded here: Yandex.Disk .

PS Since the writing of the article and before it was published from the sandbox, quite a lot of time (6 months) has passed, during which I managed to change the library several times.

A newer version of the library with DMA support and a test project for Keil and cubeMx can be found here . You can find the latest version of the library here .

Example of the library:



I am pleased to answer your questions! Good luck!

Source: https://habr.com/ru/post/313490/


All Articles