[应用相关] I2C接口的SSD1306---OLED驱动

[复制链接]
 楼主| dentsgot 发表于 2016-5-18 20:57 | 显示全部楼层 |阅读模式
/*
    这个液晶只需要两根线用模拟或者硬件的I2C都可以 硬件设计非常简单
*/
#define I2C_SDA_LOW()   
#define I2C_SDA_HIGH()

#define I2C_SCL_LOW()
#define I2C_SCL_HIGH()

#defien I2C_SDA_Read()

#define
BIRGTHNESS             //OLED的亮度  00~255

#define OLED_WIDTH    128
#define OLED_HIGH    (8*8)

#define I2C_OLED    0x78

#define I2C_Delay()    //实现延时4us

/* 见名知意 移植的时候只需要把以上的宏的具体实现方法写上去 就可以了  下面的代码用的是普通IO口模拟I2C (速度还是可以的) 这个芯片 不用上拉电阻 好像也能行
*/  

static void I2C_Start(void)
{
    I2C_SDA_HIGH();
    I2C_SCL_HIGH();
    I2C_Delay();
    I2C_SDA_LOW();
    I2C_Delay();
    I2C_SCL_LOW();
    I2C_Delay();
}

static void I2C_Stop(void)
{
    I2C_SDA_LOW();
    I2C_SCL_HIGH();
    I2C_Delay();
    I2C_SDA_HIGH();
    I2C_Delay();
}

static uint8_t I2C_WaitAck(void)  //0:aCK     1:NoAck
{
    I2C_SDA_HIGH();
    I2C_SCL_HIGH();
    I2C_Delay();
    if(I2C_SDA_Read())
        return 1;
    I2C_SCL_LOW();
    I2C_Delay();
    return 0;
}

static void I2C_SendByte(uint8_t Byte)
{
    uint8_t Cnt;
    for(Cnt=0;Cnt<8;Cnt++)
    {
        if(Byte&0x80)
            I2C_SDA_HIGH();
        else
            I2C_SDA_LOW();
        I2C_SCL_HIGH();
        I2C_Delay();
        I2C_SCL_LOW();
        I2C_Delay();
    }
}

static uint8_t OLED_WriteCmd(uint8_t Cmd)
{
    I2C_Start();
    I2C_SendByte(I2C_OLED);
    if(I2C_WaitAck())
        return 1;
    I2C_SendByte(0x00);
    if(I2C_WaitAck())
        return 2;
    I2C_SendByte(Cmd);
    if(I2C_WaitAck())
        return 3;
    I2C_Stop();
    return 0;
}

评分

参与人数 1威望 +3 收起 理由
SD10A + 3 7.37MHZ 不需要延时

查看全部评分

 楼主| dentsgot 发表于 2016-5-18 20:58 | 显示全部楼层
  1. static uint8_t OLED_WriteData(uint8_t Data)
  2. {
  3.     I2C_Start();
  4.     I2C_SendByte(I2C_OLED);
  5.     if(I2C_WaitAck())
  6.         return 1;
  7.     I2C_SendByte(0x40);
  8.     if(I2C_WaitAck())
  9.         return 2;
  10.     I2C_SendByte(Data);
  11.     if(I2C_WaitAck())
  12.         return 3;
  13.     I2C_Stop();
  14.     return 0;
  15. }

  16. static uint8_t OLED_SetXY(uint8_t x,uint8_t y)
  17. {
  18.     OLED_WriteCmd(0xb0|y);//y
  19.     OLED_WriteCmd(((0xf0&x)>>4))|0x10);//设置x高4位
  20.     OLED_WriteCmd(((0x0f&x)>>4))|0x01);//设置x低4位
  21. }

  22. uint8_t OLED_Clear(void)
  23. {
  24.     uint8_t x,y;
  25.     OLED_SetXY(0,0);
  26.     for(y=0;y<OLED_HIGH/8;y++)
  27.     {
  28.         if(OLED_SetXY(0,y))
  29.             return 1;
  30.         for(x=0;x<OLED_WIDTH;x++)
  31.         {
  32.             if(OLED_WriteData(0x00))
  33.                 return 2;
  34.         }
  35.     }
  36.     return 0;
  37. }

  38. uint8_t OLED_Init(void)
  39. {
  40.    // 延时一下 再初始化 OLED_Delay(100000);    if(OLED_WriteCmd(0xae))
  41.         return 1;//--turn off oled panel
  42.     if(OLED_WriteCmd(0x00))
  43.           return 2;//---set low column address
  44.     if(OLED_WriteCmd(0x10))
  45.         return 3;//---set high column address
  46.     if(OLED_WriteCmd(0x40))
  47.         return 4;//--set start line address  Set Mapping RAM Display Start Line (0x00~0x3F)
  48.     if(OLED_WriteCmd(0x81))
  49.         return 5;//--set contrast control register
  50.     if(OLED_WriteCmd(Brightness))
  51.         return 6; // Set SEG Output Current Brightness
  52.     if(OLED_WriteCmd(0xa1))
  53.         return 7;//--Set SEG/Column Mapping     0xa0左右反置 0xa1正常
  54.     if(OLED_WriteCmd(0xc8))
  55.         return 8;//Set COM/Row Scan Direction   0xc0上下反置 0xc8正常
  56.     if(OLED_WriteCmd(0xa6))
  57.         return 9;//--set normal display
  58.     if(OLED_WriteCmd(0xa8))
  59.         return 10;//--set multiplex ratio(1 to 64)
  60.     if(OLED_WriteCmd(0x3f))
  61.                 return 11;//--1/64 duty
  62.     if(OLED_WriteCmd(0xd3))
  63.         return 12;//-set display offset        Shift Mapping RAM Counter (0x00~0x3F)
  64.     if(OLED_WriteCmd(0x00))
  65.         return 13;//-not offset
  66.     if(OLED_WriteCmd(0xd5))
  67.         return 14;//--set display clock divide ratio/oscillator frequency
  68.     if(OLED_WriteCmd(0x80))
  69.         return 15;//--set divide ratio, Set Clock as 100 Frames/Sec
  70.     if(OLED_WriteCmd(0xd9))
  71.         return 16;//--set pre-charge period
  72.     if(OLED_WriteCmd(0xf1))
  73.         return 17;//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
  74.     if(OLED_WriteCmd(0xda))
  75.         return 18;//--set com pins hardware configuration
  76.     if(OLED_WriteCmd(0x12))
  77.         return 19;
  78.     if(OLED_WriteCmd(0xdb))
  79.         return 20;//--set vcomh
  80.     if(OLED_WriteCmd(0x40))
  81.         return 21;//Set VCOM Deselect Level
  82.     if(OLED_WriteCmd(0x20))
  83.         return 22;//-Set Page Addressing Mode (0x00/0x01/0x02)
  84.     if(OLED_WriteCmd(0x02))
  85.         return 23;//
  86.     if(OLED_WriteCmd(0x8d))
  87.         return 24;//--set Charge Pump enable/disable
  88.     if(OLED_WriteCmd(0x14))
  89.         return 25;//--set(0x10) disable
  90.     if(OLED_WriteCmd(0xa4))
  91.         return 26;// Disable Entire Display On (0xa4/0xa5)
  92.     if(OLED_WriteCmd(0xa6))
  93.         return 27;// Disable Inverse Display On (0xa6/a7)
  94.     if(OLED_WriteCmd(0xaf))
  95.         return 28;//--turn on oled panel
  96.     if(OLED_Clear(0x00))
  97.         return 29; //初始清屏
  98.     if(OLED_SetXY(0,0))
  99.         return 30;
  100.     return 0;
  101. }




yiyigirl2014 发表于 2016-5-18 23:33 | 显示全部楼层
哪个液晶?说一下型号啊,OLED的型号很多的。
baoanlcm 发表于 2016-5-19 14:39 | 显示全部楼层
可能是12864的
qgmfly 发表于 2016-10-19 17:02 | 显示全部楼层
楼主能打包分享一下吗?
aaronhu 发表于 2017-6-22 12:50 | 显示全部楼层
many thanks! code for reference!
SD10A 发表于 2017-11-2 20:59 | 显示全部楼层

我想问问LZ  横排显示 是哪个指令  我做的变成纵排的了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

29

主题

164

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部

29

主题

164

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部