Using STC32F to drive OLED SSD1306

Test OLED LCD module

OLED显示器
目 录
Contents
前言
编写程序
网络资料
基于51的驱动程序
总 结

 

01 OLED display


I. Introduction

  There is an OLED with I2C interface at hand, and the STC32F single-chip microcomputer is used to program and test it. The experimental circuit is built on a breadboard, mainly to obtain the corresponding application subroutine and lay the foundation for future applications.

GM1688719319_1280_800.MPG|_-5

2. Write the program

1. Scan address

  A test circuit is built on a breadboard, using P1.5 and P1.4 of STC32F to output I2C clock and data lines respectively, and connected to OLED SSD1306 to form a test circuit.

GM1688201421_1280_800.MPG|_-3
  First, use the I2C bus to scan to obtain the I2C address of the OLED. The scanning address starts from 1 and goes to 0x7F. Send a write command, and determine whether to return the corresponding 7-bit address by checking whether an ACK response is obtained. If not scanned, finally return 0x80 . According to the sample program about SSD1306 on the network, the I2C address of SSD1306 should be 0x3C .
GM1688201680_1280_800.MPG|_-6
  In the main program, scan once every 0.25 seconds, and finally get an I2C device address 0x3C , which is the address of SSD1306.
GM1688201787_1280_800.MPG|_-2

2. Software testing

  Next, realize the control of OLED1306 through software programming. Many subroutines are initialization programs written by others found on the Internet, and are controlled by the I2C bus. By comparison, the display obtained by this group of OLED initialization commands is the clearest. The intermediate results of the display are given here, and the dot matrix information corresponding to the OLED display corresponds to the 8 × 128 byte RAM buffer in the memory. The time required for the output buffer to be tested by the oscilloscope is given here. 44.7 milliseconds.

GM1688718569_1280_800.MPG|_-9

▲ Figure 1.2.1 The output buffer is lingering

▲ 图1.2.1 输出缓冲区是缠绵

  In order to shorten the time for outputting the OLED buffer, the speed of the I2C bus is modified. Here, the original initialized 0xff is changed to 0xc7, and the speed should be increased by 8 times. This is tested experimentally, if the speed continues to increase, the OLED cannot be driven. At this speed, the OLED can still be driven by I2C. The corresponding output time is reduced to 7.5ms. Greatly improved OLED refresh rate.

GM1688718942_1280_800.MPG|_-5

 

02 Network information


1. 51-based driver

/**********************************************************************************
 * 程序名:    SSD1306_IIC驱动程序
 * 作者:      DaveoCKII
 * 日期:      2020.7.21
 * 版本:      STC12C5A60S2
 **********************************************************************************/

typedef unsigned char u8;
//------------------------------------定义列表------------------------------------//
sbit SCL = P3^4;    // 时钟线
sbit SDA = P3^3;    // 数据线
//------------------------------------函数列表------------------------------------//

void OLED_Init(void);            // OLED 初始化

void OLED_Clear(void);            // OLED 清屏

void OLED_WriteCMD(u8 cmd);        // OLED 单次写命令

void OLED_WriteDAT(u8 dat);        // OLED 单次写数据

void OLED_WriteMultiByte(u8 *dat, u8 len, u8 cmd);    // OLED连续写入

void OLED_Frame(u8 P[8][128]);        // OLED 一帧图像写入

void OLED_Frame32(u8 P[4][128]);    // OLED 32行写入(为128X32规格设计)

//------------------------------------内部函数------------------------------------//
static void IIC_START(void);        // IIC_开始信号    (重开始信号用此替代)
static void IIC_STOP(void);         // IIC_结束信号
static void IIC_Write(u8 dat);        // IIC_写数据函数    参数:要写入的数据
//------------------------------------函数内容------------------------------------//

void OLED_WriteCMD(u8 cmd)
{
    
    
    IIC_START();        // 通信开始
    IIC_Write(0X78);    // 写从机地址'0111 100' 读写符号'0'
    IIC_Write(0X00);    // 写命令  Co='0' C/D='000 0000'
    IIC_Write(cmd);        // 写入命令
    IIC_STOP();            // 通信结束
}

void OLED_WriteDAT(u8 dat)
{
    
    
    IIC_START();        // 通信开始
    IIC_Write(0X78);    // 写从机地址'0111 100' 读写符号'0'
    IIC_Write(0X40);    // 写数据  Co='0' C/D='100 0000'
    IIC_Write(dat);        // 写入数据
    IIC_STOP();            // 通信结束
}

// 参数说明: cmd: 0X00,写命令  0X40,写数据
void OLED_WriteMultiByte(u8 *dat, u8 len, u8 cmd)
{
    
    
    u8 i,j;
    IIC_START();        // 通信开始
    IIC_Write(0X78);    // 写从机地址'0111 100' 读写符号'0'
    IIC_Write(cmd);        // 根据参数选择写命令还是数据
    for(i=0; i<len; i++)
        IIC_Write(dat[i]);
    //通信结束
    IIC_STOP();
}

void OLED_Init(void)
{
    
    
    IIC_START();            // 通信开始
    IIC_Write(0X78);        // 写从机地址'0111 100' 读写符号'0'
    IIC_Write(0x00);        // 写命令 Co='0' C/D='000 0000'
    IIC_Write(0XAE);        // 关OLED显示
    // 基础设置
    IIC_Write(0XA4);        // 输出GDDRAM内容
    IIC_Write(0XA6);        // 正常显示(10)
    IIC_Write(0X81);        // 设置对比度
    IIC_Write(0X7F);        //127级对比度
    // COM和SEG输出设置
    IIC_Write(0XD3);        // 设置垂直显示偏移(向上)
    IIC_Write(0X00);        // 偏移0
    IIC_Write(0X40);        // 设置GDDRAM起始行 0
    IIC_Write(0XA8);        // 设置MUX数 (显示行数)
    IIC_Write(0X3F);        //  MUX=63     (显示63)
    IIC_Write(0XA1);        // 左右反置关(段重映射)
    IIC_Write(0XC8);        // 上下反置关(行重映射)
    IIC_Write(0XDA);        // 设置COM引脚配置
    IIC_Write(0X12);        // 序列COM配置,禁用左右反置
    // 时钟设置
    IIC_Write(0XD5);        // 设置DCLK分频和OSC频率
    IIC_Write(0X80);        // 无分频,8级OSC频率
    // 开OLED
    IIC_Write(0X8D);        // 启用电荷泵
    IIC_Write(0X14);        // 启用电荷泵
    IIC_Write(0XAF);        // 开OLED显示
    // 通信结束
    IIC_STOP();
}

void OLED_Clear(void)
{
    
    
    u8 i,j;
    // 地址设置
    IIC_START();        // 通信开始
    IIC_Write(0X78);    // 写从机地址'0111 100' 读写符号'0'
    IIC_Write(0x00);    // 写命令 Co='0' C/D='000 0000'
    IIC_Write(0X20);    // 设置GDDRAM模式
    IIC_Write(0X00);    // 水平寻址模式
    IIC_Write(0X21);    // 设置列起始和结束地址
    IIC_Write(0X00);    // 列起始地址 0
    IIC_Write(0X7F);    // 列终止地址 127
    IIC_Write(0X22);    // 设置页起始和结束地址
    IIC_Write(0X00);    // 页起始地址 0
    IIC_Write(0X07);    // 页终止地址 7
    // 写入一帧'0'
    IIC_START();        // 通信重开始
    IIC_Write(0X78);    // 写从机地址'0111 100' 读写符号'0'
    IIC_Write(0X40);    // 写数据  Co='0' C/D='100 0000'
    for(i=0; i<8; i++){
    
    
        for(j=0; j<128; j++){
    
    
            IIC_Write(0X00);
        }
    }

    IIC_STOP();
}

void OLED_Frame(u8 P[8][128])
{
    
    
    u8 i,j;
    // 地址设置
    IIC_START();        // 通信开始
    IIC_Write(0X78);    // 写从机地址'0111 100' 读写符号'0'
    IIC_Write(0x00);    // 写命令 Co='0' C/D='000 0000'
    IIC_Write(0X20);    // 设置GDDRAM模式
    IIC_Write(0X00);    // 水平寻址模式
    IIC_Write(0X21);    // 设置列起始和结束地址
    IIC_Write(0X00);    // 列起始地址 0
    IIC_Write(0X7F);    // 列终止地址 127
    IIC_Write(0X22);    // 设置页起始和结束地址
    IIC_Write(0X00);    // 页起始地址 0
    IIC_Write(0X07);    // 页终止地址 7
    // 写入一帧数据
    IIC_START();        // 通信重开始
    IIC_Write(0X78);    // 写从机地址'0111 100' 读写符号'0'
    IIC_Write(0X40);    // 写数据  Co='0' C/D='100 0000'
    for(i=0; i<8; i++)
        for(j=0; j<128; j++)
            IIC_Write(P[i][j]);
    // 结束IIC通信
    IIC_STOP();
}

void OLED_Frame32(u8 P[4][128])
{
    
    
    u8 i,j;
    // 地址设置
    IIC_START();        // 通信开始
    IIC_Write(0X78);    // 写从机地址'0111 100' 读写符号'0'
    IIC_Write(0x00);    // 写命令 Co='0' C/D='000 0000'
    IIC_Write(0X20);    // 设置GDDRAM模式
    IIC_Write(0X00);    // 水平寻址模式
    IIC_Write(0X21);    // 设置列起始和结束地址
    IIC_Write(0X00);    // 列起始地址 0
    IIC_Write(0X7F);    // 列终止地址 127
    IIC_Write(0X22);    // 设置页起始和结束地址
    IIC_Write(0X00);    // 页起始地址 0
    IIC_Write(0X07);    // 页终止地址 7
    // 写入32行数据
    IIC_START();        // 通信重开始
    IIC_Write(0X78);    // 写从机地址'0111 100' 读写符号'0'
    IIC_Write(0X40);    // 写数据  Co='0' C/D='100 0000'
    for(i=0; i<4; i++)
        for(j=0; j<128; j++)
            IIC_Write(P[i][j]);
    // 结束IIC通信
    IIC_STOP();
}

//----------------------------------内部函数内容-----------------------------------//

static void IIC_START(void)
{
    
    
    SDA = 1;        // SDA拉高
    SCL = 1;        // SCL拉高 准备发出起始信号
    SDA = 0;        // SDA拉低 发出起始信号
    SCL = 0;        // SCL拉低 开始传输
}

static void IIC_STOP(void)
{
    
    
    SCL = 0;        // SCL拉低 防止可能出现的各种误动作
    SDA = 0;        // SDA拉低
    SCL = 1;        // SCL拉高 准备发出结束信号
    SDA = 1;        // SDA拉高 发出结束信号
}

static void IIC_Write(u8 dat)
{
    
    
    u8 i;
    for(i=0; i<8; i++)
    {
    
    
        SCL = 0;             // 拉低SCL 准备写数据
        SDA = dat & 0X80;    // 写数据
        dat <<= 1;            // 数据格式:高位在前
        SCL = 1;            // 拉高SCL 发送数据
        SCL = 0;            // 拉低SCL 结束发送
    }
    // 快速跳过ACK
    SCL = 1;        // SCL拉高告诉从机响应
    SCL = 0;        // SCL拉低结束询问ACK
}

//---------------------------------------------------------------------------------//
//------------------------------------定义结束-------------------------------------//
//---------------------------------------------------------------------------------//

//---------------------------------------------------------------------------------//
//------------------------------------定义结束-------------------------------------//
//---------------------------------------------------------------------------------//

 

Summary  ※


  Based on the STC32F single-chip microcomputer, this paper

GM1688719031_1280_800.MPG|_-2

  Test OLED LCD module

/*
**==============================================================================
** OLED1306.C:             -- by Dr. ZhuoQing, 2023-07-07
**
**==============================================================================
*/

//------------------------------------------------------------------------------

//------------------------------------------------------------------------------

void OLED1306Init(void) {
    
    
    OLED_Init();
    OLED_BufferSet(0x0);    
    OLED_Frame(g_ucOLEDBuffer);    
}

void OLED_BufferSet(u8 byt) {
    
    
    u8 i, j;
    for(i = 0; i < 8; i ++) {
    
    
        for(j = 0; j < 128; j ++)
            g_ucOLEDBuffer[i][j] = byt;
    }
}    

void OLED1306_Show(void) {
    
    
    OLED_Frame(g_ucOLEDBuffer);
}

//------------------------------------------------------------------------------
void OLED_WriteCMD(u8 cmd) {
    
    

    IIC_START();           // 通信开始
    IIC_Write(0X78);       // 写从机地址'0111 100' 读写符号'0'
    IIC_Write(0X00);       // 写命令  Co='0' C/D='000 0000'
    IIC_Write(cmd);        // 写入命令
    IIC_STOP();            // 通信结束
}

void OLED_WriteDAT(u8 dat) {
    
    

    IIC_START();           // 通信开始
    IIC_Write(0X78    );       // 写从机地址'0111 100' 读写符号'0'
    IIC_Write(0X40);       // 写数据  Co='0' C/D='100 0000'
    IIC_Write(dat);        // 写入数据
    IIC_STOP();            // 通信结束
}

//------------------------------------------------------------------------------
// 参数说明: cmd: 0X00,写命令  0X40,写数据
void OLED_WriteMultiByte(u8 *dat, u8 len, u8 cmd) {
    
    

    u8 i;
    IIC_START();           // 通信开始
    IIC_Write(0X78);       // 写从机地址'0111 100' 读写符号'0'
    IIC_Write(cmd);        // 根据参数选择写命令还是数据
    for(i=0; i<len; i++)
        IIC_Write(dat[i]);
    //通信结束
    IIC_STOP();
}

//------------------------------------------------------------------------------
const unsigned char OLEDInitCmd[] = {
    
    
    0xae,0X00,0X10,0x40,0X81,0XCF,0xff,0xa1,0xa4,
    0xA6,0xc8,0xa8,0x3F,0xd5,0x80,0xd3,0x00,0XDA,0X12,
    0x8d,0x14,0xdb,0x40,0X20,0X02,0xd9,0xf1,0xAF,
};

void OLED_Init(void) {
    
    

    u8 i, length;
    
    length = sizeof(OLEDInitCmd);       
    IIC_START();
    IIC_Write(0X78);       // 写从机地址'0111 100' 读写符号'0'
    IIC_Write(0X00);       // 写命令  Co='0' C/D='000 0000'
    for(i = 0; i < length; i ++) {
    
    
        IIC_Write(OLEDInitCmd[i]);
    }
    
    IIC_STOP();
}

//------------------------------------------------------------------------------
void OLED_Clear(void) {
    
    

    u8 i,j;

    // 地址设置
    IIC_START();        // 通信开始
    IIC_Write(0X78);    // 写从机地址'0111 100' 读写符号'0'
    IIC_Write(0x00);    // 写命令 Co='0' C/D='000 0000'
    IIC_Write(0X20);    // 设置GDDRAM模式
    IIC_Write(0X00);    // 水平寻址模式
    IIC_Write(0X21);    // 设置列起始和结束地址
    IIC_Write(0X00);    // 列起始地址 0
    IIC_Write(0X7F);    // 列终止地址 127
    IIC_Write(0X22);    // 设置页起始和结束地址
    IIC_Write(0X00);    // 页起始地址 0
    IIC_Write(0X07);    // 页终止地址 7           

    // 写入一帧'0'
    IIC_START();        // 通信重开始
    IIC_Write(0X78);    // 写从机地址'0111 100' 读写符号'0'
    IIC_Write(0X40);    // 写数据  Co='0' C/D='100 0000'
    for(i=0; i<8; i++){
    
    
        for(j=0; j<128; j++){
    
    
            IIC_Write(0X00);
        }
    }

    IIC_STOP();
}

//------------------------------------------------------------------------------
void OLED_Frame(u8 P[8][128]) {
    
    
    
    u8 i,j;

    // 地址设置
    IIC_START();        // 通信开始
    IIC_Write(0X78);    // 写从机地址'0111 100' 读写符号'0'
    IIC_Write(0x00);    // 写命令 Co='0' C/D='000 0000'
    IIC_Write(0X20);    // 设置GDDRAM模式
    IIC_Write(0X00);    // 水平寻址模式
    IIC_Write(0X21);    // 设置列起始和结束地址
    IIC_Write(0X00);    // 列起始地址 0
    IIC_Write(0X7F);    // 列终止地址 127
    IIC_Write(0X22);    // 设置页起始和结束地址
    IIC_Write(0X00);    // 页起始地址 0
    IIC_Write(0X07);    // 页终止地址 7

    // 写入一帧数据
    IIC_START();        // 通信重开始
    IIC_Write(0X78);    // 写从机地址'0111 100' 读写符号'0'
    IIC_Write(0X40);    // 写数据  Co='0' C/D='100 0000'
    for(i=0; i<8; i++) {
    
    
        for(j=0; j<128; j++) {
    
    
            IIC_Write(P[i][j]);                        
        }
    }

    // 结束IIC通信
    IIC_STOP();
}

//------------------------------------------------------------------------------
void OLED_Frame32(u8 P[4][128]) {
    
    

    u8 i,j;

    // 地址设置
    IIC_START();        // 通信开始
    IIC_Write(0X78);    // 写从机地址'0111 100' 读写符号'0'
    IIC_Write(0x00);    // 写命令 Co='0' C/D='000 0000'
    IIC_Write(0X20);    // 设置GDDRAM模式
    IIC_Write(0X00);    // 水平寻址模式
    IIC_Write(0X21);    // 设置列起始和结束地址
    IIC_Write(0X00);    // 列起始地址 0
    IIC_Write(0X7F);    // 列终止地址 127
    IIC_Write(0X22);    // 设置页起始和结束地址
    IIC_Write(0X00);    // 页起始地址 0
    IIC_Write(0X07);    // 页终止地址 7

    // 写入32行数据
    IIC_START();        // 通信重开始
    IIC_Write(0X78);    // 写从机地址'0111 100' 读写符号'0'
    IIC_Write(0X40);    // 写数据  Co='0' C/D='100 0000'
    for(i=0; i<4; i++)
        for(j=0; j<128; j++)
            IIC_Write(P[i][j]);
    // 结束IIC通信
    IIC_STOP();
}

//------------------------------------------------------------------------------
void OLED1306ShowF8X16(unsigned char ucX, unsigned char ucY, unsigned char ucID) {
    
    
    const unsigned char * p;
    unsigned char i;
    
    p = &F8X16[ucID*16];    
    
    for(i = 0; i < 8; i ++) {
    
    
        if(i + ucX >= OLED1306_WIDTH) break;
        g_ucOLEDBuffer[ucY][i+ucX] = *(p + i);
    }
    for(i = 0; i < 8; i ++) {
    
    
        g_ucOLEDBuffer[ucY+1][i+ucX] = *(p + i + 8);
    }
}

void OLED1306ShowF6X8(unsigned char ucX, unsigned char ucY, unsigned char ucID) {
    
    
    const unsigned char * p;
    unsigned char i;
    
    p = (const unsigned char *)&F6x8[ucID];    
    
    for(i = 0; i < 6; i ++) {
    
    
        if(i + ucX >= OLED1306_WIDTH) break;
        g_ucOLEDBuffer[ucY][i+ucX] = *(p + i);
    }
    
}

//------------------------------------------------------------------------------
void OLED1306_GReset(void) {
    
    
    unsigned char i, j;
    for(i = 0; i < 128; i ++) {
    
    
        for(j = 0; j < 8; j ++)
            g_ucOLEDBuffer[j][i] = 0x0;
    }
}

void OLED1306_GSet(void) {
    
    
    unsigned char i, j;
    for(i = 0; i < 128; i ++) {
    
    
        for(j = 0; j < 8; j ++)
            g_ucOLEDBuffer[j][i] = 0xff;
    }
    
}

void OLED1306_GSetP(unsigned char x, unsigned char y) {
    
    
    if(x > OLED1306_WIDTH-1 || y > OLED1306_HEIGHT-1) return;
    g_ucOLEDBuffer[y/8][x] |= (0x01 <<(y%8));
}

void OLED1306_GResetP(unsigned char x, unsigned char y) {
    
    
    if(x > OLED1306_WIDTH-1 || y > OLED1306_HEIGHT-1) return;
    g_ucOLEDBuffer[y/8][x] &= ~(0x01<<(y%8));
}

unsigned char OLED1306_GGetP(unsigned char x, unsigned char y) {
    
    
    if(x > OLED1306_WIDTH-1 || y > OLED1306_HEIGHT-1) return 0x0;
    return (g_ucOLEDBuffer[y/8][x] >> (y%8)) & 0x01;
}

void OLED1306_GSetByte(unsigned char sdata, unsigned char page, unsigned char column) {
    
    
    if(page > 7 || column > 127) return;
    g_ucOLEDBuffer[page][column] = sdata;
}

void OLED1306_GFlipPart(unsigned char x,unsigned char y,
                       unsigned char width,unsigned char height) {
    
    
    unsigned char i,j;

    for(i = x; i < x+width; i++){
    
    
        for(j = y; j < y+height;j++){
    
    
            if(OLED1306_GGetP(i,j)){
    
    
                OLED1306_GResetP(i,j);
            } else{
    
    
                OLED1306_GSetP(i,j);
            }
        }
    }
}

void OLED1306_GModifyPart(unsigned char x1,unsigned char y1,
                         unsigned char x2,unsigned char y2,
                         unsigned char color) {
    
    
    unsigned char i,j;

    for(i = x1;i < x2; i ++) {
    
    
        for(j = y1;j < y2; j++) {
    
    
            if(color)
                OLED1306_GSetP(i,j);
            else OLED1306_GResetP(i,j);
        }
    }
}

void OLED1306_GDrawBitmap(unsigned char x,unsigned char y, const unsigned char *dat,
                         unsigned char width, unsigned char height) {
    
    
                          
    unsigned char i,j,offset,index,fast_fill;
    const unsigned char *p;
    
    offset = 0;
    index = 0;
    fast_fill = 0;
    p = dat;
    
    offset = y%8;
    if(offset == 0) fast_fill=1;
    offset = 0;
    
    for(i = x; i < x+width; i++){
    
    
        for(j = y;j < y+height; j++){
    
    
            if(fast_fill){
    
    
                OLED1306_GSetByte(*p,(u8)(j/8),i);
                j += 8;
                p ++;
            } else {
    
    
                if((*p>>offset)&0x01){
    
    
                    OLED1306_GSetP(i,j);
                }else{
    
    
                    OLED1306_GResetP(i,j);
                }
                offset++;
                if(offset%8==0){
    
     p++;offset=0;};
            }
        }
    }
}

void OLED1306_GDrawChar(unsigned char x,unsigned char y,const char dat,
                       unsigned char width,unsigned char height) {
    
    
                        
    unsigned char i,j,offset,index,fast_fill;
    const unsigned char *p;
    
    offset = 0;
    index = 0;
    fast_fill = 0;
    
    index=(int)dat;
    
    if(index>126||index<32) return;//不在可绘制列表内
    
//    p=&ascii[index-32][0];

    offset=y%8;
    if(offset==0)fast_fill=1;
    offset=0;
    
    for(i=x;i<x+width;i++){
    
    
        for(j=y;j<y+height;j++){
    
    
            if(fast_fill){
    
    
                OLED1306_GSetByte(*p,(u8)(j/8),i);
                j+=8;
                p++;
            }else{
    
    
                if((*p>>offset)&0x01){
    
    
                    OLED1306_GSetP(i,j);
                }else{
    
    
                    OLED1306_GResetP(i,j);
                }
                offset++;
                if(offset%8==0){
    
    p++;offset=0;};
            }
        }
    }
}

void OLED1306_GDrawString(unsigned char x,unsigned char y,const char *dat,
                         unsigned char width,unsigned char height) {
    
    
    unsigned char x_pos;
    
    x_pos = x;
    
    while(*dat!=0){
    
    
        if(*dat=='\n'){
    
     y+=height;dat++;x_pos=x;continue;}
        
        if(x_pos>OLED1306_WIDTH-1||y>OLED1306_HEIGHT-1){
    
    dat++;continue;}
        
        OLED1306_GDrawChar(x_pos,y,*dat,width,height);
        x_pos+=width;
        dat++;
    }
}

void OLED1306_GDrawL_V(unsigned char X_B,unsigned char Y_B,unsigned char height) {
    
    
    unsigned char Y;

    for(Y=Y_B;Y<Y_B+height;Y++) {
    
            
        OLED1306_GSetP(X_B,Y);
    }
}

void OLED1306_GDrawL_R(unsigned char X_B,unsigned char Y_B,unsigned char width) {
    
    
    unsigned char X;
    
    for(X=X_B;X<X_B+width;X++)
    {
    
    
        OLED1306_GSetP(X,Y_B);
    }
}

void OLED1306_GDrawHollowRect(unsigned char xpos,unsigned char ypos,
                             unsigned char iwidth,unsigned char iheight) {
    
    
    OLED1306_GDrawL_R(xpos,ypos,iwidth);
    OLED1306_GDrawL_R(xpos,(u8)(ypos+iheight-1),iwidth);
    
    OLED1306_GDrawL_V(xpos,ypos,iheight);
    OLED1306_GDrawL_V((u8)(xpos+iwidth-1),ypos,iheight);
}

void OLED1306_GDrawSolidRect(unsigned char xpos,unsigned char ypos,
                           unsigned char iwidth,unsigned char iheight) {
    
    
    unsigned char i,j;
    
    for(i=xpos;i<xpos+iwidth;i++){
    
    
        for(j=ypos;j<ypos+iheight;j++){
    
    
            OLED1306_GSetP(i,j);
        }
    }
}

void OLED1306_GDrawNetRect(unsigned char xpos,unsigned char ypos,
                          unsigned char iwidth, unsigned char iheight) {
    
    
    unsigned char i,j;

    for(i=xpos;i<xpos+iwidth;i++){
    
    
        for(j=ypos;j<ypos+iheight;j++){
    
    
            if(i%2==0||j%2==0)
                OLED1306_GResetP(i,j);
            else OLED1306_GSetP(i,j);
        }
    }
}

void OLED1306_GDrawRectdotO(unsigned char xpos,unsigned char ypos,
                           unsigned char iwidth,unsigned char iheight,
                           unsigned char density) {
    
    
    unsigned char i;

    for(i=xpos+2;i<xpos+iwidth-2;i+=density){
    
    
        OLED1306_GSetP(i, (u8)(ypos+2));
    }
        
    for(i=xpos+2;i<xpos+iwidth-2;i+=density){
    
    
        OLED1306_GSetP(i, (u8)(ypos+iheight-3));
    }
    
    for(i=ypos+2;i<ypos+iheight-2;i+=density){
    
    
        OLED1306_GSetP((u8)(xpos+2), i);
    }
    
    for(i=ypos+2;i<ypos+iheight-2;i+=density){
    
    
        OLED1306_GSetP((u8)(xpos+iwidth-3), i);
    }
    
    OLED1306_GDrawHollowRect(xpos,ypos,iwidth,iheight);   
}

void OLED1306_GDrawRectdotI(unsigned char xpos,unsigned char ypos,
                           unsigned char iwidth,unsigned char iheight,
                           unsigned char density)                            {
    
    
    unsigned char i;
    
    for(i=xpos;i<xpos+iwidth;i+=density){
    
    
        OLED1306_GSetP(i, ypos);
    }
    for(i=xpos;i<xpos+iwidth;i+=density){
    
    
        OLED1306_GSetP(i, (u8)(ypos+iheight-1));
    }
    for(i=ypos;i<ypos+iheight;i+=density){
    
    
        OLED1306_GSetP(xpos, i);
    }
    for(i=ypos;i<ypos+iheight;i+=density){
    
    
        OLED1306_GSetP((u8)(xpos+iwidth-1), i);
    }
    OLED1306_GDrawHollowRect((u8)(xpos+2),(u8)(ypos+2),(u8)(iwidth-4),(u8)(iheight-4));
}

//------------------------------------------------------------------------------
void OLED1306ShowString(unsigned char ucX, unsigned char ucY, unsigned char * pString) {
    
    
    while(*(pString)) {
    
    
        OLED1306ShowF8X16(ucX, ucY, (u8)(*(pString)-' '));
        pString ++;
        ucX += 8;        
    }    
}

void OLED1306ShowStringCo(unsigned char ucX, unsigned char ucY, const unsigned char * pString) {
    
    
    while(*(pString)) {
    
    
        OLED1306ShowF8X16(ucX, ucY, (u8)(*(pString)-' '));
        pString ++;
        ucX += 8;        
    }        
}

//==============================================================================
//                END OF FILE : OLED1306.C
//------------------------------------------------------------------------------
/*
**==============================================================================
** OLED1306.H:            -- by Dr. ZhuoQing, 2023-07-07
**
**  Description:
**
**==============================================================================
*/
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//==============================================================================
void OLED1306Init(void);

//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
typedef unsigned char u8;

//------------------------------------------------------------------------------
void OLED_WriteCMD(u8 cmd);
void OLED_WriteDAT(u8 dat);

void OLED_WriteMultiByte(u8 *dat, u8 len, u8 cmd);
void OLED_Init(void);
void OLED_Clear(void);
void OLED_Frame(u8 P[8][128]);
void OLED_Frame32(u8 P[4][128]);

//------------------------------------------------------------------------------
OLED1306_EXT u8 g_ucOLEDBuffer[8][128];

void OLED_BufferSet(u8 byt);
void OLED1306_Show(void);

//------------------------------------------------------------------------------
void OLED1306ShowF8X16(unsigned char ucX, unsigned char ucY, unsigned char ucID);
void OLED1306ShowF6X8(unsigned char ucX, unsigned char ucY, unsigned char ucID);

void OLED1306ShowString(unsigned char ucX, unsigned char ucY, unsigned char * pString);
void OLED1306ShowStringCo(unsigned char ucX, unsigned char ucY, const unsigned char * pString);

//------------------------------------------------------------------------------

void OLED1306_GReset(void);
void OLED1306_GSet(void);
void OLED1306_GSetP(unsigned char x, unsigned char y);
void OLED1306_GResetP(unsigned char x, unsigned char y);
unsigned char OLED1306_GGetP(unsigned char x, unsigned char y);
void OLED1306_GSetByte(unsigned char sdata, unsigned char page, unsigned char column);
void OLED1306_GFlipPart(unsigned char x,unsigned char y,
                       unsigned char width,unsigned char height);
void OLED1306_GModifyPart(unsigned char x1,unsigned char y1,
                         unsigned char x2,unsigned char y2,
                         unsigned char color);
void OLED1306_GDrawBitmap(unsigned char x,unsigned char y, const unsigned char *dat,
                         unsigned char width, unsigned char height);
void OLED1306_GDrawChar(unsigned char x,unsigned char y,const char dat,
                       unsigned char width,unsigned char height);
void OLED1306_GDrawString(unsigned char x,unsigned char y,const char *dat,
                         unsigned char width,unsigned char height);
void OLED1306_GDrawL_V(unsigned char X_B,unsigned char Y_B,unsigned char height);
void OLED1306_GDrawL_R(unsigned char X_B,unsigned char Y_B,unsigned char width);
void OLED1306_GDrawHollowRect(unsigned char xpos,unsigned char ypos,
                             unsigned char iwidth,unsigned char iheight);
void OLED1306_GDrawSolidRect(unsigned char xpos,unsigned char ypos,
                           unsigned char iwidth,unsigned char iheight);
void OLED1306_GDrawNetRect(unsigned char xpos,unsigned char ypos,
                          unsigned char iwidth, unsigned char iheight);
void OLED1306_GDrawRectdotO(unsigned char xpos,unsigned char ypos,
                           unsigned char iwidth,unsigned char iheight,
                           unsigned char density);
void OLED1306_GDrawRectdotI(unsigned char xpos,unsigned char ypos,
                           unsigned char iwidth,unsigned char iheight,
                           unsigned char density);
                           
//==============================================================================
//             END OF FILE : OLED1306.H
//------------------------------------------------------------------------------


■ Links to related literature:

● Links to related diagrams:

Guess you like

Origin blog.csdn.net/zhuoqingjoking97298/article/details/131492289
Recommended