联盛德 HLK-W806 (十一): 软件SPI和硬件SPI驱动ST7567液晶LCD

目录

ST7567介绍

ST7567是128x64分辨率单色LCD液晶显示屏常用的芯片方案. ST7567是点阵LCD驱动集成电路, 可以使用8位并口或4线SPI串口(SPI-4)直接连接外部微控制器. 从MPU传输的数据存储在内部65x132 bits的显示数据内存(DDRAM). 在 DDRAM 中存储的数据与LCD点阵直接关联. ST7567 包含 132 段输出, 64 位输出和 1 个公共图标输出, 输出不需要外部时钟驱动.

特性

单片驱动 Single-chip LCD Controller & Driver, 片内显示内存 On-chip Display Data RAM (DDRAM)

  • 容量Capacity: 65x132=8580 bits
  • 直接显示模式 Directly display RAM pattern from DDRAM

可选的显示占空比 Selectable Display Duty (by SEL2 & SEL1)

  • 1/65 duty : 65 common x 132 segment
  • 1/55 duty : 55 common x 132 segment
  • 1/49 duty : 49 common x 132 segment
  • 1/33 duty : 33 common x 132 segment

微控制器接口 Microprocessor Interface

  • 双向8位并口Bidirectional 8-bit parallel interface supports: 8080-series and 6800-series MPU
  • SPI-4串口(只写) Serial interface (SPI-4) is also supported (write only)

附加功能 Abundant Functions

  • 显示开关 Display ON/OFF,
  • 正显/反显 Normal/Reverse Display Mode,
  • 设置显示开始位置 Set Display Start Line,
  • 读取IC状态 Read IC Status,
  • 显示全部点阵 Set all Display Points ON,
  • 设置LCD基准 Set LCD Bias,
  • 电量控制 Electronic Volume Control,
  • 写入后保持起点 Read-modify-Write,
  • 段驱动方向设置 Select Segment Driver Direction,
  • 节电模式 Power Saving Mode,
  • 位驱动方向设置 Select Common Driver Direction,
  • 电压调节电阻比值设置 Select Voltage Regulator Resistor Ratio (for V0).

外部硬件复位脚 External Hardware Reset Pin (RSTB)

内建振荡信号源 Built-in Oscillation Circuit

  • 无需外部振荡源 No external component required

低功耗模拟电路 Low Power Consumption Analog Circuit

  • Voltage Booster (4X, 5X)
  • High-accuracy Voltage Regulator for LCD Vop: (Thermal Gradient: -0.05%/°C)
  • Voltage Follower for LCD Bias Voltage

Wide Operation Voltage Range

  • VDD1-VSS1=1.8V~3.3V
  • VDD2-VSS2=2.4V~3.3V
  • VDD3-VSS3=2.4V~3.3V

温度范围 Temperature Range: -30~85°C
封装类型 Package Type: COG

指令表

命令模式和数据模式的切换

  • A0低: 命令模式
  • A0高: 数据模式

指令列表

  1. Display ON/OFF
    AF:ON, AE:OFF
  2. Set Start Line
    0 0 0 1 S5 S4 S3 S2 S1 S0 Set display start line
  3. Set Page Address
    0 0 1 0 1 1 Y3 Y2 Y1 Y0 Set page address
  4. Set Column Address
    0 0 0 0 0 1 X7 X6 X5 X4 Set column address (MSB)
    0 0 0 0 0 0 X3 X2 X1 X0 Set column address (LSB)
  5. Read Status: Read IC Status
    0 1 0 MX D RST 0 0 0 0
  6. Write Data: Write display data to RAM
    1 0 D7 D6 D5 D4 D3 D2 D1 D0
  7. Read Data: Read display data from RAM
    1 1 D7 D6 D5 D4 D3 D2 D1 D0
  8. SEG Direction: Set scan direction of SEG
    0 0 1 0 1 0 0 0 0 MX
    MX=1, reverse direction
    MX=0, normal direction
  9. Inverse Display: INV =1, inverse display, INV =0, normal display
    0 0 1 0 1 0 0 1 1 INV
  10. All Pixel ON: AP=1, set all pixel ON, AP=0, normal display
    0 0 1 0 1 0 0 1 0 AP
  11. Bias Select: Select bias setting, 0=1/9; 1=1/7 (at 1/65 duty)
    0 0 1 0 1 0 0 0 1 BS
  12. Read-modify-Write: Column address increment: Read:+0 , Write:+1
    0 0 1 1 1 0 0 0 0 0
  13. END: Exit Read-modify-Write mode
    0 0 1 1 1 0 1 1 1 0
  14. RESET: Software reset
    0 0 1 1 1 0 0 0 1 0
  15. COM Direction: Set output direction of COM, MY=1, reverse direction, MY=0, normal direction
    0 0 1 1 0 0 MY - - -
  16. Power Control, Control built-in power circuit ON/OFF
    0 0 0 0 1 0 1 VB VR VF
  17. Regulation Ratio: Select regulation resistor ratio
    0 0 0 0 1 0 0 RR2 RR1 RR0
  18. Set EV: Double command, Set EV0 electronic volume (EV) level
    0 0 1 0 0 0 0 0 0 1
    0 0 0 0 EV5 EV4 EV3 EV2 EV1 EV0
  19. Set Booster: Double command, Set booster level: BL=0: 4X, BL=1: 5X
    0 0 1 1 1 1 1 0 0 0
    0 0 0 0 0 0 0 0 0 BL
  20. Power Save: Display OFF + All Pixel ON
    0 0 + Compound Command
  21. NOP: No operation
    0 0 1 1 1 0 0 0 1 1
  22. Test: Do NOT use. Reserved for testing.
    0 0 1 1 1 1 1 1 1 -

ST7567 128x64 LCD 模块

模块规格图

PIN脚定义

  1. NC
  2. NC
  3. LED_A 背光正极, 加1~5K电阻后接VCC, 或连接GPIO以便程序控制背光
  4. LED_K 背光负极, 接地
  5. GND 接地
  6. CSB 片选, CS
  7. RESET 复位脚, 接GPIO
  8. AO 命令/数据切换
  9. SCLK SPI时钟
  10. SDA SPI MOSI
  11. VDD 接VCC
  12. VSS 逻辑地, 接地
  13. VO: V0 is the LCD driving voltage for common circuits at negative frame.
  14. XVO: XV0 is the LCD driving voltage for common circuits at positive frame.
  15. VG: VG is the LCD driving voltage for segment circuits.
  16. GND 接地
  17. NC
  18. NC

接线

完整需要10根接线, 其中LED_A可以加电阻后直接接3.3V, RESET可以悬空, 这样只需要接8根线.

3. LED_A 加1~5K电阻 -> PB16
4. LED_K           -> GND
5. GND             -> GND
6. CSB             -> PB14
7. RESET           -> PB10
8. AO              -> PB11
9. SCLK            -> PB15
10. SDA            -> PB17
11. VDD            -> 3.3V
12. VSS            -> GND

演示代码的使用

SPI驱动部分与SSD1306相同, 指令很少, 只有22个, 与SSD1306相比, 增加了背光控制, 增加了硬件复位.

本示例使用的代码

指令定义

/* ST7567 commands definitions */
#define ST7567_DISPLAY_OFF                   0xAE /* 0xae: Display OFF (sleep mode) */
#define ST7567_DISPLAY_ON                    0xAF /* 0xaf: Display ON in normal mode */

#define ST7567_SET_START_LINE                0x40 /* 0x40-7f: Set display start line */
#define  ST7567_SET_START_LINE_MASK          0x3f

#define ST7567_SET_PAGE_ADDRESS              0xB0 /* 0xb0-b7: Set page start address */
#define  ST7567_SET_PAGE_ADDRESS_MASK        0x07

#define ST7567_SET_COLUMN_ADDRESS_MSB        0x10 /* 0x10-0x1f: Set higher column address */
#define ST7567_SET_COLUMN_ADDRESS_MSB_MASK   0x0f

#define ST7567_SET_COLUMN_ADDRESS_LSB        0x00 /* 0x00-0x0f: Set lower column address */
#define  ST7567_SET_COLUMN_ADDRESS_LSB_MASK  0x0F

/**
 * SEG: 0 - 131
*/
#define ST7567_SEG_DIRECTION_NORMAL          0xA0 /* 0xa0: Column address 0 is mapped to SEG0 */
#define ST7567_SEG_DIRECTION_REVERSE         0xA1 /* 0xa1: Column address 128 is mapped to SEG0 */

/**
 * COM: 0 - 63
*/
#define ST7567_COM_DIRECTION_NORMAL          0xC0 /* 0xc0: Set COM output direction, normal mode */
#define ST7567_COM_DIRECTION_REVERSE         0xC8 /* 0xc8: Set COM output direction, reverse mode */

#define ST7567_INVERSE_DISPLAY_OFF           0xA6 /* 0xa6: Normal display */
#define ST7567_INVERSE_DISPLAY_ON            0xA7 /* 0xa7: Inverse display */

#define ST7567_ALL_PIXEL_ON                  0xA5 /* 0xa5: Entire display ON */
#define ST7567_ALL_PIXEL_NORMAL              0xA4 /* 0xa4: Resume to RAM content display */

#define ST7567_BIAS_1_9                      0xA2 /* 0xa2: Select BIAS setting 1/9 */
#define ST7567_BIAS_1_7                      0xA3 /* 0xa3: Select BIAS setting 1/7 */

#define ST7567_READ_MODIFY_WRITE_START       0xE0 /* 0xe0: Enter the Read Modify Write mode */
#define ST7567_READ_MODIFY_WRITE_END         0xEE /* 0xee: Leave the Read Modify Write mode */
#define ST7567_RESET                         0xE2 /* 0xe2: Software RESET */

/**
 * This instruction controls the built-in power circuits. 
 * Typically, these 3 flags are turned ON at the same time.
*/
#define ST7567_POWER_CONTROL                 0x28
#define ST7567_POWER_CONTROL_VF              0x01
#define ST7567_POWER_CONTROL_VR              0x02
#define ST7567_POWER_CONTROL_VB              0x04

/**
 * The operation voltage (V0) calculation formula is shown below: 
 * (RR comes from Regulation Ratio, EV comes from EV[5:0])
 * V0 = RR X [ 1 – (63 – EV) / 162 ] X 2.1, or 
 * V0 = RR X [ ( 99 + EV ) / 162 ] X 2.1
*/
#define ST7567_REGULATION_RATIO              0x20
#define ST7567_REGULATION_RATIO_3_0          0x00
#define ST7567_REGULATION_RATIO_3_5          0x01
#define ST7567_REGULATION_RATIO_4_0          0x02
#define ST7567_REGULATION_RATIO_4_5          0x03
#define ST7567_REGULATION_RATIO_5_0          0x04 /* Default */
#define ST7567_REGULATION_RATIO_5_5          0x05
#define ST7567_REGULATION_RATIO_6_0          0x06
#define ST7567_REGULATION_RATIO_6_5          0x07

/**
 * This is double byte instruction. The first byte set ST7567 into EV 
 * adjust mode and the following instruction will change the EV setting. 
 * That means these 2 bytes must be used together. They control the electronic 
 * volume to adjust a suitable V0 voltage for the LCD.
*/
#define ST7567_SET_EV                        0x81
#define ST7567_SET_EV_MASK                   0x3F

#define ST7567_SET_BOOSTER                   0xF8 /* Set booster level */
#define ST7567_SET_BOOSTER_4X                0x00
#define ST7567_SET_BOOSTER_5X                0x01

#define ST7567_NOP                           0xE3
#define ST7567_TEST                          0xFE

显示内存和偏移量

ST7567的显示与内存并非一一对应, 每一行为132byte, 这额外的4个byte并不会显示在屏幕上, 但是会占位.

/**
 * In datasheet, it says "the column address is increased (+1) after each display 
 * data access (read/write). This allows MPU accessing DDRAM content continuously. 
 * This feature stops at the end of each page (Column Address “83h”) because the 
 * Column Address and Page Address circuits are independent. For example, both Page 
 * Address and Column Address should be assigned for changing the DDRAM pointer 
 * from (Page-0, Column-83h) to (Page-1, Column-0)."
 * In actual test the Page Address will grow automatically.
*/
/* ST7567 data buffer */
static uint8_t ST7567_Buffer_all[(ST7567_WIDTH + ST7567_SEG_EXPAND) * ST7567_HEIGHT / 8];

在行方向翻转后, 需要在代码中考虑这4个byte的偏移量, 否则显示会不完整.

#if ST7567_X_ORIENT == ST7567_SEG_DIRECTION_REVERSE
    #define ST7567_X_OFFSET  ST7567_SEG_EXPAND
#else
    #define ST7567_X_OFFSET  0
#endif

// 基础方法: 在指定的位置绘制点
void ST7567_DrawPixel(uint16_t x, uint16_t y, uint8_t color)
{
    if (x >= ST7567_WIDTH || y >= ST7567_HEIGHT)
    {
        /* Error */
        return;
    }

    if (color == ST7567_COLOR_FRONT)
    {
        ST7567_Buffer_all[ST7567_X_OFFSET + x + (y / 8) * (ST7567_WIDTH + ST7567_SEG_EXPAND)] |= 1 << (y % 8);
    }
    else
    {
        ST7567_Buffer_all[ST7567_X_OFFSET + x + (y / 8) * (ST7567_WIDTH + ST7567_SEG_EXPAND)] &= ~(1 << (y % 8));
    }
}

设置项

在 st7567.h 中, 对以下宏定义进行设置. 如果只是运行demo, 建议先用默认配置跑通. 如果对pin脚进行了修改, 对应的也要调整连线.

// 设置是否使用硬件SPI
#define ST7567_HARDWARE_SPI     1

// 设置PIN脚
// CS: B4, B14
#define ST7567_CS_PORT      GPIOB
#define ST7567_CS_PIN       GPIO_PIN_14
// SCK: B1, B2, B15, B24
#define ST7567_SCK_PORT     GPIOB
#define ST7567_SCK_PIN      GPIO_PIN_15
// MOSI: B5, B17, B26, PA7
#define ST7567_MOSI_PORT    GPIOB
#define ST7567_MOSI_PIN     GPIO_PIN_17
// MISO: B0, B3, B16, B25

#define ST7567_RES_PORT     GPIOB
#define ST7567_RES_PIN      GPIO_PIN_10
#define ST7567_DC_PORT      GPIOB
#define ST7567_DC_PIN       GPIO_PIN_11
#define ST7567_BL_PORT      GPIOB
#define ST7567_BL_PIN       GPIO_PIN_16

// 设置屏幕的宽
// X width
#define ST7567_WIDTH  128
// 设置屏幕的高
// Y height
#define ST7567_HEIGHT 64
// 设置每行的附加字节数, 默认为4
// Additional bytes in each row
#define ST7567_SEG_EXPAND 4
// 行方向
// X orientation
#define ST7567_X_ORIENT ST7567_SEG_DIRECTION_REVERSE
// 列方向
// Y orientation
#define ST7567_Y_ORIENT ST7567_COM_DIRECTION_NORMAL

st7567.c 中的LCD初始化方法, 这里的参数如果不适合你的LCD, 需要根据手册调整.

有几处修改后容易导致不显示的选项, 需要谨慎修改

  • ST7567_POWER_CONTROL 这个是升压设置, 当前这块屏幕, 只有三个位都置1时, 屏幕才有显示, 其它选项都不行
  • ST7567_SET_EV 这个是对比度电压设置, 范围为0 - 0x3F, 太低(< 0x10)会显示全空白, 太高(> 0x32)会显示为全黑
  • ST7567_ALL_PIXEL_NORMAL 如果设置为 ST7567_ALL_PIXEL_ON 可能并非全黑, 而是全空白. 未跑通时建议用NORMAL
  • ST7567_SET_PAGE_ADDRESS 和 ST7567_SET_COLUMN_ADDRESS_MSB, ST7567_SET_COLUMN_ADDRESS_LSB 用于设置显示的开始行和列位置, 需要初始化到原点.
void ST7567_Init(void)
{
    ST7567_Reset();
    ST7567_BackLight_On();

    ST7567_WriteCommand(ST7567_RESET);
    ST7567_WriteCommand(ST7567_POWER_CONTROL
        |ST7567_POWER_CONTROL_VB
        |ST7567_POWER_CONTROL_VR
        |ST7567_POWER_CONTROL_VF); 
    ST7567_WriteCommand(ST7567_SET_EV);
    ST7567_WriteCommand(ST7567_SET_EV_MASK & 0x20);
    ST7567_WriteCommand(ST7567_BIAS_1_9);
    ST7567_WriteCommand(ST7567_X_ORIENT);
    ST7567_WriteCommand(ST7567_Y_ORIENT);
    ST7567_WriteCommand(ST7567_REGULATION_RATIO | ST7567_REGULATION_RATIO_5_0);
    ST7567_WriteCommand(ST7567_INVERSE_DISPLAY_OFF);
    ST7567_WriteCommand(ST7567_DISPLAY_ON);
    ST7567_WriteCommand(ST7567_ALL_PIXEL_NORMAL);

    ST7567_WriteCommand(ST7567_SET_START_LINE | (0x00 & ST7567_SET_START_LINE_MASK));
    ST7567_WriteCommand(ST7567_SET_PAGE_ADDRESS | (0x00 & ST7567_SET_PAGE_ADDRESS_MASK));
    ST7567_WriteCommand(ST7567_SET_COLUMN_ADDRESS_MSB);
    ST7567_WriteCommand(ST7567_SET_COLUMN_ADDRESS_LSB);
}

图形绘制

图形绘制的方法与SSD1306一致, 可以绘制点, 线, 圈, 文字, 反显, 调节对比度, 开关背光

int main(void)
{
    //...
    
    // 绘制直线
    ST7567_DrawLine(0,   0, 127,  0, 1);
    ST7567_DrawLine(0,   0,   0, 63, 1);
    ST7567_DrawLine(127, 0, 127, 63, 1);
    ST7567_DrawLine(0,  63, 127, 63, 1);

    // 绘制文字
    ST7567_Puts("LCD:ST7567", &Font_6x12, 1);
    
    // 绘制后, 需要使用这个方法刷新LCD显示
    ST7567_UpdateScreen();

    // 设置对比度
    while (y1 <= 0x30)
    {
        ST7567_SetContrast(y1++);
        HAL_Delay(100);
    }
    
    // 反显
    ST7567_ToggleInvert();
    ST7567_UpdateScreen();
    
    // 绘制圆
    ST7567_DrawCircle(64, 32, 25, 0);
    ST7567_UpdateScreen();

    //...
}

实际演示展示

B站视频: https://www.bilibili.com/video/BV1VZ4y1X7p7

LCD液晶屏的延迟很高, 如果变化太快, 每秒帧数在七八帧以上基本就看不清了. 这点不如OLED屏幕.

posted on 2021-12-19 22:36  Milton  阅读(7966)  评论(2编辑  收藏  举报

导航