#include
sbit RS=P2^0;
sbit RW=P2^1;
sbit EN=P2^2;
sbit CS1=P2^3;
sbit CS2=P2^4;
sbit RST=P2^5;
#define databus P0
#define uchar unsigned char
#define startline 0xc0
#define displayon 0x3f
#define startcolumn 0x40
#define page 0xb8
void init();
void clearscreen(uchar);
void readbusy();
void writecmd(uchar);
void writedata(uchar);
//uchar readdata();
void main()
{
uchar i,j;
/*-- 文字: 孟 --*/
/*-- 宋體12; 此字體下對應(yīng)的點陣為:寬x高=16x16 --*/
uchar code str[][16]={
0x00,0x10,0x10,0x10,0x11,0x11,0x91,0x11,0xF9,0x15,0x13,0x11,0x10,0x10,0x10,0x00,
0x40,0x40,0x40,0x7E,0x42,0x42,0x7E,0x43,0x42,0x7E,0x42,0x42,0x7E,0x40,0x40,0x00};
init();
CS1=0;
CS2=1;
for(i=0;i<2;i++)
{
writecmd(i|page);
writecmd(startcolumn);
for(j=0;j<16;j++)
{
writedata(str[i][j]);
}
}
while(1);
}
void init()
{
RST=0;
RST=1;
CS1=0;
CS2=0;
writecmd(displayon);
writecmd(startline);
clearscreen(0);
}
void clearscreen(uchar select)
{
uchar i,j;
switch(select)
{
case 0:CS1=0;CS2=0;break;
case 1:CS1=0;CS2=1;break;
case 2:CS1=1;CS2=0;break;
default:CS1=1;CS2=1;
}
for(i=0;i<8;i++)
{
writecmd(i|page);
writecmd(startcolumn);
for(j=0;j<64;j++)
{
writedata(0);
}
}
}
void readbusy()
{
RS=0;
RW=1;
do{
databus=0xff;
EN=1;
ACC=databus;
EN=0;
}while(ACC&0X80);
}
void writecmd(uchar cmd)
{
readbusy();
RS=0;
RW=0;
databus=cmd;
EN=1;
EN=0;
}
void writedata(uchar dat)
{
readbusy();
RS=1;
RW=0;
databus=dat;
EN=1;
EN=0;
}
/*
uchar readdata()
{
uchar temp;
readbusy();
RS=1;
RW=1;
databus=0xff;
EN=1;
temp=databus;
EN=0;
readbusy();
RS=1;
RW=1;
databus=0xff;
EN=1;
temp=databus;
EN=0;
return temp;
}
*/
特別要注意的是讀忙子程序,如果這個沒有處理好,屏幕就是一團漆黑。
曾經(jīng)在這個上面浪費了許多時間,Keil c51編譯器支持在c代碼中使用累加器ACC,
這點非常好。因為我之前寫過12864的匯編驅(qū)動,仿真和實際硬件上都能正常運行,
但是換成c語言后想一一對照來寫,卻被卡在了ACC使用上。
READBUSY: CLR RS
SETB RW
BUSY_0: MOV DATABUS,#0FFH ;P0口作為通用輸入口讀取數(shù)據(jù)時,應(yīng)先寫1.
SETB EN
MOV A,DATABUS
CLR EN ;而在連續(xù)讀取數(shù)據(jù)時,可以不用寫1.
JB ACC.7,BUSY_0
RET
因為MOV A,DATABUS這句不好直接翻譯成c,而現(xiàn)在用了ACC之后寫的c代碼反匯編后和匯編代碼
一樣,而且驅(qū)動正常了。