LCD12864 C語(yǔ)言驅(qū)動(dòng)
掃描二維碼
隨時(shí)隨地手機(jī)看文章
#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; 此字體下對(duì)應(yīng)的點(diǎn)陣為:寬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;
}
*/
特別要注意的是讀忙子程序,如果這個(gè)沒(méi)有處理好,屏幕就是一團(tuán)漆黑。
曾經(jīng)在這個(gè)上面浪費(fèi)了許多時(shí)間,Keil c51編譯器支持在c代碼中使用累加器ACC,
這點(diǎn)非常好。因?yàn)槲抑皩?xiě)過(guò)12864的匯編驅(qū)動(dòng),仿真和實(shí)際硬件上都能正常運(yùn)行,
但是換成c語(yǔ)言后想一一對(duì)照來(lái)寫(xiě),卻被卡在了ACC使用上。
READBUSY: CLR RS
SETB RW
BUSY_0: MOV DATABUS,#0FFH ;P0口作為通用輸入口讀取數(shù)據(jù)時(shí),應(yīng)先寫(xiě)1.
SETB EN
MOV A,DATABUS
CLR EN ;而在連續(xù)讀取數(shù)據(jù)時(shí),可以不用寫(xiě)1.
JB ACC.7,BUSY_0
RET
因?yàn)镸OV A,DATABUS這句不好直接翻譯成c,而現(xiàn)在用了ACC之后寫(xiě)的c代碼反匯編后和匯編代碼
一樣,而且驅(qū)動(dòng)正常了。