找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3502|回复: 0
收起左侧

51单片机OLED,SPI液晶屏测试程序

[复制链接]
ID:382699 发表于 2020-11-1 12:38 | 显示全部楼层 |阅读模式
51hei截图_20201031232804.png

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

#define  u8 unsigned char
#define  u32 unsigned int

sbit CS=P0^4; //片选
sbit RST =P0^2;//复位
sbit DC =P0^3;//数据/命令控制
sbit SCL=P0^0;//时钟 D0(SCLK?
sbit SDIN=P0^1;//D1(MOSI) 数据

unsigned char code Hzk[][32]={

{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++)
        {                          
                SCL=0;
                if(dat&0x80)
                        {
                   SDIN=1;
                        }
else
                   SDIN=0;
                                SCL=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[2*no][t],1);
                                adder+=1;
     }      
                OLED_Set_Pos(x,y+1);      
    for(t=0;t<16;t++)
                        {      
                                OLED_WR_Byte(Hzk[2*no+1][t],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 address  Set 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);
  }
}

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表