您现在的位置: 主页 > MCU > 单片机应用 > LCD12864 C语言驱动 -
本文所属标签:
为本文创立个标签吧:

LCD12864 C语言驱动 -

来源: 网络用户发布,如有版权联系网管删除 2018-09-08 

[导读]#includesbit 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 0x

#include

本文引用地址: http://www.21ic.com/app/mcu/201808/785363.htm

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; 此字体下对应的点阵为:宽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;

}

*/

特别要注意的是读忙子程序,如果这个没有处理好,屏幕就是一团漆黑。

曾经在这个上面浪费了许多时间,Keil c51编译器支持在c代码中使用累加器ACC,

这点非常好。因为我之前写过12864的汇编驱动,仿真和实际硬件上都能正常运行,

但是换成c语言后想一一对照来写,却被卡在了ACC使用上。

READBUSY: CLR RS

SETB RW

BUSY_0: MOV DATABUS,#0FFH ;P0口作为通用输入口读取数据时,应先写1.

SETB EN

MOV A,DATABUS

CLR EN ;而在连续读取数据时,可以不用写1.

JB ACC.7,BUSY_0

RET

因为MOV A,DATABUS这句不好直接翻译成c,而现在用了ACC之后写的c代码反汇编后和汇编代码

一样,而且驱动正常了。




              查看评论 回复



嵌入式交流网主页 > MCU > 单片机应用 > LCD12864 C语言驱动 -
 

网站地图

围观()