RT_Thread学习笔记(6) OLED驱动移植 IIC

RT THREAD OLED IIC驱动移植

1.硬件环境

在这里插入图片描述

MCU:STM32F103C8T6

OLED:SSD1306 0.96

关键用u8g2的话内存开销太大了,所以正好学习一下IIC驱动


2.程序开发

2.1 开启IIC

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HTMgc0wO-1625904982998)(https://note.youdao.com/yws/res/2/WEBRESOURCE205552f156293d564bed747aa3a47f22)][外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PVvQnVzm-1625904982999)(https://note.youdao.com/yws/res/d/WEBRESOURCE732f85cd041e92ec3c1cd3db725069bd)]
在这里插入图片描述

记得下载后在MSH中查看是否真的开启了IIC总线

2.2程序移植

首先需要明确一点 rtt和传统驱动的区别

rtthread把地址和读写位是分开的,底层发送的数据是将地址左移1位再或上读写位,如果你发0xa0,左移后再或上写,这个数据不对了,应该发0x50左移后变成0xa0再或上写就对了,相关的解释网址如下:

https://club.rt-thread.org/ask/question/8065.html

其实我当时在驱动AT24C256的时候是十分困惑的,为什么在文档里写的0xa0为什么在程序中要设置为0x50,直到我自己真正动手驱动的时候才知道了原因

所以既然OLED的从机地址是0x78,对应0111 1000

那我们右移一位 0011 1100变成0x3c


然后在整个驱动的过程中最终要的还是写指令的操作,代码如下:

static rt_err_t WriteCmd(struct rt_i2c_bus_device *bus, rt_uint8_t data)
{
    rt_uint8_t buf[2];
    struct rt_i2c_msg msgs;

    buf[0] = 0x00;
    buf[1] = data;


    msgs.addr = OLED_ADDR;
    msgs.flags = RT_I2C_WR;
    msgs.buf = buf;
    msgs.len = 2;

    /* 调用I2C设备接口传输数据 */
    if (rt_i2c_transfer(bus, &msgs, 1) == 1)
    {
        return RT_EOK;
    }
    else
    {
        return -RT_ERROR;
    }
}

static rt_err_t WriteDat(struct rt_i2c_bus_device *bus, rt_uint8_t data)
{
    rt_uint8_t buf[2];
    struct rt_i2c_msg msgs;

    buf[0] = 0x40;
    buf[1] = data;


    msgs.addr = OLED_ADDR;
    msgs.flags = RT_I2C_WR;
    msgs.buf = buf;
    msgs.len = 2;

    /* 调用I2C设备接口传输数据 */
    if (rt_i2c_transfer(bus, &msgs, 1) == 1)
    {
        return RT_EOK;
    }
    else
    {
        return -RT_ERROR;
    }
}

初始化函数

void OLED_INIT(void)
{
    rt_thread_mdelay(100);

    WriteCmd(i2c_bus,0xAE); //display off
    WriteCmd(i2c_bus,0x20); //Set Memory Addressing Mode
    WriteCmd(i2c_bus,0x10); //00,Horizontal Addressing Mode;01,Vertical Addressing Mode;10,Page Addressing Mode (RESET);11,Invalid
    WriteCmd(i2c_bus,0xb0); //Set Page Start Address for Page Addressing Mode,0-7
    WriteCmd(i2c_bus,0xc8); //Set COM Output Scan Direction
    WriteCmd(i2c_bus,0x00); //---set low column address
    WriteCmd(i2c_bus,0x10); //---set high column address
    WriteCmd(i2c_bus,0x40); //--set start line address
    WriteCmd(i2c_bus,0x81); //--set contrast control register
    WriteCmd(i2c_bus,0xff); //亮度调节 0x00~0xff
    WriteCmd(i2c_bus,0xa1); //--set segment re-map 0 to 127
    WriteCmd(i2c_bus,0xa6); //--set normal display
    WriteCmd(i2c_bus,0xa8); //--set multiplex ratio(1 to 64)
    WriteCmd(i2c_bus,0x3F); //
    WriteCmd(i2c_bus,0xa4); //0xa4,Output follows RAM content;0xa5,Output ignores RAM content
    WriteCmd(i2c_bus,0xd3); //-set display offset
    WriteCmd(i2c_bus,0x00); //-not offset
    WriteCmd(i2c_bus,0xd5); //--set display clock divide ratio/oscillator frequency
    WriteCmd(i2c_bus,0xf0); //--set divide ratio
    WriteCmd(i2c_bus,0xd9); //--set pre-charge period
    WriteCmd(i2c_bus,0x22); //
    WriteCmd(i2c_bus,0xda); //--set com pins hardware configuration
    WriteCmd(i2c_bus,0x12);
    WriteCmd(i2c_bus,0xdb); //--set vcomh
    WriteCmd(i2c_bus,0x20); //0x20,0.77xVcc
    WriteCmd(i2c_bus,0x8d); //--set DC-DC enable
    WriteCmd(i2c_bus,0x14); //
    WriteCmd(i2c_bus,0xaf); //--turn on oled panel

}

下载程序后看到一片花就对了

其余相关函数都放进来了

//全屏填充
void OLED_FILL(unsigned char fill_data)
{
    unsigned char m,n;
    for(m=0;m<8;m++){
        WriteCmd(i2c_bus,0xb0+m);
        WriteCmd(i2c_bus,0x00);
        WriteCmd(i2c_bus,0x10);

        for(n=0;n<128;n++){
            WriteDat(i2c_bus,fill_data);
        }
    }
}

//OLED清屏
void OLED_CLS(void)
{
    OLED_FILL(0x00);
}




void OLED_SetPos(unsigned char x, unsigned char y) //设置起始点坐标
{
    WriteCmd(i2c_bus,0xb0+y);
    WriteCmd(i2c_bus,((x&0xf0)>>4)|0x10);
    WriteCmd(i2c_bus,(x&0x0f)|0x01);
}


const unsigned char F6x8[][6] =
{
    {0x00, 0x00, 0x00, 0x00, 0x00, 0x00},// sp
    {0x00, 0x00, 0x00, 0x2f, 0x00, 0x00},// !
    {0x00, 0x00, 0x07, 0x00, 0x07, 0x00},// "
    {0x00, 0x14, 0x7f, 0x14, 0x7f, 0x14},// #
    {0x00, 0x24, 0x2a, 0x7f, 0x2a, 0x12},// $
    {0x00, 0x62, 0x64, 0x08, 0x13, 0x23},// %
    {0x00, 0x36, 0x49, 0x55, 0x22, 0x50},// &
    {0x00, 0x00, 0x05, 0x03, 0x00, 0x00},// '
    {0x00, 0x00, 0x1c, 0x22, 0x41, 0x00},// (
    {0x00, 0x00, 0x41, 0x22, 0x1c, 0x00},// )
    {0x00, 0x14, 0x08, 0x3E, 0x08, 0x14},// *
    {0x00, 0x08, 0x08, 0x3E, 0x08, 0x08},// +
    {0x00, 0x00, 0x00, 0xA0, 0x60, 0x00},// ,
    {0x00, 0x08, 0x08, 0x08, 0x08, 0x08},// -
    {0x00, 0x00, 0x60, 0x60, 0x00, 0x00},// .
    {0x00, 0x20, 0x10, 0x08, 0x04, 0x02},// /
    {0x00, 0x3E, 0x51, 0x49, 0x45, 0x3E},// 0
    {0x00, 0x00, 0x42, 0x7F, 0x40, 0x00},// 1
    {0x00, 0x42, 0x61, 0x51, 0x49, 0x46},// 2
    {0x00, 0x21, 0x41, 0x45, 0x4B, 0x31},// 3
    {0x00, 0x18, 0x14, 0x12, 0x7F, 0x10},// 4
    {0x00, 0x27, 0x45, 0x45, 0x45, 0x39},// 5
    {0x00, 0x3C, 0x4A, 0x49, 0x49, 0x30},// 6
    {0x00, 0x01, 0x71, 0x09, 0x05, 0x03},// 7
    {0x00, 0x36, 0x49, 0x49, 0x49, 0x36},// 8
    {0x00, 0x06, 0x49, 0x49, 0x29, 0x1E},// 9
    {0x00, 0x00, 0x36, 0x36, 0x00, 0x00},// :
    {0x00, 0x00, 0x56, 0x36, 0x00, 0x00},// ;
    {0x00, 0x08, 0x14, 0x22, 0x41, 0x00},// <
    {0x00, 0x14, 0x14, 0x14, 0x14, 0x14},// =
    {0x00, 0x00, 0x41, 0x22, 0x14, 0x08},// >
    {0x00, 0x02, 0x01, 0x51, 0x09, 0x06},// ?
    {0x00, 0x32, 0x49, 0x59, 0x51, 0x3E},// @
    {0x00, 0x7C, 0x12, 0x11, 0x12, 0x7C},// A
    {0x00, 0x7F, 0x49, 0x49, 0x49, 0x36},// B
    {0x00, 0x3E, 0x41, 0x41, 0x41, 0x22},// C
    {0x00, 0x7F, 0x41, 0x41, 0x22, 0x1C},// D
    {0x00, 0x7F, 0x49, 0x49, 0x49, 0x41},// E
    {0x00, 0x7F, 0x09, 0x09, 0x09, 0x01},// F
    {0x00, 0x3E, 0x41, 0x49, 0x49, 0x7A},// G
    {0x00, 0x7F, 0x08, 0x08, 0x08, 0x7F},// H
    {0x00, 0x00, 0x41, 0x7F, 0x41, 0x00},// I
    {0x00, 0x20, 0x40, 0x41, 0x3F, 0x01},// J
    {0x00, 0x7F, 0x08, 0x14, 0x22, 0x41},// K
    {0x00, 0x7F, 0x40, 0x40, 0x40, 0x40},// L
    {0x00, 0x7F, 0x02, 0x0C, 0x02, 0x7F},// M
    {0x00, 0x7F, 0x04, 0x08, 0x10, 0x7F},// N
    {0x00, 0x3E, 0x41, 0x41, 0x41, 0x3E},// O
    {0x00, 0x7F, 0x09, 0x09, 0x09, 0x06},// P
    {0x00, 0x3E, 0x41, 0x51, 0x21, 0x5E},// Q
    {0x00, 0x7F, 0x09, 0x19, 0x29, 0x46},// R
    {0x00, 0x46, 0x49, 0x49, 0x49, 0x31},// S
    {0x00, 0x01, 0x01, 0x7F, 0x01, 0x01},// T
    {0x00, 0x3F, 0x40, 0x40, 0x40, 0x3F},// U
    {0x00, 0x1F, 0x20, 0x40, 0x20, 0x1F},// V
    {0x00, 0x3F, 0x40, 0x38, 0x40, 0x3F},// W
    {0x00, 0x63, 0x14, 0x08, 0x14, 0x63},// X
    {0x00, 0x07, 0x08, 0x70, 0x08, 0x07},// Y
    {0x00, 0x61, 0x51, 0x49, 0x45, 0x43},// Z
    {0x00, 0x00, 0x7F, 0x41, 0x41, 0x00},// [
    {0x00, 0x55, 0x2A, 0x55, 0x2A, 0x55},// 55
    {0x00, 0x00, 0x41, 0x41, 0x7F, 0x00},// ]
    {0x00, 0x04, 0x02, 0x01, 0x02, 0x04},// ^
    {0x00, 0x40, 0x40, 0x40, 0x40, 0x40},// _
    {0x00, 0x00, 0x01, 0x02, 0x04, 0x00},// '
    {0x00, 0x20, 0x54, 0x54, 0x54, 0x78},// a
    {0x00, 0x7F, 0x48, 0x44, 0x44, 0x38},// b
    {0x00, 0x38, 0x44, 0x44, 0x44, 0x20},// c
    {0x00, 0x38, 0x44, 0x44, 0x48, 0x7F},// d
    {0x00, 0x38, 0x54, 0x54, 0x54, 0x18},// e
    {0x00, 0x08, 0x7E, 0x09, 0x01, 0x02},// f
    {0x00, 0x18, 0xA4, 0xA4, 0xA4, 0x7C},// g
    {0x00, 0x7F, 0x08, 0x04, 0x04, 0x78},// h
    {0x00, 0x00, 0x44, 0x7D, 0x40, 0x00},// i
    {0x00, 0x40, 0x80, 0x84, 0x7D, 0x00},// j
    {0x00, 0x7F, 0x10, 0x28, 0x44, 0x00},// k
    {0x00, 0x00, 0x41, 0x7F, 0x40, 0x00},// l
    {0x00, 0x7C, 0x04, 0x18, 0x04, 0x78},// m
    {0x00, 0x7C, 0x08, 0x04, 0x04, 0x78},// n
    {0x00, 0x38, 0x44, 0x44, 0x44, 0x38},// o
    {0x00, 0xFC, 0x24, 0x24, 0x24, 0x18},// p
    {0x00, 0x18, 0x24, 0x24, 0x18, 0xFC},// q
    {0x00, 0x7C, 0x08, 0x04, 0x04, 0x08},// r
    {0x00, 0x48, 0x54, 0x54, 0x54, 0x20},// s
    {0x00, 0x04, 0x3F, 0x44, 0x40, 0x20},// t
    {0x00, 0x3C, 0x40, 0x40, 0x20, 0x7C},// u
    {0x00, 0x1C, 0x20, 0x40, 0x20, 0x1C},// v
    {0x00, 0x3C, 0x40, 0x30, 0x40, 0x3C},// w
    {0x00, 0x44, 0x28, 0x10, 0x28, 0x44},// x
    {0x00, 0x1C, 0xA0, 0xA0, 0xA0, 0x7C},// y
    {0x00, 0x44, 0x64, 0x54, 0x4C, 0x44},// z
    {0x14, 0x14, 0x14, 0x14, 0x14, 0x14}// horiz lines
};

void OLED_ShowStr(unsigned char x, unsigned char y, unsigned char ch[])
{
    unsigned char c = 0,i = 0,j = 0;
    while(ch[j] != '\0')
    {
        c = ch[j] - 32;
        if(x > 126)
        {
            x = 0;
            y++;
        }
        OLED_SetPos(x,y);
        for(i=0;i<6;i++)
            WriteDat(i2c_bus,F6x8[c][i]);
        x += 6;
        j++;
    }
}


2.3程序执行效果

在开头显示了数据

3.所有代码

因为我是放到mian.c中测试用的,有需求的话自行封装

/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-07-10     RT-Thread    first version
 */

#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>


#define DBG_TAG "main"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>

#define OLED_I2C_BUS_NAME "i2c1"
#define OLED_ADDR 0x3c //从机地址 0011 1100 原来是0x78 记得右移1位

static struct rt_i2c_bus_device *i2c_bus=RT_NULL;



static rt_err_t WriteCmd(struct rt_i2c_bus_device *bus, rt_uint8_t data)
{
    rt_uint8_t buf[2];
    struct rt_i2c_msg msgs;

    buf[0] = 0x00;
    buf[1] = data;


    msgs.addr = OLED_ADDR;
    msgs.flags = RT_I2C_WR;
    msgs.buf = buf;
    msgs.len = 2;

    /* 调用I2C设备接口传输数据 */
    if (rt_i2c_transfer(bus, &msgs, 1) == 1)
    {
        return RT_EOK;
    }
    else
    {
        return -RT_ERROR;
    }
}

static rt_err_t WriteDat(struct rt_i2c_bus_device *bus, rt_uint8_t data)
{
    rt_uint8_t buf[2];
    struct rt_i2c_msg msgs;

    buf[0] = 0x40;
    buf[1] = data;


    msgs.addr = OLED_ADDR;
    msgs.flags = RT_I2C_WR;
    msgs.buf = buf;
    msgs.len = 2;

    /* 调用I2C设备接口传输数据 */
    if (rt_i2c_transfer(bus, &msgs, 1) == 1)
    {
        return RT_EOK;
    }
    else
    {
        return -RT_ERROR;
    }
}

void OLED_INIT(void)
{
    rt_thread_mdelay(100);

    WriteCmd(i2c_bus,0xAE); //display off
    WriteCmd(i2c_bus,0x20); //Set Memory Addressing Mode
    WriteCmd(i2c_bus,0x10); //00,Horizontal Addressing Mode;01,Vertical Addressing Mode;10,Page Addressing Mode (RESET);11,Invalid
    WriteCmd(i2c_bus,0xb0); //Set Page Start Address for Page Addressing Mode,0-7
    WriteCmd(i2c_bus,0xc8); //Set COM Output Scan Direction
    WriteCmd(i2c_bus,0x00); //---set low column address
    WriteCmd(i2c_bus,0x10); //---set high column address
    WriteCmd(i2c_bus,0x40); //--set start line address
    WriteCmd(i2c_bus,0x81); //--set contrast control register
    WriteCmd(i2c_bus,0xff); //亮度调节 0x00~0xff
    WriteCmd(i2c_bus,0xa1); //--set segment re-map 0 to 127
    WriteCmd(i2c_bus,0xa6); //--set normal display
    WriteCmd(i2c_bus,0xa8); //--set multiplex ratio(1 to 64)
    WriteCmd(i2c_bus,0x3F); //
    WriteCmd(i2c_bus,0xa4); //0xa4,Output follows RAM content;0xa5,Output ignores RAM content
    WriteCmd(i2c_bus,0xd3); //-set display offset
    WriteCmd(i2c_bus,0x00); //-not offset
    WriteCmd(i2c_bus,0xd5); //--set display clock divide ratio/oscillator frequency
    WriteCmd(i2c_bus,0xf0); //--set divide ratio
    WriteCmd(i2c_bus,0xd9); //--set pre-charge period
    WriteCmd(i2c_bus,0x22); //
    WriteCmd(i2c_bus,0xda); //--set com pins hardware configuration
    WriteCmd(i2c_bus,0x12);
    WriteCmd(i2c_bus,0xdb); //--set vcomh
    WriteCmd(i2c_bus,0x20); //0x20,0.77xVcc
    WriteCmd(i2c_bus,0x8d); //--set DC-DC enable
    WriteCmd(i2c_bus,0x14); //
    WriteCmd(i2c_bus,0xaf); //--turn on oled panel

}

//全屏填充
void OLED_FILL(unsigned char fill_data)
{
    unsigned char m,n;
    for(m=0;m<8;m++){
        WriteCmd(i2c_bus,0xb0+m);
        WriteCmd(i2c_bus,0x00);
        WriteCmd(i2c_bus,0x10);

        for(n=0;n<128;n++){
            WriteDat(i2c_bus,fill_data);
        }
    }
}

//OLED清屏
void OLED_CLS(void)
{
    OLED_FILL(0x00);
}




void OLED_SetPos(unsigned char x, unsigned char y) //设置起始点坐标
{
    WriteCmd(i2c_bus,0xb0+y);
    WriteCmd(i2c_bus,((x&0xf0)>>4)|0x10);
    WriteCmd(i2c_bus,(x&0x0f)|0x01);
}


const unsigned char F6x8[][6] =
{
    {0x00, 0x00, 0x00, 0x00, 0x00, 0x00},// sp
    {0x00, 0x00, 0x00, 0x2f, 0x00, 0x00},// !
    {0x00, 0x00, 0x07, 0x00, 0x07, 0x00},// "
    {0x00, 0x14, 0x7f, 0x14, 0x7f, 0x14},// #
    {0x00, 0x24, 0x2a, 0x7f, 0x2a, 0x12},// $
    {0x00, 0x62, 0x64, 0x08, 0x13, 0x23},// %
    {0x00, 0x36, 0x49, 0x55, 0x22, 0x50},// &
    {0x00, 0x00, 0x05, 0x03, 0x00, 0x00},// '
    {0x00, 0x00, 0x1c, 0x22, 0x41, 0x00},// (
    {0x00, 0x00, 0x41, 0x22, 0x1c, 0x00},// )
    {0x00, 0x14, 0x08, 0x3E, 0x08, 0x14},// *
    {0x00, 0x08, 0x08, 0x3E, 0x08, 0x08},// +
    {0x00, 0x00, 0x00, 0xA0, 0x60, 0x00},// ,
    {0x00, 0x08, 0x08, 0x08, 0x08, 0x08},// -
    {0x00, 0x00, 0x60, 0x60, 0x00, 0x00},// .
    {0x00, 0x20, 0x10, 0x08, 0x04, 0x02},// /
    {0x00, 0x3E, 0x51, 0x49, 0x45, 0x3E},// 0
    {0x00, 0x00, 0x42, 0x7F, 0x40, 0x00},// 1
    {0x00, 0x42, 0x61, 0x51, 0x49, 0x46},// 2
    {0x00, 0x21, 0x41, 0x45, 0x4B, 0x31},// 3
    {0x00, 0x18, 0x14, 0x12, 0x7F, 0x10},// 4
    {0x00, 0x27, 0x45, 0x45, 0x45, 0x39},// 5
    {0x00, 0x3C, 0x4A, 0x49, 0x49, 0x30},// 6
    {0x00, 0x01, 0x71, 0x09, 0x05, 0x03},// 7
    {0x00, 0x36, 0x49, 0x49, 0x49, 0x36},// 8
    {0x00, 0x06, 0x49, 0x49, 0x29, 0x1E},// 9
    {0x00, 0x00, 0x36, 0x36, 0x00, 0x00},// :
    {0x00, 0x00, 0x56, 0x36, 0x00, 0x00},// ;
    {0x00, 0x08, 0x14, 0x22, 0x41, 0x00},// <
    {0x00, 0x14, 0x14, 0x14, 0x14, 0x14},// =
    {0x00, 0x00, 0x41, 0x22, 0x14, 0x08},// >
    {0x00, 0x02, 0x01, 0x51, 0x09, 0x06},// ?
    {0x00, 0x32, 0x49, 0x59, 0x51, 0x3E},// @
    {0x00, 0x7C, 0x12, 0x11, 0x12, 0x7C},// A
    {0x00, 0x7F, 0x49, 0x49, 0x49, 0x36},// B
    {0x00, 0x3E, 0x41, 0x41, 0x41, 0x22},// C
    {0x00, 0x7F, 0x41, 0x41, 0x22, 0x1C},// D
    {0x00, 0x7F, 0x49, 0x49, 0x49, 0x41},// E
    {0x00, 0x7F, 0x09, 0x09, 0x09, 0x01},// F
    {0x00, 0x3E, 0x41, 0x49, 0x49, 0x7A},// G
    {0x00, 0x7F, 0x08, 0x08, 0x08, 0x7F},// H
    {0x00, 0x00, 0x41, 0x7F, 0x41, 0x00},// I
    {0x00, 0x20, 0x40, 0x41, 0x3F, 0x01},// J
    {0x00, 0x7F, 0x08, 0x14, 0x22, 0x41},// K
    {0x00, 0x7F, 0x40, 0x40, 0x40, 0x40},// L
    {0x00, 0x7F, 0x02, 0x0C, 0x02, 0x7F},// M
    {0x00, 0x7F, 0x04, 0x08, 0x10, 0x7F},// N
    {0x00, 0x3E, 0x41, 0x41, 0x41, 0x3E},// O
    {0x00, 0x7F, 0x09, 0x09, 0x09, 0x06},// P
    {0x00, 0x3E, 0x41, 0x51, 0x21, 0x5E},// Q
    {0x00, 0x7F, 0x09, 0x19, 0x29, 0x46},// R
    {0x00, 0x46, 0x49, 0x49, 0x49, 0x31},// S
    {0x00, 0x01, 0x01, 0x7F, 0x01, 0x01},// T
    {0x00, 0x3F, 0x40, 0x40, 0x40, 0x3F},// U
    {0x00, 0x1F, 0x20, 0x40, 0x20, 0x1F},// V
    {0x00, 0x3F, 0x40, 0x38, 0x40, 0x3F},// W
    {0x00, 0x63, 0x14, 0x08, 0x14, 0x63},// X
    {0x00, 0x07, 0x08, 0x70, 0x08, 0x07},// Y
    {0x00, 0x61, 0x51, 0x49, 0x45, 0x43},// Z
    {0x00, 0x00, 0x7F, 0x41, 0x41, 0x00},// [
    {0x00, 0x55, 0x2A, 0x55, 0x2A, 0x55},// 55
    {0x00, 0x00, 0x41, 0x41, 0x7F, 0x00},// ]
    {0x00, 0x04, 0x02, 0x01, 0x02, 0x04},// ^
    {0x00, 0x40, 0x40, 0x40, 0x40, 0x40},// _
    {0x00, 0x00, 0x01, 0x02, 0x04, 0x00},// '
    {0x00, 0x20, 0x54, 0x54, 0x54, 0x78},// a
    {0x00, 0x7F, 0x48, 0x44, 0x44, 0x38},// b
    {0x00, 0x38, 0x44, 0x44, 0x44, 0x20},// c
    {0x00, 0x38, 0x44, 0x44, 0x48, 0x7F},// d
    {0x00, 0x38, 0x54, 0x54, 0x54, 0x18},// e
    {0x00, 0x08, 0x7E, 0x09, 0x01, 0x02},// f
    {0x00, 0x18, 0xA4, 0xA4, 0xA4, 0x7C},// g
    {0x00, 0x7F, 0x08, 0x04, 0x04, 0x78},// h
    {0x00, 0x00, 0x44, 0x7D, 0x40, 0x00},// i
    {0x00, 0x40, 0x80, 0x84, 0x7D, 0x00},// j
    {0x00, 0x7F, 0x10, 0x28, 0x44, 0x00},// k
    {0x00, 0x00, 0x41, 0x7F, 0x40, 0x00},// l
    {0x00, 0x7C, 0x04, 0x18, 0x04, 0x78},// m
    {0x00, 0x7C, 0x08, 0x04, 0x04, 0x78},// n
    {0x00, 0x38, 0x44, 0x44, 0x44, 0x38},// o
    {0x00, 0xFC, 0x24, 0x24, 0x24, 0x18},// p
    {0x00, 0x18, 0x24, 0x24, 0x18, 0xFC},// q
    {0x00, 0x7C, 0x08, 0x04, 0x04, 0x08},// r
    {0x00, 0x48, 0x54, 0x54, 0x54, 0x20},// s
    {0x00, 0x04, 0x3F, 0x44, 0x40, 0x20},// t
    {0x00, 0x3C, 0x40, 0x40, 0x20, 0x7C},// u
    {0x00, 0x1C, 0x20, 0x40, 0x20, 0x1C},// v
    {0x00, 0x3C, 0x40, 0x30, 0x40, 0x3C},// w
    {0x00, 0x44, 0x28, 0x10, 0x28, 0x44},// x
    {0x00, 0x1C, 0xA0, 0xA0, 0xA0, 0x7C},// y
    {0x00, 0x44, 0x64, 0x54, 0x4C, 0x44},// z
    {0x14, 0x14, 0x14, 0x14, 0x14, 0x14}// horiz lines
};

void OLED_ShowStr(unsigned char x, unsigned char y, unsigned char ch[])
{
    unsigned char c = 0,i = 0,j = 0;
    while(ch[j] != '\0')
    {
        c = ch[j] - 32;
        if(x > 126)
        {
            x = 0;
            y++;
        }
        OLED_SetPos(x,y);
        for(i=0;i<6;i++)
            WriteDat(i2c_bus,F6x8[c][i]);
        x += 6;
        j++;
    }
}
int main(void)
{
    rt_err_t result=RT_NULL;
    i2c_bus=(struct rt_i2c_bus_device *)rt_device_find("i2c1");
    if(i2c_bus==RT_NULL){
        rt_kprintf("no device \n");
    }else{
        rt_kprintf("find device \n");

        //初始化程序段
        OLED_INIT();
        OLED_CLS();

        OLED_ShowStr(0,3,"i am king");
    }


    return RT_EOK;
}


相关操作不应该全部放在main.c里面,本文仅做测试。
学习了一下还是挺爽的,下期再见

  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
RT-Thread作品秀】OLED显示演示作者:谢博翔 概述初步体验 RT-Thread出的RTT核心版 ART-Pi,使用RTT用OLED显示 开发环境硬件:ART-Pi RT-Thread版本:4.0.3 开发工具及版本:RT-Thread Studio 2.0 RT-Thread使用情况概述软件包:使用u8g2 U8g2是嵌入式设备的单色图形库,目前支持单色OLED和LCD,包括以下控制器:SSD1305,SSD1306等 选用u8g2的原因: U8g2库平台支持性好,基本上支持绝大部分开发板,并且RTT已经有对应的软件包 U8g2库显示控制器支持性好,基本上市面上的OLED都完美支持; U8g2库API众多,特别支持了中文,支持了不同字体,这是一个对于开发者俩说不小的福利 硬件框架软件框架说明添加u8g2软件包,修改I2C接口和显示文字,编译、烧录。运行程序,打开监视串口:输入shell:u8g2_ssd1306_12864_sw_i2c_example 软件模块说明修改I2C接口 输入显示的内容 定义 RT_USINF_PIN 演示效果视频地址比赛感悟应改要多学习,多认真思考,多动手实践,不应该只停留在基础的实践,应该多coding,而且掌握好时间; 未来应该是RTOS和Linux的天下,现在 RT-Thread开源,是一件很好的事情;虽然国内开源的RTOS的已经好几家, 但现在看来,生态做的又好,软件又好用的,可能 RT-Thread 是最好的! 也感谢电路城这次的活动,以前对电路城并不是很了解,这次上传资料,简单看了下,感觉收获多多,也希望 后面能在这里学到和提供一些东西~ 加油

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值