之前早早买了这个东西,但是一直没时间(因为没调通 ),在网上搜了一些资料。感觉差强人意,为什么总有人直接甩代码,看见那种文章都让我有种冲动
步骤如下
- 0.96寸OLED组成 - SSD1306芯片
- 0.96寸OLED通信 - SPI
- 0.96寸OLED控制 - 写数据/写命令等
- STM32代码实现
0.96寸OLED组成 - SSD1306芯片
可以从我们购买的显示屏原理图之类的看到使用的芯片SSD1306
SSD1306芯片手册解读
Pin selectable MCU Interfaces:
- 8-bit 6800/8080-series parallel interface
- 3 /4 wire Serial Peripheral Interface
- I2 C Interface
就是支持这些通信,咱们主要使用SPI进行控制。
SPI 4 线
相比平时用的spi只是多了一个 数据/命令 控制引脚,毕竟片选引脚也是软件控制的,一起控制就好了。
SPI 3 线
这里将 数据/命令 控制引脚加到了spi通信的数据中,数据位多了一位。
移植代码
移植是最最最省事快速的事情了,甚至你都不需要懂什么,只需要知道什么通信方式即可。
stm32-ssd1306 github 工程https://github.com/afiskon/stm32-ssd1306
将文件加到工程中,在h文件中进行配置,这里是hal的,所以请注意看下下面配置如何配置即可
#define STM32F4
#define SSD1306_USE_SPI
#if defined(STM32F0)
#include "stm32f0xx_hal.h"
#elif defined(STM32F1)
#include "stm32f1xx_hal.h"
#elif defined(STM32F4)
#include "stm32f4xx_hal.h"
#include "stm32f4xx_hal_gpio.h"
#elif defined(STM32L0)
#include "stm32l0xx_hal.h"
#elif defined(STM32L4)
#include "stm32l4xx_hal.h"
#elif defined(STM32F3)
#include "stm32f3xx_hal.h"
#elif defined(STM32H7)
#include "stm32h7xx_hal.h"
#elif defined(STM32F7)
#include "stm32f7xx_hal.h"
#else
#error "SSD1306 library was tested only on STM32F1, STM32F3, STM32F4, STM32F7, STM32L0, STM32L4, STM32H7 MCU families. Please modify ssd1306.h if you know what you are doing. Also please send a pull request if it turns out the library works on other MCU's as well!"
#endif
#include "ssd1306_fonts.h"
/* vvv I2C config vvv */
#ifndef SSD1306_I2C_PORT
#define SSD1306_I2C_PORT hi2c1
#endif
#ifndef SSD1306_I2C_ADDR
#define SSD1306_I2C_ADDR (0x3C << 1)
#endif
/* ^^^ I2C config ^^^ */
/* vvv SPI config vvv */
#ifndef SSD1306_SPI_PORT
#define SSD1306_SPI_PORT hspi1
#endif
#ifndef SSD1306_CS_Port
#define SSD1306_CS_Port GPIOA
#endif
#ifndef SSD1306_CS_Pin
#define SSD1306_CS_Pin GPIO_PIN_4
#endif
#ifndef SSD1306_DC_Port
#define SSD1306_DC_Port GPIOB
#endif
#ifndef SSD1306_DC_Pin
#define SSD1306_DC_Pin GPIO_PIN_2
#endif
#ifndef SSD1306_Reset_Port
#define SSD1306_Reset_Port GPIOB
#endif
#ifndef SSD1306_Reset_Pin
#define SSD1306_Reset_Pin GPIO_PIN_1
#endif
/* ^^^ SPI config ^^^ */
#if defined(SSD1306_USE_I2C)
extern I2C_HandleTypeDef SSD1306_I2C_PORT;
#elif defined(SSD1306_USE_SPI)
extern SPI_HandleTypeDef SSD1306_SPI_PORT;
#else
#error "You should define SSD1306_USE_SPI or SSD1306_USE_I2C macro!"
#endif
// SSD1306 OLED height in pixels
#ifndef SSD1306_HEIGHT
#define SSD1306_HEIGHT 64
#endif
// SSD1306 width in pixels
#ifndef SSD1306_WIDTH
#define SSD1306_WIDTH 128
#endif
// some LEDs don't display anything in first two columns
// #define SSD1306_WIDTH 130
配置完可以进行测试
while (1)
{
ssd1306_TestAll();
HAL_Delay(100);
}