// 12864COG(串口)
//***************************************************************************
//连线表: CPU=89C52 *
//CS=P3.0 SID=P3.1 SCK=P3.2 RS=P3.3 Fosc=12Mhz *
//***************************************************************************
#include <reg52.h>
#include <stdlib.h>
#include <intrins.h>
#include <stdio.h>
sbit CS =P3^0; //片选
sbit SID=P3^1; //数据
sbit SCK=P3^2; //Clock信号
sbit RS =P3^3; //数据指令选择
sbit Key=P3^4; //锁定按键
sbit RST=P3^5;
#define Uchar unsigned char
#define Uint unsigned int
Uchar code bmp1[];
Uchar code bmp2[];
Uchar code bmp3[];
Uchar code ASCIIchardot[];
Uchar code ComTable[]={3,2,1,0,7,6,5,4,};
void WriteCommand( Uchar CommandByte )
{
Uchar i;
CS=0;
RS=0; //Command
for(i=0;i<8;i++)
{ SCK=1;
SID=( (CommandByte>>(7-i)) &0x01);
SCK=0;
_nop_();
SCK=1;
}
}
void WriteData( Uchar DataByte )
{
Uchar i;
CS=0;
RS=1; //Data
for(i=0;i<8;i++)
{
SCK=1;
SID=( (DataByte>>(7-i)) &0x01);
SCK=0;
_nop_();
SCK=1;
}
}
void Delay(Uint MS)
{
Uchar us,usn;
while(MS!=0)
{ usn = 4;
while(usn!=0)
{
us=0xf0;
while (us!=0){us--;};
usn--;
}
MS--;
}
}
void LcmClear( void )
{
Uint i,j;
WriteCommand(0x40); //Set Display Start Line = com0
for(i=0;i<8;i++)
{
WriteCommand(0xB0|ComTable[i]); //Set Page Address as ComTable
WriteCommand(0x10); //Set Column Address = 0
WriteCommand(0x01); //Colum from 1 -> 129 auto add
for(j=0;j<128;j++)
{
WriteData( 0 );
}
}
}
void LcmInit( void )
{
WriteCommand(0xAE); //Display OFF
WriteCommand(0xA2); //1/64 Duty 1/9 Bias
WriteCommand(0xA0); //ADC select --> right
WriteCommand(0xC0); //com1 --> com64
//WriteCommand(0x24); //设置内部电阻比Rb/Ra
//WriteCommand(0x81); //Sets V0
WriteCommand(0x20);//不用内部电位器调节对比度(当不用内部电压编程功能时 寄存器低6位设置为100000)
WriteCommand(0x2F); //voltage follower ON regulator ON booster ON
WriteCommand(0xA6); //Normal Display (not reverse dispplay)
WriteCommand(0xA4); //Entire Display Disable
LcmClear();
WriteCommand(0xAF); //Display ON
WriteCommand(0x40); //Set Display Start Line = com0
WriteCommand(0xB0); //Set Page Address = 0
WriteCommand(0x10); //Set Column Address 4 higher bits = 0
WriteCommand(0x01); //Set Column Address 4 lower bits = 1 , from IC SEG1 -> SEG129
}
void LcmDisplayBmp( Uchar *puts )
{
Uchar i,j;
Uint X=0;
WriteCommand(0x40); //Set Display Start Line = com0
for(i=0;i<8;i++)
{
WriteCommand(0xB0|ComTable[i]); //Set Page Address
WriteCommand(0x10); //Set Column Address = 0
WriteCommand(0x01); //Colum from 1 -> 129 auto add
for(j=0;j<128;j++)
{
WriteData( puts[X] );
X++;
}
}
}
//显示ASICC字符的函数
void LcmPutChar(Uchar col,Uchar page,Uchar Order)
//这里我不用x y做参数,是更贴切一些,实际上就是列值(横坐标)0-127,页值(纵坐标)0-7
{
Uchar i;
Uint x;
x = (Order-0x20)*0x10; //ASICC字符从0x20开始,每个16 byte
WriteCommand(ComTable[page&0x07]|0xB0); //Set Page Address
WriteCommand( ((col+1)>>4) | 0x10); //Set Column Address High Byte
WriteCommand( (col+1)&0x0F ); //Low Byte Colum from 1 -> 129 auto add
for(i=0;i<8;i++)
{
WriteData( ASCIIchardot[x] );
x++;
}
page++; //下半字符page+1
WriteCommand(ComTable[page&0x07]|0xB0); //Set Page Address
WriteCommand( ((col+1)>>4) | 0x10); //Set Column Address High Byte
WriteCommand( (col+1)&0x0F ); //Low Byte Colum from 1 -> 129 auto add
for(i=0;i<8;i++)
{
WriteData( ASCIIchardot[x] );
x++;
}
page--; //写完一个字符page还原
}
//显示字符串的函数
/*
为了避免需要LcmPutChar返回page col值,
只能将判断col和paeg的程序放到PutStr中
调用函数的时候需要注意
*/
void LcmPutStr(Uchar col,Uchar page,Uchar *puts)
{
while(*puts != '\0') //判断字符串时候显示完毕
{
if(col>120) //判断行末空间是否足够放一个字符,自动换行
{
page=page+2;
col=0;
}
if(page>6) //到了屏幕最下角,自动返回左上角
{
page=0;
col=0;
}
LcmPutChar(col,page,*puts);
puts++;
Delay(10); //延时演示效果
col=col+8; //下一个字符8列之后
}
}
void LcmDisplayDots( void )
{
Uchar i,j,X=0x55;
WriteCommand(0x40); //Set Display Start Line = com0
for(i=0;i<8;i++)
{
WriteCommand(0xB0|ComTable[i]); //Set Page Address
WriteCommand(0x10); //Set Column Address = 0
WriteCommand(0x01); //Colum from 1 -> 129 auto add
for(j=0;j<128;j++)
{
WriteData( X );
X=~X;
}
}
}
void LcmDisplayDotsUp( void )
{
Uchar i,j,X=0x55;
WriteCommand(0x40); //Set Display Start Line = com0
for(i=0;i<4;i++)
{
WriteCommand(0xB0|ComTable[i]); //Set Page Address
WriteCommand(0x10); //Set Column Address = 0
WriteCommand(0x01); //Colum from 1 -> 129 auto add
for(j=0;j<128;j++)
{
WriteData( X );
X=~X;
}
}
for(i=4;i<8;i++)
{
WriteCommand(0xB0|ComTable[i]); //Set Page Address
WriteCommand(0x10); //Set Column Address = 0
WriteCommand(0x01); //Colum from 1 -> 129 auto add
for(j=0;j<128;j++)
{
WriteData( 0xff );
}
}
}
void LcmDisplayDotsDown( void )
{
Uchar i,j,X=0x55;
WriteCommand(0x40); //Set Display Start Line = com0
for(i=0;i<4;i++)
{
WriteCommand(0xB0|ComTable[i]); //Set Page Address
WriteCommand(0x10); //Set Column Address = 0
WriteCommand(0x01); //Colum from 1 -> 129 auto add
for(j=0;j<128;j++)
{
WriteData( 0xff );
}
}
for(i=4;i<8;i++)
{
WriteCommand(0xB0|ComTable[i]); //Set Page Address
WriteCommand(0x10); //Set Column Address = 0
WriteCommand(0x01); //Colum from 1 -> 129 auto add
for(j=0;j<128;j++)
{
WriteData( X );
X=~X;
}
}
}
void LcmDisplayBlack( )
{
Uchar i,j;
WriteCommand(0x40); //Set Display Start Line = com0
for(i=0;i<8;i++)
{
WriteCommand(0xB0|ComTable[i]); //Set Page Address
WriteCommand(0x10); //Set Column Address = 0
WriteCommand(0x01); //Colum from 1 -> 129 auto add
for(j=0;j<128;j++)
WriteData( 0xff );
}
}
void DelayKey(unsigned char HalfSecond)
{ //Delay_key加入时间参数
unsigned int i;
for(i=0;i<26*HalfSecond;i++)
{
if(Key==0)
{
Delay(20);
while(Key==0) {Delay(20);}
break;
}
else Delay(10);
}
}
void main( void )
{
Delay(50);
RST=0;
Delay(250);
RST=1;
Delay(10);
LcmInit();
while(1)
{
LcmDisplayBmp(bmp1);
DelayKey(2);
//LcmDisplayBmp(bmp2);
//DelayKey(2);
//LcmDisplayBmp(bmp3);
//DelayKey(2);
LcmClear();
LcmPutStr(0,0,"abcdefghijklmnopqrstuvwxyz123456ABCDEFGHIJKLMNOPQRSTUVWXYZ123456");
DelayKey(2);
LcmDisplayBlack();
DelayKey(2);
}
}
/* ASICC字库代码 8x16点阵 */
unsigned char code ASCIIchardot[16*96] = {
/* */
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/*! */
0x00,0x00,0x00,0x1F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xCC,0x0C,0x00,0x00,0x00,
/*" */
0x00,0x08,0x30,0x60,0x08,0x30,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/*# */
0x02,0x03,0x1E,0x02,0x03,0x1E,0x02,0x00,0x20,0xFC,0x20,0x20,0xFC,0x20,0x20,0x00,
/*$ */
0x00,0x0E,0x11,0x3F,0x10,0x0C,0x00,0x00,0x00,0x18,0x04,0xFF,0x84,0x78,0x00,0x00,
/*% */
0x0F,0x10,0x0F,0x00,0x07,0x18,0x00,0x00,0x00,0x84,0x38,0xC0,0x78,0x84,0x78,0x00,
/*& */
0x00,0x0F,0x10,0x11,0x0E,0x00,0x00,0x00,0x78,0x84,0xC4,0x24,0x98,0xE4,0x84,0x08,
/*' */
0x08,0x68,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/*( */
0x00,0x00,0x00,0x07,0x18,0x20,0x40,0x00,0x00,0x00,0x00,0xE0,0x18,0x04,0x02,0x00,
/*) */
0x00,0x40,0x20,0x18,0x07,0x00,0x00,0x00,0x00,0x02,0x04,0x18,0xE0,0x00,0x00,0x00,
/** */
0x02,0x02,0x01,0x0F,0x01,0x02,0x02,0x00,0x40,0x40,0x80,0xF0,0x80,0x40,0x40,0x00,
/*+ */
0x00,0x00,0x00,0x0F,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0xF8,0x80,0x80,0x80,0x00,
/