分立元件 发表于 2020-1-8 14:02:08

分享一些常用的液晶屏C51测试程序!

本帖最后由 分立元件 于 2020-1-8 14:12 编辑

经过几天的努力,终于把这几个液晶屏的51单片机驱动程序找全了,特地来分享给大家,现在网上的资料真是太难找了,网络越来越封闭了,技术资料都是抄来抄去的:Q !

OLED 0.69寸12864液晶屏(IIC和SPI接口)、5110液晶屏、1602液晶屏。

12864 OLED SPI 程序:




#include<reg52.h>
#include<intrins.h>

#defineu8 unsigned char
#defineu32 unsigned int
#define OLED_CMD0      //写命令
#define OLED_DATA 1      //写数据
#define OLED_MODE 0

#define OLED_CS_Clr()OLED_CS=0
#define OLED_CS_Set()OLED_CS=1

#define OLED_RST_Clr() OLED_RST=0
#define OLED_RST_Set() OLED_RST=1

#define OLED_DC_Clr() OLED_DC=0
#define OLED_DC_Set() OLED_DC=1

#define OLED_SCLK_Clr() OLED_SCL=0
#define OLED_SCLK_Set() OLED_SCL=1

#define OLED_SDIN_Clr() OLED_SDIN=0
#define OLED_SDIN_Set() OLED_SDIN=1;

#define SIZE 16
#define XLevelL                0x02
#define XLevelH                0x10
#define Max_Column      128
#define Max_Row                64
#define      Brightness      0xFF
#define X_WIDTH         128
#define Y_WIDTH         64      

sbit OLED_CS=P0^4; //片选
sbit OLED_RST =P0^2;//复位
sbit OLED_DC =P0^3;//数据/命令控制
sbit OLED_SCL=P0^0;//时钟 D0(SCLK?
sbit OLED_SDIN=P0^1;//D1(MOSI) 数据

unsigned char code Hzk[]={

{0x00,0x00,0xF8,0x88,0x88,0x88,0x88,0xFF,0x88,0x88,0x88,0x88,0xF8,0x00,0x00,0x00},
{0x00,0x00,0x1F,0x08,0x08,0x08,0x08,0x7F,0x88,0x88,0x88,0x88,0x9F,0x80,0xF0,0x00},/*"电",0*/

{0x10,0x10,0xD0,0xFF,0x90,0x10,0x00,0xFE,0x02,0x02,0x02,0xFE,0x00,0x00,0x00,0x00},
{0x04,0x03,0x00,0xFF,0x00,0x83,0x60,0x1F,0x00,0x00,0x00,0x3F,0x40,0x40,0x78,0x00},/*"机",1*/

{0x00,0x08,0x30,0x00,0xFF,0x20,0x20,0x20,0x20,0xFF,0x20,0x20,0x22,0x2C,0x20,0x00},
{0x04,0x04,0x02,0x01,0xFF,0x80,0x40,0x30,0x0E,0x01,0x06,0x18,0x20,0x40,0x80,0x00},/*"状",2*/

{0x00,0x04,0x84,0x84,0x44,0x24,0x54,0x8F,0x14,0x24,0x44,0x84,0x84,0x04,0x00,0x00},
{0x41,0x39,0x00,0x00,0x3C,0x40,0x40,0x42,0x4C,0x40,0x40,0x70,0x04,0x09,0x31,0x00},/*"态",3*/

{0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*":",4*/

{0x00,0x02,0x02,0xC2,0x02,0x02,0x02,0xFE,0x82,0x82,0x82,0x82,0x82,0x02,0x00,0x00},
{0x40,0x40,0x40,0x7F,0x40,0x40,0x40,0x7F,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00},/*"正",5*/

{0xC8,0xB8,0x8F,0xE8,0x88,0x88,0x40,0x48,0x48,0xE8,0x5F,0x48,0x48,0x48,0x40,0x00},
{0x08,0x18,0x08,0xFF,0x04,0x04,0x00,0x02,0x0B,0x12,0x22,0xD2,0x0A,0x06,0x00,0x00},/*"转",6*/

{0x00,0x00,0xFC,0x24,0x24,0xE4,0x24,0x24,0x22,0x22,0x22,0xA3,0x62,0x00,0x00,0x00},
{0x40,0x30,0x8F,0x80,0x40,0x40,0x23,0x14,0x08,0x14,0x22,0x41,0x40,0x80,0x80,0x00},/*"反",7*/

};

void delay_ms(unsigned int ms)
{                        
      unsigned int a;
      while(ms)
      {
                a=1800;
                while(a--);
                ms--;
      }
      return;
}

void OLED_WR_Byte(u8 dat,u8 cmd)
{      
      u8 i;                        
      if(cmd)
          OLED_DC_Set();
      else
          OLED_DC_Clr();                  
      OLED_CS_Clr();
      for(i=0;i<8;i++)
      {                        
                OLED_SCLK_Clr();
                if(dat&0x80)
                        {
                   OLED_SDIN_Set();
                        }
else
                   OLED_SDIN_Clr();
                              OLED_SCLK_Set();
                dat<<=1;   
      }                                                   
      OLED_CS_Set();
      OLED_DC_Set();            
}

void OLED_Clear(void)
{
      u8 i,n;                  
      for(i=0;i<8;i++)
      {
                OLED_WR_Byte (0xb0+i,OLED_CMD);    //设置页地址(0~7)
                OLED_WR_Byte (0x00,OLED_CMD);      //设置显示位置—列低地址
                OLED_WR_Byte (0x10,OLED_CMD);      //设置显示位置—列高地址   
                for(n=0;n<128;n++)OLED_WR_Byte(0,OLED_DATA);
      } //更新显示
}

void OLED_Set_Pos(unsigned char x, unsigned char y)
{
      OLED_WR_Byte(0xb0+y,OLED_CMD);
      OLED_WR_Byte(((x&0xf0)>>4)|0x10,OLED_CMD);
      OLED_WR_Byte((x&0x0f)|0x01,OLED_CMD);
}   

void OLED_ShowCHinese(u8 x,u8 y,u8 no)
{                                 
      u8 t,adder=0;
      OLED_Set_Pos(x,y);      
    for(t=0;t<16;t++)
                {
                              OLED_WR_Byte(Hzk,OLED_DATA);
                              adder+=1;
   }      
                OLED_Set_Pos(x,y+1);      
    for(t=0;t<16;t++)
                        {      
                              OLED_WR_Byte(Hzk,OLED_DATA);
                              adder+=1;
      }                                       
}

void OLED_Init(void)
{
    OLED_RST_Set();
      delay_ms(100);
      OLED_RST_Clr();
      delay_ms(100);
      OLED_RST_Set();
      OLED_WR_Byte(0xAE,OLED_CMD);//--turn off oled panel
      OLED_WR_Byte(0x00,OLED_CMD);//---set low column address
      OLED_WR_Byte(0x10,OLED_CMD);//---set high column address
      OLED_WR_Byte(0x40,OLED_CMD);//--set start line addressSet Mapping RAM Display Start Line (0x00~0x3F)
      OLED_WR_Byte(0x81,OLED_CMD);//--set contrast control register
      OLED_WR_Byte(0xCF,OLED_CMD); // Set SEG Output Current Brightness
      OLED_WR_Byte(0xA1,OLED_CMD);//--Set SEG/Column Mapping   0xa0左右反置 0xa1正常
      OLED_WR_Byte(0xC8,OLED_CMD);//Set COM/Row Scan Direction   0xc0上下反置 0xc8正常
      OLED_WR_Byte(0xA6,OLED_CMD);//--set normal display
      OLED_WR_Byte(0xA8,OLED_CMD);//--set multiplex ratio(1 to 64)
      OLED_WR_Byte(0x3f,OLED_CMD);//--1/64 duty
      OLED_WR_Byte(0xD3,OLED_CMD);//-set display offset      Shift Mapping RAM Counter (0x00~0x3F)
      OLED_WR_Byte(0x00,OLED_CMD);//-not offset
      OLED_WR_Byte(0xd5,OLED_CMD);//--set display clock divide ratio/oscillator frequency
      OLED_WR_Byte(0x80,OLED_CMD);//--set divide ratio, Set Clock as 100 Frames/Sec
      OLED_WR_Byte(0xD9,OLED_CMD);//--set pre-charge period
      OLED_WR_Byte(0xF1,OLED_CMD);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
      OLED_WR_Byte(0xDA,OLED_CMD);//--set com pins hardware configuration
      OLED_WR_Byte(0x12,OLED_CMD);
      OLED_WR_Byte(0xDB,OLED_CMD);//--set vcomh
      OLED_WR_Byte(0x40,OLED_CMD);//Set VCOM Deselect Level
      OLED_WR_Byte(0x20,OLED_CMD);//-Set Page Addressing Mode (0x00/0x01/0x02)
      OLED_WR_Byte(0x02,OLED_CMD);//
      OLED_WR_Byte(0x8D,OLED_CMD);//--set Charge Pump enable/disable
      OLED_WR_Byte(0x14,OLED_CMD);//--set(0x10) disable
      OLED_WR_Byte(0xA4,OLED_CMD);// Disable Entire Display On (0xa4/0xa5)
      OLED_WR_Byte(0xA6,OLED_CMD);// Disable Inverse Display On (0xa6/a7)
      OLED_WR_Byte(0xAF,OLED_CMD);//--turn on oled panel
      
      OLED_WR_Byte(0xAF,OLED_CMD); /*display ON*/
      OLED_Clear();
      OLED_Set_Pos(0,0);      
}

void oled()
{
      OLED_ShowCHinese(0,3,0);//电
      OLED_ShowCHinese(18,3,1);//机
      OLED_ShowCHinese(36,3,2);//状
      OLED_ShowCHinese(54,3,3);//态
      OLED_ShowCHinese(72,3,4);//:
      OLED_ShowCHinese(90,3,5);//:
      OLED_ShowCHinese(108,3,6);//转
}

void oled1()
{
      OLED_ShowCHinese(0,4,0);//电
      OLED_ShowCHinese(18,4,1);//机
      OLED_ShowCHinese(36,4,2);//状
      OLED_ShowCHinese(54,4,3);//态
      OLED_ShowCHinese(72,4,4);//:
      OLED_ShowCHinese(90,4,7);//:
      OLED_ShowCHinese(108,4,6);//转
}

void main()
{
OLED_Init();
OLED_Clear();
oled();
delay_ms(500);
OLED_Clear();
oled1();
while(1);
}

系统限制只能下载:

补充内容 (2020-1-9 09:51):
1602有个数值打错了:for(m=0;m<19;m++) //19改16才对,要不然第一行多输出3个乱码
        {
          shuju(table);         
      delay(200);
        }      

补充内容 (2020-1-9 13:06):
OLED 12864 IIC的是双色屏程序

washu 发表于 2020-1-8 15:02:43

楼主注意几个问题,

1、主控,这个驱动能不能用取决于你的屏的主控

2、默认值,很多主控具有上电或重启后的默认值,比如很多小 OLED 主控是 SSD1306、1309,它手册写了那些是默认值,如果是默认值的你不需要重复发送这些配置指令去初始化它。

此外,最好拿到屏幕制造商的说明,看看哪些项目应该配置为非默认值。比如从左往右还是从右往左、从上往下还是从下往上写,其实和你的屏幕安装方式有关,你要倒过来 180 度安装屏幕,写顺序就要反过来,否则就可以用默认值不需要配置。

分立元件 发表于 2020-1-8 16:08:06

washu 发表于 2020-1-8 15:02
楼主注意几个问题,

1、主控,这个驱动能不能用取决于你的屏的主控


谢谢你了,没有注意到这些问题

有保电器 发表于 2020-1-9 10:15:16

学习中,谢谢分享

分立元件 发表于 2020-1-9 11:50:56

本帖最后由 分立元件 于 2020-1-9 12:03 编辑

化简 12864 OLED SPI51单片机源程序

#include<reg52.h>
#include<intrins.h>

#defineu8 unsigned char
#defineu32 unsigned int

sbit CS=P0^4; //片选
sbit DC =P0^3;//数据/命令控制
sbit RST =P0^2;//复位
sbit SDA=P0^1;//据数
sbit SCK=P0^0;//时钟


unsigned char code Hzk[]={

{0x00,0x00,0xF8,0x88,0x88,0x88,0x88,0xFF,0x88,0x88,0x88,0x88,0xF8,0x00,0x00,0x00},
{0x00,0x00,0x1F,0x08,0x08,0x08,0x08,0x7F,0x88,0x88,0x88,0x88,0x9F,0x80,0xF0,0x00},/*"电",0*/

{0x10,0x10,0xD0,0xFF,0x90,0x10,0x00,0xFE,0x02,0x02,0x02,0xFE,0x00,0x00,0x00,0x00},
{0x04,0x03,0x00,0xFF,0x00,0x83,0x60,0x1F,0x00,0x00,0x00,0x3F,0x40,0x40,0x78,0x00},/*"机",1*/

{0x00,0x08,0x30,0x00,0xFF,0x20,0x20,0x20,0x20,0xFF,0x20,0x20,0x22,0x2C,0x20,0x00},
{0x04,0x04,0x02,0x01,0xFF,0x80,0x40,0x30,0x0E,0x01,0x06,0x18,0x20,0x40,0x80,0x00},/*"状",2*/

{0x00,0x04,0x84,0x84,0x44,0x24,0x54,0x8F,0x14,0x24,0x44,0x84,0x84,0x04,0x00,0x00},
{0x41,0x39,0x00,0x00,0x3C,0x40,0x40,0x42,0x4C,0x40,0x40,0x70,0x04,0x09,0x31,0x00},/*"态",3*/

{0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*":",4*/

{0x00,0x02,0x02,0xC2,0x02,0x02,0x02,0xFE,0x82,0x82,0x82,0x82,0x82,0x02,0x00,0x00},
{0x40,0x40,0x40,0x7F,0x40,0x40,0x40,0x7F,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00},/*"正",5*/

{0xC8,0xB8,0x8F,0xE8,0x88,0x88,0x40,0x48,0x48,0xE8,0x5F,0x48,0x48,0x48,0x40,0x00},
{0x08,0x18,0x08,0xFF,0x04,0x04,0x00,0x02,0x0B,0x12,0x22,0xD2,0x0A,0x06,0x00,0x00},/*"转",6*/

{0x00,0x00,0xFC,0x24,0x24,0xE4,0x24,0x24,0x22,0x22,0x22,0xA3,0x62,0x00,0x00,0x00},
{0x40,0x30,0x8F,0x80,0x40,0x40,0x23,0x14,0x08,0x14,0x22,0x41,0x40,0x80,0x80,0x00},/*"反",7*/

};

void delay_ms(unsigned int ms)
{                        
      unsigned int a;
      while(ms)
      {
                a=1800;
                while(a--);
                ms--;
      }
      return;
}

void OLED_WR_Byte(u8 dat,u8 cmd)                       
{      
      u8 i;                        
      if(cmd)
          DC=1;
      else
          DC=0;                  
          CS=0;
      for(i=0;i<8;i++)
      {                        
                SCK=0;
                if(dat&0x80)
                        {
                   SDA=1;
                        }
else
                   SDA=0;
                   SCK=1;
                dat<<=1;   
      }                                                   
      CS=1;
      DC=1;            
}

void OLED_Clear(void)
{
      u8 i,n;                  
      for(i=0;i<8;i++)
      {
                OLED_WR_Byte (0xb0+i,0);    //设置页地址(0~7)
                OLED_WR_Byte (0x00,0);      //设置显示位置—列低地址
                OLED_WR_Byte (0x10,0);      //设置显示位置—列高地址   
                for(n=0;n<128;n++)OLED_WR_Byte(0,1);
      } //更新显示
}

void OLED_Set_Pos(unsigned char x, unsigned char y)
{
      OLED_WR_Byte(0xb0+y,0);
      OLED_WR_Byte(((x&0xf0)>>4)|0x10,0);
      OLED_WR_Byte((x&0x0f)|0x01,0);
}   

void OLED_ShowCHinese(u8 x,u8 y,u8 no)
{                                 
    u8 t,adder=0;
    OLED_Set_Pos(x,y);
                     
    for(t=0;t<16;t++)
    {
   OLED_WR_Byte(Hzk,1);
   adder+=1;
   }      
   OLED_Set_Pos(x,y+1);      
   for(t=0;t<16;t++)
    {      
   OLED_WR_Byte(Hzk,1);
   adder+=1;
    }                                       
}

void OLED_Init(void)                                  //初始化
{
      RST=1;
      delay_ms(100);
      RST=0;
      delay_ms(100);
      RST=1;
      OLED_WR_Byte(0xAE,0);//--turn off oled panel
      OLED_WR_Byte(0x00,0);//---set low column address
      OLED_WR_Byte(0x10,0);//---set high column address
      OLED_WR_Byte(0x40,0);//--set start line addressSet Mapping RAM Display Start Line (0x00~0x3F)
      OLED_WR_Byte(0x81,0);//--set contrast control register
      OLED_WR_Byte(0xCF,0); // Set SEG Output Current Brightness
      OLED_WR_Byte(0xA1,0);//--Set SEG/Column Mapping   0xa0左右反置 0xa1正常
      OLED_WR_Byte(0xC8,0);//Set COM/Row Scan Direction   0xc0上下反置 0xc8正常
      OLED_WR_Byte(0xA6,0);//--set normal display
      OLED_WR_Byte(0xA8,0);//--set multiplex ratio(1 to 64)
      OLED_WR_Byte(0x3f,0);//--1/64 duty
      OLED_WR_Byte(0xD3,0);//-set display offset      Shift Mapping RAM Counter (0x00~0x3F)
      OLED_WR_Byte(0x00,0);//-not offset
      OLED_WR_Byte(0xd5,0);//--set display clock divide ratio/oscillator frequency
      OLED_WR_Byte(0x80,0);//--set divide ratio, Set Clock as 100 Frames/Sec
      OLED_WR_Byte(0xD9,0);//--set pre-charge period
      OLED_WR_Byte(0xF1,0);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
      OLED_WR_Byte(0xDA,0);//--set com pins hardware configuration
      OLED_WR_Byte(0x12,0);
      OLED_WR_Byte(0xDB,0);//--set vcomh
      OLED_WR_Byte(0x40,0);//Set VCOM Deselect Level
      OLED_WR_Byte(0x20,0);//-Set Page Addressing Mode (0x00/0x01/0x02)
      OLED_WR_Byte(0x02,0);//
      OLED_WR_Byte(0x8D,0);//--set Charge Pump enable/disable
      OLED_WR_Byte(0x14,0);//--set(0x10) disable
      OLED_WR_Byte(0xA4,0);// Disable Entire Display On (0xa4/0xa5)
      OLED_WR_Byte(0xA6,0);// Disable Inverse Display On (0xa6/a7)
      OLED_WR_Byte(0xAF,0);//--turn on oled panel
      
      OLED_WR_Byte(0xAF,0); /*display ON*/
      OLED_Clear();
      OLED_Set_Pos(0,0);      
}

void oled()
{
      OLED_ShowCHinese(0,3,0);//电
      OLED_ShowCHinese(18,3,1);//机
      OLED_ShowCHinese(36,3,2);//状
      OLED_ShowCHinese(54,3,3);//态
      OLED_ShowCHinese(72,3,4);//:
      OLED_ShowCHinese(90,3,5);//正
      OLED_ShowCHinese(108,3,6);//转
}

void oled1()
{
      OLED_ShowCHinese(0,3,0);//电
      OLED_ShowCHinese(18,3,1);//机
      OLED_ShowCHinese(36,3,2);//状
      OLED_ShowCHinese(54,3,3);//态
      OLED_ShowCHinese(72,3,4);//:
      OLED_ShowCHinese(90,3,7);//反
      OLED_ShowCHinese(108,3,6);//转
}

void main()
{
OLED_Init();          //初始化液晶屏
while(1)
{
OLED_Clear();
oled();
delay_ms(50);
OLED_Clear();
oled1();
delay_ms(50);
}
}

风影欣 发表于 2020-1-12 17:01:45

谢谢分享!

zxf2690 发表于 2020-2-8 21:00:32

谢谢分享正需要

清风碧雪 发表于 2020-2-9 01:34:53

一般买这类OLED屏都给出非常详尽的程序,百度上的文库也可以通过软件免费下载。

bg6ib 发表于 2020-2-13 21:18:15

感谢楼主,屏驱动确实是问题,所以我用ARDUINO标准库,这样就很简单了
页: [1]
查看完整版本: 分享一些常用的液晶屏C51测试程序!