1、液晶160128A C语言/*/连线表: CPU=W78E54B CPUClock=12Mhz */ LCM - CPU */ WR - WR */ RD - RD */ CS - P2.7 */ A0(RS)- P2.0 */ DB07 - P0.07 */ RESET - P1.0 */ 注:本程序BUS总线模式程序和IO模式程序的硬件连线方式一致 */ 但IO模式的时候需要在P0口接10K上拉电阻 */*#include #include #include /自己些的一个头文件,主要是Uchar和二进制#define Graphic 1#define TXT 0#define LcmXP
2、ixel 240 /横向宽度#define LcmYPixel 128 /纵向高度#define Uchar unsigned char#define Uint unsigned int/#define BUS_MODE /总线模式驱动液晶#define IO_MODE /IO口模式驱动液晶#ifdef BUS_MODEchar xdata LcmCmdPort _at_ 0x0100; /CS低有效 RS高-命令char xdata LcmDatPort _at_ 0x0000; /CS低有效 RS低-数据#endif#ifdef IO_MODEsbit CSP = P27;sbit RSP
3、 = P20;sbit WRP = P36;sbit RDP = P37;#define LcmDataBus P0#endifsbit RES = P10; /sbit Key = P32; /sbit p0_dr=P34; /坚鸿测试架增加指令,控制P0口输入输出sbit FS = P35; /字体选择,一般在硬件接口直接接电源或地sbit P33=P33;Uchar code str0;Uchar code bmp1;void int_zhongduan();void delay(unsigned int t);/*/液晶驱动底层程序开始/*/指令&数据读写状态void CheckRWC
4、D(void)#ifdef BUS_MODE while(LcmCmdPort & 0x03) != 0x03); /低电平忙#endif#ifdef IO_MODE p0_dr=1;/坚鸿测试架增加指令,控制P0口输入输出 CSP = 0; WRP = 1; RSP = 1; LcmDataBus = 0xff; RDP = 0; while(LcmDataBus & 0x03) != 0x03); RDP = 1; CSP = 1; p0_dr=0;/坚鸿测试架增加指令,控制P0口输入输出#endif/数据自动写状态void CheckAutoWrite(void)#ifdef BUS_M
5、ODE while(LcmCmdPort & 0x08) = 0); /低电平忙#endif#ifdef IO_MODE p0_dr=1;/坚鸿测试架增加指令,控制P0口输入输出 CSP = 0; WRP = 1; RSP = 1; LcmDataBus = 0xff; RDP = 0; while(LcmDataBus & 0x08) =0 ); RDP = 1; CSP = 1; p0_dr=0;/坚鸿测试架增加指令,控制P0口输入输出#endif/屏读/屏考贝出错状态void CheckScreen(void) #ifdef BUS_MODE while(LcmCmdPort & 0x4
6、0); /1出错,0正确#endif#ifdef IO_MODE p0_dr=1;/坚鸿测试架增加指令,控制P0口输入输出 CSP = 0; WRP = 1; RSP = 1; LcmDataBus = 0xff; RDP = 0; while(LcmDataBus & 0x40); RDP = 1; CSP = 1; p0_dr=0;/坚鸿测试架增加指令,控制P0口输入输出#endif/一个数据子写入函数void LcmWriteData(Uchar uData) #ifdef BUS_MODE LcmDatPort = uData;#endif #ifdef IO_MODE CSP = 0
7、; RDP = 1; RSP = 0; LcmDataBus = uData; WRP = 0; WRP = 1; CSP = 1; LcmDataBus = 0xff;#endif/一个数据读出函数Uchar LcmReadData( void ) Uchar uData;#ifdef BUS_MODE uData = LcmDatPort;#endif#ifdef IO_MODE p0_dr=1;/坚鸿测试架增加指令,控制P0口输入输出 CSP = 0; WRP = 1; RSP = 0; LcmDataBus = 0xff; RDP = 0; uData = LcmDataBus; RD
8、P = 1; CSP = 1; p0_dr=0;/坚鸿测试架增加指令,控制P0口输入输出#endif return uData;/无参数指令写入函数void LcmWriteCommand(Uchar Command) CheckRWCD(); /检测指令读写状态#ifdef BUS_MODE LcmCmdPort = Command;#endif#ifdef IO_MODE CSP = 0; RDP = 1; RSP = 1; LcmDataBus = Command; WRP = 0; WRP = 1; CSP = 1; LcmDataBus = 0xff;#endif/单参数指令写入函数
9、void LcmWriteCommandWith1Par(Uchar Parameter,Uchar Command) LcmWriteData(Parameter); LcmWriteCommand(Command);/双参数指令写入函数void LcmWriteCommandWith2Par(Uchar Parameter1,Uchar Parameter2,Uchar Command) LcmWriteData(Parameter1); LcmWriteData(Parameter2); LcmWriteCommand(Command);/LCM初始化函数void LcmInit( vo
10、id ) LcmWriteCommandWith2Par(0x00,0x00,0x40); /设置文本显示区首地址 LcmWriteCommandWith2Par(LcmXPixel/8,0x00,0x41); /设置文本显示区宽度 LcmWriteCommandWith2Par(0x00,0x08,0x42); /设置图形显示区首地址0x0800 LcmWriteCommandWith2Par(LcmXPixel/8,0x00,0x43); /设置图形显示区宽度 LcmWriteCommand(0xA7); /设置光标形状 8x8方块 LcmWriteCommand(0x81); /显示方式
11、设置 文本xor图形(异或) LcmWriteCommand(0x9C); /显示开关设置 文本开,图形开,光标闪烁关/函数功能: 延时函数/入口参数: 需要延时的毫秒数(MS)/出口参数: 无void DelayMS(unsigned int MS) unsigned char us,usn; while(MS!=0) usn = 2; /for 12M while(usn!=0) us=0xf6; while (us!=0)us-; usn-; MS-; /函数功能: 特殊延时函数/入口参数: 需要延时的?.?秒(Second.MS100)/出口参数: 无void DelayKey(uns
12、igned int Second , unsigned int MS100) /输入精确到0.1S,是用, unsigned int i; for(i=0;i (LcmXPixel/8-1) /自动换行 x=0; y+; PutChar(x,y,*str); +x; +str; /显示一副图片函数void PutBmp(Uchar *puts) Uchar i,j; Uint X=0; LocateXY(0,0,Graphic); LcmWriteCommand(0xB0); /进入自动写 for(i=0;iLcmYPixel;i+) for(j=0;jLcmXPixel/8;j+) Chec
13、kAutoWrite(); LcmWriteData(putsX); X+; LcmWriteCommand(0xB2); /退出自动写/反显一副图片函数void ReverseOneBmp( void ) /利用数据一次读写操作 Uchar i,j,X; /只能对一段RAM取反 LocateXY(0,0,Graphic); for(i=0;iLcmYPixel;i+) for(j=0;jLcmXPixel/8;j+) LcmWriteCommand(0xC5); /读数据,地址不变 CheckRWCD(); X = LcmReadData(); X = X; LcmWriteCommandW
14、ith1Par(X,0xC0); /屏读功能演示void ScreenReadDemo( void ) /利用屏读 /能对当前屏幕反显,包括合成内容(本函数仅读文本到图形) /但是写入后的内容都在图形区 /可以将文本区内容读出以图形方式写到图形区 Uchar i,j,X; /使用屏读屏考时候必须设置宽度为40(硬件已决定) /所以如确定要用到屏读屏考的话最后初始化时就设40 LcmWriteCommandWith2Par(0x28,0x00,0x41); /设置文本显示区宽度=40 LcmWriteCommandWith2Par(0x28,0x00,0x43); /设置图形显示区宽度=40 P
15、utString(0,0,str0); /0,0不会受到宽度改变的影响 for(i=0;i8;i+) /屏读较慢,就以一行文本做演示 LcmWriteCommandWith2Par(0x28*i+0x800)&0xff,(0x28*i+0x800)/256,0x24); for(j=0;jLcmXPixel/8;j+) LcmWriteCommand(0xE0); CheckScreen(); CheckRWCD(); X = LcmReadData(); X = X; LcmWriteCommandWith1Par(X,0xC0); /屏读需要一点时间,运行速度比较慢,运行过程中由于文本图形
16、互补的关系,屏幕全黑 LcmWriteCommand(0x98); /关闭文本区看显示效果,会在图形显示区显示一样的字符串 DelayKey(1,0); LcmWriteCommand(0x9C); /恢复文本区显示 LcmWriteCommandWith2Par(LcmXPixel/8,0x00,0x41); /宽度改回原值 LcmWriteCommandWith2Par(LcmXPixel/8,0x00,0x43); LcmClear(0); /清屏/显示点阵子函数void PutDots(Uchar DotByte,Uchar n) Uchar i,j; LocateXY(0,0,Gra
17、phic); LcmWriteCommand(0xB0); /进入自动写 for(i=0;iLcmYPixel/n;i+) for(j=0;jLcmXPixel/8*n;j+) CheckAutoWrite(); LcmWriteData(DotByte); DotByte=DotByte; LcmWriteCommand(0xB2); /退出自动写/主函数void main(void) Uchar i; p0_dr=0;/坚鸿测试架增加指令,控制P0口输入输出 int_zhongduan(); FS = 0; RES = 0; DelayMS(100); RES = 1; DelayMS(2
18、0); LcmInit(); LcmClear(0); while(1) PutBmp(bmp1); /显示一副图片 DelayKey(1,0); ReverseOneBmp(); /反显当前图片 DelayKey(1,0); LcmClear(0); /清屏,此处不清屏且同时打开文本和图形可观察文本xor图形的显示效果 LcmWriteCommand(0x94); /仅开文本,关图形,这种情况下有可能英文字库出现错误 for(i=0;i(LcmYPixel/8);i+) /显示一屏字符串 PutString(0,i,str0); DelayKey(0,5); LcmWriteCommand(
19、0x9C); /开文本图形 DelayKey(0,5); LcmClear(0); LcmClear(0xff); /全黑检测 DelayKey(1,0); ScreenReadDemo(); /屏读演示,函数内包含了延时 PutDots(0x55,1); DelayKey(1,0); PutDots(0xaa,1); DelayKey(1,0); PutDots(0x33,2); DelayKey(1,0); PutDots(0xcc,2); DelayKey(1,0); PutDots(0xF0,4); DelayKey(1,0); PutDots(0x0F,4); DelayKey(1,
20、0); /代码区Uchar code str0=ShenZhen SunSon Electronic INC; /减去0x20即可Uchar code bmp1=/*/* 转换的文件:D:!Program!Bmp_Sample240128.bmp/* 源图形宽度 * 高度:240 * 128/* 调整后宽度 * 高度:240 * 128/* 字模格式:横向取模,冗余模式,字节正序,正色取模,/* 点阵转换时间:2009/07/25 10:40:22/* 位图点阵占用3840字节/*/0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0
21、xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0
22、x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x80,0x00,0x00,0x00,0x07,0xFF,0xFF,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x80,0x00,0x00,0x00,0x0F,0xFF,0xFF,0xC0,0x00,0x00,0x00,0
23、x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x80,0x00,0x00,0x00,0x0F,0xFF,0xFF,0xE0,0x00,0xF0,0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x80,0x00,0x00,0x00,0x0F,0xFF,0xFF,0xFC,0x01,0xFC,0x00,0
24、x00,0x03,0xF8,0x00,0x1E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0x00,0x00,0x00,0x1F,0x00,0x01,0x80,0x00,0x00,0x00,0x0F,0x9F,0xFF,0xFE,0x01,0x8F,0x0F,0xFE,0x0F,0x18,0x00,0x7F,0xE0,0x00,0x00,0x00,0x38,0x00,0x00,0x19,0x80,0x00,0x00,0x7F,0xE0,0x01,0x80,0x00,0x00,0x00,0x0F,0x1F,0xFC,0x7F,0x01,0x83,0x7F,0xFF,0xDC,0x18,0x01,0xFF,0xF0,0x00,0x00,0x00,0x7C,0x
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1