//------------------------ I/O port definition --------------------
#include "reg51.h"
#include "intrins.h"
 
#define  port P1
#define  uchar  unsigned char
#define  uint   unsigned int
 
sbit RD1=P3^0;     //WR1
sbit RW=P3^1;      //WR0
sbit RS=P3^2;      //CD
sbit RES=P3^3;     //RST
sbit CS=P3^4;      //CS0
sbit sda=P1^1;
sbit scl=P1^0;
sbit pause=P2^0;     
 

uchar vop=0x37;
 
void flash();
void delay(uint t);
void contrast();
void write_byte(uchar data1);
void writed(uchar data1);
void WriteAddress(uchar c);
 

uchar code log1[]={/*--  调入了一幅图像：G:\Program\OLED\HP6432_20_SSD1315_I2C\6432.bmp  --*/
/*--  宽度x高度=64x32  --*/
0xFF,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0xF9,0x01,0x01,0x01,0x89,0x69,
0x19,0x09,0x01,0x21,0x21,0x01,0x89,0x69,0x19,0x09,0x01,0xF1,0x09,0x09,0xF9,0x01,
0x09,0x01,0x01,0x89,0x69,0x19,0x09,0x01,0x89,0x69,0x19,0x09,0x01,0xF1,0x09,0x09,
0xF9,0x01,0x89,0x69,0x19,0x09,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0xFF,
0xFF,0x00,0x00,0x00,0x00,0xC0,0xA0,0xA0,0x40,0x80,0xE3,0x81,0x80,0x80,0x01,0x22,
0x02,0x01,0xC0,0x20,0x20,0xE0,0x01,0x02,0x02,0x01,0x00,0x01,0x22,0x22,0x21,0x20,
0xE0,0x00,0x00,0x01,0xE2,0x02,0x01,0x00,0xE1,0x02,0x02,0xE1,0x00,0x01,0x02,0xE2,
0x01,0x00,0xC1,0x22,0xC2,0x01,0xC0,0x20,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,
0xFF,0x00,0x00,0x00,0x00,0x07,0x08,0x08,0x07,0x00,0x8F,0x44,0x42,0xC1,0x00,0x40,
0x40,0xC0,0x47,0x08,0x08,0x07,0x00,0x40,0x40,0xC0,0x40,0x00,0x89,0x49,0x49,0x89,
0x0F,0xC0,0x08,0x08,0x0F,0x08,0x88,0x40,0x4F,0x80,0x00,0x0F,0x01,0x01,0xC1,0x0F,
0x00,0xCC,0x03,0x00,0x07,0xC8,0x07,0x00,0x03,0x0C,0x00,0x00,0x00,0x00,0x00,0xFF,
0xFF,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x8F,0x90,0x90,0x8F,0x80,0x8C,
0x93,0x90,0x88,0x80,0x81,0x81,0x80,0x8C,0x93,0x90,0x88,0x80,0x8D,0x92,0x90,0x88,
0x81,0x9F,0x89,0x85,0x83,0x80,0x8B,0x94,0x94,0x8F,0x80,0x9E,0x92,0x92,0x9F,0x80,
0x80,0x9F,0x82,0x82,0x82,0x9F,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0xFF



,
};
 
void wait()
 {
  while(pause)
  {
    ;
   }
  
  }
 
void delay(uint times)
 {
  uint i,j;
  for(i=0;i<times;i++)
  
   {
   	 for(j=0;j<20;j++);
   }
  }

//---------------------------------------------------------------------
void flash()

{ unsigned char t1=4;

   while(t1>0)t1--;

}

//---------------------------------------------------------------------
void start()

 { sda=1; flash();scl=1;flash(); sda=0; flash(); scl=0; flash();}

//---------------------------------------------------------------------
void stop()

 { scl=0; flash();sda=0;flash(); scl=1; flash(); sda=1; flash(); }

//---------------------------------------------------------------------
 void write_byte(unsigned char dat)
{
  unsigned char k,i=200;
  for (k=0;k<8;k++)
   {  
      dat=dat<<1;
      sda=CY;
      flash();
      scl=1;
      flash();
      scl=0;
      flash();
   }
  sda=1;
  flash ();
  scl=1;
  flash();
  while (sda==1&&i--);
  scl=0;
}

//---------------------------------------------------------------------
void init()
 {
   RES=1;
   delay(1000);
   RES=0;
   delay(1000);
   RES=1;
   delay(1000);

   start();
   write_byte(0x78);//set slave adress

   write_byte(0x80);
   write_byte(0xAE);//set display display ON/OFF,AFH/AEH

   write_byte(0x80);	
   write_byte(0x40);//set display start line:COM0	

   write_byte(0x80);
   write_byte(0x81);//set contrast control
   write_byte(0x80);
   write_byte(0x75);	
   
   write_byte(0x80);
   write_byte(0x20);//set memory addressing mode
   write_byte(0x80);
   write_byte(0x02);//page addressing mode		
   
   write_byte(0x80);
   write_byte(0xA0);//set segment re-map
   
   write_byte(0x80);
   write_byte(0xA4);//entire display on: A4H:OFF/A5H:ON

   write_byte(0x80);
   write_byte(0xA6);//set normal/inverse display: A6H:normal/A7H:inverse

   write_byte(0x80);
   write_byte(0xA8);//set multiplex ratio
   write_byte(0x80);
   write_byte(0x1F);//1/32duty
   
   write_byte(0x80);
   write_byte(0xC0);//set com output scan direction

   write_byte(0x80);
   write_byte(0xD3);//set display  offset
   write_byte(0x80);
   write_byte(0x00);//

   write_byte(0x80);
   write_byte(0xD5);//set display  clock divide ratio/oscillator frequency
   write_byte(0x80);
   write_byte(0x80);//105Hz

   write_byte(0x80);
   write_byte(0xD9);//set pre-charge period
   write_byte(0x80);
   write_byte(0x1F);//

   write_byte(0x80);
   write_byte(0xDA);//set com pins hardware configuration
   write_byte(0x80);
   write_byte(0x12);//

   write_byte(0x80);
   write_byte(0xDB);//set vcomh deselect level
   write_byte(0x80);
   write_byte(0x30);//0.83*VCC

   write_byte(0x80);		
   write_byte(0x8D);//charge pump setting
   write_byte(0x80);
 //  write_byte(0x14);//enable charge pump,VCC=7.5V
   write_byte(0x10);//禁用内部倍压电路

   write_byte(0x80);
   write_byte(0xAF);//set display display ON/OFF,AEH/AFH 
  
   stop();
    
  }

//--------------------------------------------------------------------- 
void display_pic(uchar *p)
 {
   uchar i,j;
   uint k=0; 
      
   for(i=0;i<4;i++)
    {
     start();
     write_byte(0x78);  //set slave adress

     write_byte(0x80);
     write_byte(0xB0+i);//set page address

     write_byte(0x80);
     write_byte(0x00);  //set col address LSB
     write_byte(0x80);
     write_byte(0x12);  //set col address MSB

     write_byte(0x40);   
    
     for(j=0;j<64;j++) 
      {        
        write_byte(*p++);
      }      
     stop();
    }
   
 }

//---------------------------------------------------------------------  
void display(uchar dat1,uchar dat2)
  {
    uchar row,i;  
   
    for(row=0; row<4; row++) 
     { start();
       write_byte(0x78);  //set slave adress

       write_byte(0x80);
       write_byte(0xB0+row);//set page address

       write_byte(0x80);
       write_byte(0x00);  //set col address LSB
       write_byte(0x80);
       write_byte(0x12);  //set col address MSB

       write_byte(0x40);
                                  
       for(i=0; i<32; i++)
        {
         write_byte(dat1);
         write_byte(dat2);
        
        }
      
       stop();
     }
   
  }


  //---------------------------------------------------------------------  
void display1(uchar dat1,uchar dat2)
  {
    uchar row,i;  
   
    for(row=0; row<4; row++) 
     { start();
       write_byte(0x78);  //set slave adress

       write_byte(0x80);
       write_byte(0xB0+row);//set page address

       write_byte(0x80);
       write_byte(0x00);  //set col address LSB
       write_byte(0x80);
       write_byte(0x12);  //set col address MSB

       write_byte(0x40);
                                  
       for(i=0; i<16; i++)
        {
         write_byte(dat1);
         write_byte(dat1);
         write_byte(dat2);
         write_byte(dat2);
        
        }
      
       stop();
     }
   
  }



    
//-----------------------------
void main ()
   {
    
    delay(200);
    init();
    while(1)
     {
      start();
      write_byte(0x78);//set slave adress

      write_byte(0x80);
      write_byte(0x81);//set contrast control
      write_byte(0x80);
      write_byte(0x02);

      stop();   
      display(0xff,0xff);
      delay(2000);

      start();
      write_byte(0x78);//set slave adress

      write_byte(0x80);
      write_byte(0x81);//set contrast control
      write_byte(0x80);
      write_byte(0x75);

      stop();   
      display(0xff,0xff);
      delay(2000);


       
      //display(0x00,0X00);
      //delay(2000);

      display1(0x33,0XCC);
      delay(2000);
      
      //display(0xAA,0XAA);
      //delay(2000);
       
      //display(0xFF,0X00);
      //delay(2000);
      
      display(0x55,0XAA);
      delay(2000);
            
      display_pic(log1); 
      delay(2000);
      //wait();
           

      
      }
    }



