C51單片機(jī),128*64串口LCD驅(qū)動
CA12864K 串口C51演示程序
// CA12864K 測試程序(串口)
//***************************************************************************
//連線表: CPU=89C52 SystemClock=12Mhz *
//CS=P3.0 SCLK=P3.1 SID=P3.2 Reset=RC in Board *
//***************************************************************************
#include
#include
#include
#include
sbit CS =P3^0;
sbit SCK=P3^1;
sbit SID=P3^2;
sbit Key=P3^4;
unsigned char code AC_TABLE[]={
0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87, //第一行漢字位置
0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97, //第二行漢字位置
0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f, //第三行漢字位置
0x98,0x99,0x9a,0x9b,0x9c,0x9d,0x9e,0x9f, //第四行漢字位置
};
unsigned char code str2[]="歡迎光臨sunson!!";
unsigned char code str1[]="黃河遠(yuǎn)上白云間,一片孤城萬仞山。羌笛何須怨楊柳,春風(fēng)不度玉門關(guān)。";
unsigned char code bmp1[];
//串口發(fā)送一個字節(jié)
void SendByte(unsigned char Dbyte)
{
unsigned char i;
for(i=0;i<8;i++)
{
SCK = 0;
Dbyte=Dbyte<<1; //左移一位
SID = CY; //移出的位給SID
SCK = 1;
SCK = 0;
}
}
//串口接收一個字節(jié)
//僅在讀取數(shù)據(jù)的時候用到
//而讀出的數(shù)據(jù)是一次只能讀出4bit的
unsigned char ReceiveByte(void)
{
unsigned char i,temp1,temp2;
temp1=temp2=0;
for(i=0;i<8;i++)
{
temp1=temp1<<1;
SCK = 0;
SCK = 1;
SCK = 0;
if(SID) temp1++;
}
for(i=0;i<8;i++)
{
temp2=temp2<<1;
SCK = 0;
SCK = 1;
SCK = 0;
if(SID) temp2++;
}
return ((0xf0&temp1)+(0x0f&temp2));
}
void CheckBusy( void )
{
do SendByte(0xfc); //11111,RW(1),RS(0),0
while(0x80&ReceiveByte()); //BF(.7)=1 Busy
}
void WriteCommand( unsigned char Cbyte )
{
CS = 1;
CheckBusy();
SendByte(0xf8); //11111,RW(0),RS(0),0
SendByte(0xf0&Cbyte); //高四位
SendByte(0xf0&Cbyte<<4);//低四位(先執(zhí)行<<)
CS = 0;
}
void WriteData( unsigned char Dbyte )
{
CS = 1;
CheckBusy();
SendByte(0xfa); //11111,RW(0),RS(1),0
SendByte(0xf0&Dbyte); //高四位
SendByte(0xf0&Dbyte<<4);//低四位(先執(zhí)行<<)
CS = 0;
}
unsigned char ReadData( void )
{
CheckBusy();
SendByte(0xfe); //11111,RW(1),RS(1),0
return ReceiveByte();
}
void Delay(unsigned int MS)
{
unsigned char us,usn;
while(MS!=0) //for 12M
{ usn = 2;
while(usn!=0)
{
us=0xf5;
while (us!=0){us--;};
usn--;
}
MS--;
}
}
//松山電子測試架專用延時函數(shù)
void DelayKey(unsigned int Second , unsigned int MS100)
{ //輸入精確到0.1S,是用","
unsigned int i;
for(i=0;i { if(Key==0) { Delay(20); while(Key==0) {Delay(20);} break; } else Delay(10); } } void LcmInit( void ) { WriteCommand(0x30); //8BitMCU,基本指令集合 WriteCommand(0x03); //AC歸0,不改變DDRAM內(nèi)容 WriteCommand(0x0C); //顯示ON,游標(biāo)OFF,游標(biāo)位反白OFF WriteCommand(0x01); //清屏,AC歸0 WriteCommand(0x06); //寫入時,游標(biāo)右移動 } //文本區(qū)清RAM函數(shù) void LcmClearTXT( void ) { unsigned char i; WriteCommand(0x30); //8BitMCU,基本指令集合 WriteCommand(0x80); //AC歸起始位 for(i=0;i<64;i++) WriteData(0x20); } //圖形區(qū)和文本區(qū)顯示在兩個不同的RAM區(qū) //圖形區(qū)清RAM函數(shù) void LcmClearBMP( void ) { unsigned char i,j; WriteCommand(0x34); //8Bit擴(kuò)充指令集,即使是36H也要寫兩次 WriteCommand(0x36); //繪圖ON,基本指令集里面36H不能開繪圖 for(i=0;i<32;i++) //12864實際為256x32 { WriteCommand(0x80|i); //行位置 WriteCommand(0x80); //列位置 for(j=0;j<32;j++) //256/8=32 byte WriteData(0); } } void PutStr(unsigned char row,unsigned char col,unsigned char *puts) { WriteCommand(0x30); //8BitMCU,基本指令集合 WriteCommand(AC_TABLE[8*row+col]); //起始位置 while(*puts != '/0') //判斷字符串是否顯示完畢 { if(col==8) //判斷換行 { //若不判斷,則自動從第一行到第三行 col=0; row++; } if(row==4) row=0; //一屏顯示完,回到屏左上角 WriteCommand(AC_TABLE[8*row+col]); WriteData(*puts); //一個漢字要寫兩次 puts++; WriteData(*puts); puts++; col++; } } void PutBMP(unsigned char *puts) { unsigned int x=0; unsigned char i,j; WriteCommand(0x34); //8Bit擴(kuò)充指令集,即使是36H也要寫兩次 WriteCommand(0x36); //繪圖ON,基本指令集里面36H不能開繪圖 for(i=0;i<32;i++) //12864實際為256x32 { WriteCommand(0x80|i); //行位置 WriteCommand(0x80); //列位置 for(j=0;j<32;j++) //256/8=32 byte { //列位置每行自動增加 WriteData(puts[x]); x++; } } } //松山電子測試用點陣顯示 void DisplayDots(unsigned char DotByte) { unsigned char i,j; WriteCommand(0x34);