
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include "reg51.h"  
#define mydata (( unsigned char xdata *)0x0076)  //記憶體起始位
sbit RES=P3^0;
sbit RD0=P3^1;
sbit INT00=P3^2;
sbit INT10=P3^3;
sbit DC=P3^4;
sbit CS=P3^5;
sbit WR0=P3^6;

//-----------------IIC start(不用時請關閉)---------------
sbit SCL=P1^0;
sbit SDA=P1^1;

//-----------------IIC end-------------------------------

void clear(void);
void delay(unsigned int a)
{
while(a!=0)
  a--;
}





//-------------------------(IIC start-----------------------------------------------
void IIC_START(void)
{
SCL=1;
SDA=1;
//delay(1);
SDA=0;
//delay(1);
SCL=0;
//delay(1);
}
//-------------------------IIC start)-----------------------------------------
//-------------------------(IIC stop------------------------------------------
void IIC_STOP(void)
{
SDA=0;


SCL=0;
//delay(1);

SCL=1;
//delay(1);
SDA=1;


//delay(1);
}
//-------------------------IIC stop)------------------------------------------
//-------------------------(IIC LOW------------------------------------------
void IIC_LOW(void)
{
SDA=0;
SCL=1;
//delay(1);
SCL=0;
//delay(1);

SDA=0;

}
//-------------------------IIC LOW)------------------------------------------
//-------------------------(IIC HIGH------------------------------------------
void IIC_HIGH(void)
{
SDA=1;
SCL=1;
//delay(1);
SCL=0;
//delay(1);
SDA=1;

}
//-------------------------IIC HIGH)------------------------------------------------
//-------------------------(IIC SERIAL Sned-----------------------------------------
void IIC_SERIAL(unsigned char a)
{
  if (((a&0x80)>>7) == 1)
  {IIC_HIGH();}
  else
  {IIC_LOW();}

  if (((a&0x40)>>6) == 1)
  {IIC_HIGH();}
  else
  {IIC_LOW();}

  if (((a&0x20)>>5) == 1)
  {IIC_HIGH();}
  else
  {IIC_LOW();}

  if (((a&0x10)>>4) == 1)
  {IIC_HIGH();}
  else
  {IIC_LOW();}

  if (((a&0x08)>>3) == 1)
  {IIC_HIGH();}
  else
  {IIC_LOW();}

  if (((a&0x04)>>2) == 1)
  {IIC_HIGH();}
  else
  {IIC_LOW();}

  if (((a&0x02)>>1) == 1)
  {IIC_HIGH();}
  else
  {IIC_LOW();}

  if ((a&0x01)>>0 == 1)
  {IIC_HIGH();}
  else
  {IIC_LOW();}

SCL=0;
//delay(1);
SCL=1;
//delay(1);
SCL=0;
//delay(1);
}
//-------------------------IIC SERIAL Sned)-----------------------------------------
void data_out(char a)
{
IIC_START();
IIC_SERIAL(0x78);  //Slave Address
IIC_SERIAL(0xc0); //Control Byte
IIC_SERIAL(a);
IIC_STOP();
}


void comm_out(char a)
{
IIC_START();
IIC_SERIAL(0x78);  //Slave Address
IIC_SERIAL(0x80); //Control Byte
IIC_SERIAL(a);
IIC_STOP();
}
//-------------------------------------------------------------------------------------
void init_oledSSD1327(void)
{
CS=0;
RES=1;
comm_out(0xae);//Set display off
comm_out(0xa0);//Set re-map
comm_out(0x43);
comm_out(0xa1);//Set display start line
comm_out(0x00);
comm_out(0xa2);//Set display offset
comm_out(0x00);
comm_out(0xa4);//Normal Display
comm_out(0xa8);//Set multiplex ratio
comm_out(0x7f);
comm_out(0xab);//Function Selection A
comm_out(0x01);//Enable internal VDD regulator
comm_out(0x81);//Set contrast
comm_out(0x53);
comm_out(0xb1);//Set Phase Length
comm_out(0x31);
comm_out(0xb3);//Set Front Clock Divider /Oscillator Frequency
comm_out(0xc1);
comm_out(0xb4); //For brightness enhancement
comm_out(0xb5);
comm_out(0xb6);//Set Second pre-charge Period
comm_out(0x0d);
comm_out(0xbc);//Set Pre-charge voltage
comm_out(0x04);
comm_out(0xbe);//Set VCOMH
comm_out(0x07);
comm_out(0xd5);//Function Selection B
comm_out(0x60);
clear( );// clear the whole DDRAM.
comm_out(0xaf);//Display on
}

//----------------計時器0 setting start----------------------------------------------------
void delay_Time(unsigned int i)
{
TR0=1;
TL0=(256-240);   //120us 
TH0=(256-240);   //24M Hz (quartz)
while(i!=0){
    while(TF0!=1);
    TF0=0;
    i--;
    }
TR0=0;
}
void delay_second(unsigned char i)
{
while(i!=0){
  delay_Time(10);
  i--;
  }
}


//  清除畫面
void clear(void)
{
int i,j;
comm_out(0x15);//Set column address
comm_out(0x00);//Column Start Address
comm_out(0x3f);//Column End Address
comm_out(0x75);//Set row address
comm_out(0x00);//Row Start Address
comm_out(0x7f);//Row End Address
for(i=0;i<128;i++)
{
for(j=0;j<64;j++)
{
data_out(0x00);
}
}

}
// 全點亮
void All_on(void)
{
int i,j;
comm_out(0x15);//Set column address
comm_out(0x00);//Column Start Address
comm_out(0x3f);//Column End Address
comm_out(0x75);//Set row address
comm_out(0x00);//Row Start Address
comm_out(0x7f);//Row End Address
for(i=0;i<128;i++)
{
for(j=0;j<64;j++)
{
data_out(0xff);
}
}

}








void main(void)
{

RES=0;
delay(100);
RES=1;
init_oledSSD1327();
while(1)
{

All_on();
}
}