#includereg51.h#defineucharunsignedchar#defineuintunsignedintsbitLCD_E=P3^5;//定义接口sbitLCD_RW=P3^6;sbitLCD_RS=P3^7;#defineLCD_DataP1//数据接口ucharup[]={IvU,hupeixian};ucharcodedown[]={_byfu}/*************************延时函数*****************************/voidDelay_ms(uinttime){uinti,j;for(i=0;itime;i++)for(j=0;j930;j++);}/**********************检测状态************************************/ucharLCD_ReadStatus(void){LCD_Data=0xFF;LCD_RS=0;LCD_RW=1;LCD_E=1;LCD_E=1;LCD_E=0;while(LCD_Data&0x80);//检测忙信号return(LCD_Data);}/************************写数据***********************************/voidLCD_WriteData(ucharWDLCD){LCD_ReadStatus();//检测忙LCD_Data=WDLCDLCD_RS=1;LCD_RW=0;LCD_E=1;//若晶振速度太高可以在这后加小的延时LCD_E=1;//延时LCD_E=0;}/************************写指令********************************/voidLCD_WriteCommand(ucharWCLCD,BuysC)//BuysC为0时忽略忙检测{if(BuysC)LCD_ReadStatus();//根据需要检测忙LCD_Data=WCLCD;LCD_RS=0;LCD_RW=0;LCD_E=1;LCD_E=1;LCD_E=0;}/*****************初始化***********************************/voidLCD_Init(void){LCD_Data=0;LCD_WriteCommand(0x38,0);//三次显示模式设置,不检测忙信号Delay_ms(5);LCD_WriteCommand(0x38,0);Delay5Ms();LCD_WriteCommand(0x38,0);Delay_ms(5);LCD_WriteCommand(0x38,1);//显示模式设置,开始要求每次检测忙信号LCD_WriteCommand(0x08,1);//关闭显示LCD_WriteCommand(0x01,1);//显示清屏LCD_WriteCommand(0x06,1);//显示光标移动设置LCD_WriteCommand(0x0C,1);//显示开及光标设置}/***********************显示一个字符********************************/voidDisplayOneChar(ucharX,ucharY,ucharDData){Y&=0x1;X&=0xF;//限制X不能大于15,Y不能大于1if(Y)X|=0x40;//当要显示第二行时地址码+0x40;X|=0x80;//算出指令码LCD_WriteCommand(X,0);//这里不检测忙信号,发送地址码LCD_WriteData(DData);}/************************显示一串字符*******************************/voidDisplayListChar(ucharX,ucharY,ucharcode*DData){ucharListLength;ListLength=0;Y&=0x1;X&=0xF;//限制X不能大于15,Y不能大于1while(DData[ListLength]0x20)//若到达字串尾则退出{if(X=0xF)//X坐标应小于0xF{DisplayOneChar(X,Y,DData[ListLength]);//显示单个字符ListLength++;X++;}}}/******************************************************************/voidmain(void){Delay_ms(400);//启动等待,等LCD讲入工作状态LCD_Init();//LCD初始化Delay_ms(5);//延时片刻(可不要)DisplayListChar(0,1,up);DisplayListChar(6,0,down);LCD_Data=0xff;while(1);}