HLK-W806 (VIII): 4-wire SPI driver for SSD1306/SSD1315 128x64 OLED LCD screen

Publisher:会哭的蓝精灵Latest update time:2022-06-20 Source: eefocusKeywords:driver  SSD1306  128x64 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

SSD1306/SSD1315 OLED

0.96 128x64 OLED LCD screen is the most common screen in embedded applications. There are two common pin types for this screen: I2C and SPI. The driver is usually SSD1306, and recently some are SSD1315.


I2C type OLED has only 4 pins, the connection mode cannot be modified, and it is easier to identify

SPI type OLED has 6/7/8 pin types. The most common one is 7 pin, a module with protocol options. The back PCB of this module will have three mode selection instructions. The Rx listed at the back represents the positions where resistors need to be soldered. Usually these resistors are 5KR. The default factory mode is 4-wire SPI.

In SPI mode, the drive mode of SSD1306 and SSD1315 is the same. Because W806 can use hardware SPI, the refresh speed is nearly 10 times faster than I2C mode. The following is the drive mode of 4-wire SPI.


connect

Although it is a 4-wire SPI, all 7 wires need to be connected. The 4 wires do not include VCC, GND and Reset. The connection method for this demonstration is as follows


PB14 -> CS

PB15 -> SCK/CLK/D0

PB17 -> MOSI/SDA/D1

PB10 -> RES (Reset)

PB11 -> DC

GND -> GND

3.3V -> VCC

Library file

This example is improved from the I2C example, adding the SPI transmission method and updating the demonstration function. This library file supports


Draw a dot

Line drawing

Draw a circle

Output text (soft font library required)

Drawing

use

When driving SSD1306/SSD1315, the files that need to be imported are:


ssd1306.c

ssd1306.h

ssd1306_fonts.c

ssd1306_fonts.h

Add the header file to main.c and initialize SPI_HandleTypeDef


#include "ssd1306.h"


SPI_HandleTypeDef hspi;


static void SPI_Init(void)

{

    hspi.Instance = SPI;

    hspi.Init.Mode = SPI_MODE_MASTER;

    hspi.Init.CLKPolarity = SPI_POLARITY_LOW;

    hspi.Init.CLKPhase = SPI_PHASE_1EDGE;

    hspi.Init.NSS = SPI_NSS_SOFT;

    hspi.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_40;

    hspi.Init.FirstByte = SPI_LITTLEENDIAN;


    if (HAL_SPI_Init(&hspi) != HAL_OK)

    {

        Error_Handler();

    }

}


static void GPIO_Init(void)

{

    GPIO_InitTypeDef GPIO_InitStruct = {0};

    __HAL_RCC_GPIO_CLK_ENABLE();

    GPIO_InitStruct.Pin = SSD1306_RES_PIN;

    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT;

    GPIO_InitStruct.Pull = GPIO_NOPULL;

    HAL_GPIO_Init(SSD1306_RES_PORT, &GPIO_InitStruct);

    GPIO_InitStruct.Pin = SSD1306_DC_PIN;

    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT;

    GPIO_InitStruct.Pull = GPIO_NOPULL;

    HAL_GPIO_Init(SSD1306_DC_PORT, &GPIO_InitStruct);

}


In ssd1306.h, confirm that the mode is set to SPI


/**

 * Mode switch: 0 - SPI, 1 - I2C

 */

#define SSD1306_MODE_I2C 0


Modify the parameters corresponding to the current screen: width, height


/* SSD1306 settings */

/* SSD1306 width in pixels */

#ifndef SSD1306_WIDTH

#define SSD1306_WIDTH 128

#endif

/* SSD1306 LCD height in pixels */

#ifndef SSD1306_HEIGHT

#define SSD1306_HEIGHT 64

#endif


Then you can use it in your code


# Initialization

GPIO_Init();

SPI_Init();

uint8_t res = SSD1306_Init();

printf("OLED init: %dn", res);

# Locate the starting point of the drawing

SSD1306_GotoXY(5, 5);

# Output characters

SSD1306_Puts("OLED:11x18", &Font_11x18, 1);

# Update the display

SSD1306_UpdateScreen(); // display


Clear screen


SSD1306_Fill(0); // clear oled


Line drawing


int y1 = 64, y2 = 0;

while (y1 > 0)

{

    SSD1306_DrawLine(0, y1, 128, y2, 1);

    SSD1306_UpdateScreen();

    y1 -= 2;

    y2 += 2;

}


Fill with inverted color, draw a circle


SSD1306_Fill(1); // clear oled

SSD1306_UpdateScreen();

SSD1306_DrawCircle(64, 32, 25, 0);

SSD1306_UpdateScreen();

SSD1306_DrawCircle(128, 32, 25, 0);

SSD1306_UpdateScreen();

SSD1306_DrawCircle(0, 32, 25, 0);

SSD1306_UpdateScreen();

SSD1306_DrawCircle(32, 32, 25, 0);

SSD1306_UpdateScreen();

SSD1306_DrawCircle(96, 32, 25, 0);

SSD1306_UpdateScreen();


Related code

The code has been updated to the SDK's I2C example (supports both I2C and SPI)


Github: wm-sdk-w806/tree/main/demo/i2c/ssd1306_oled

Gitee: wm-sdk-w806/tree/main/demo/i2c/ssd1306_oled


refer to

OLED displays (SSD1306 and SSD1315) http://cholla.mmto.org/electronics/displays/oled/


Keywords:driver  SSD1306  128x64 Reference address:HLK-W806 (VIII): 4-wire SPI driver for SSD1306/SSD1315 128x64 OLED LCD screen

Previous article:HLK-W806 (Part 9): Software SPI and hardware SPI driver for ST7789V LCD
Next article:Lianshengde HLK-W806 (VII): Compatible with development board LuatOS Air103

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号