清风斜影:
/*SMC1602A(16*2)模拟口线接线方式连接线图: --------------------------------------------------- |LCM-----51 |LCM-----51 |LCM------51 | --------------------------------------------------| |DB0-----P0.0|DB4-----P0.4|RW-------P2.1 | |DB1-----P0.1|DB5-----P0.5|RS-------P2.0 | |DB2-----P0.2|DB6-----P0.6|E--------P2.2 | |DB3-----P0.3|DB7-----P0.7|VLCD接1K电阻到GND| ---------------------------------------------------=============================================================*/#includesbitLCM_RW=P2^1;//定义引脚sbitLCM_RS=P2^0;sbitLCM_E=P2^2;#defineLCM_DataP0#defineBusy 0x80//用于检测LCM状态字中的Busy标识voidWriteDataLCM(unsignedcharWDLCM);voidWriteCommandLCM(unsignedcharWCLCM,BuysC);unsignedcharReadDataLCM(void);unsignedcharReadStatusLCM(void);voidLCMInit(void);voidDisplayOneChar(unsignedcharX,unsignedcharY,unsignedcharDData);voidDisplayListChar(unsignedcharX,unsignedcharY,unsignedcharcode*DData);voidDelay5Ms(void);voidDelay400Ms(void);//unsignedcharcodeuctech[]={"uctech"};//unsignedcharcodenet[]={"uctech.icpcn.com"};voidmain(void){Delay400Ms();//启动等待,等LCM讲入工作状态LCMInit();//LCM初始化Delay5Ms();//延时片刻(可不要)//DisplayListChar(0,0,"9.63_8.24");DisplayListChar(0,1,"123.456"); DisplayOneChar(7,0,0x44); DisplayOneChar(8,0,0x46);//DisplayOneChar(8,0,0x20); DisplayOneChar(9,0,0x43);//ReadDataLCM();//测试用句无意义while(1);}/*----------------------------------------------------------------*//*写数据 *//* 输入:RS=1,RW=0,E=1,D0~D7=数据,E=高脉冲 输出:无 *//*----------------------------------------------------------------*/voidWriteDataLCM(unsignedcharWDLCM){ReadStatusLCM();//检测忙LCM_Data=WDLCM;LCM_RS=1;LCM_RW=0;LCM_E=0;//若晶振速度太高可以在这后加小的延时LCM_E=0;//延时LCM_E=1;}/*----------------------------------------------------------------*//*写指令 *//* 输入:RS=0,RW=0,E=1,D0~D7=指令码,E=高脉冲 输出:无 *//*----------------------------------------------------------------*/voidWriteCommandLCM(unsignedcharWCLCM,BuysC)//BuysC为0时忽略忙检测{if(BuysC)ReadStatusLCM();//根据需要检测忙LCM_Data=WCLCM;LCM_RS=0;LCM_RW=0;LCM_E=0;LCM_E=0;LCM_E=1;}/*----------------------------------------------------------------*//*读数据 *//* 输入:RS=1,RW=1,E=1 输出:D0~D7=数据 *//*----------------------------------------------------------------*/unsignedcharReadDataLCM(void){LCM_RS=1;LCM_RW=1;LCM_E=0;LCM_E=0;LCM_E=1;return(LCM_Data);}/*==========================*//*读状态:输入:RS=0,RW=1,E=1 输出:D0~D7状态字 *//* 状态字说明 *//* D0~D6当前数据地址指针的数值 *//* D7 读写操作使能 1:禁止 0:允许 *//*==========================*/unsignedcharReadStatusLCM(void){LCM_Data=0xFF;LCM_RS=0;LCM_RW=1;LCM_E=0;LCM_E=0;LCM_E=1;while(LCM_Data&Busy);//检测忙信号return(LCM_Data);}voidLCMInit(void)//LCM初始化{LCM_Data=0;WriteCommandLCM(0x38,0);//三次显示模式设置,不检测忙信号Delay5Ms();WriteCommandLCM(0x38,0);Delay5Ms();WriteCommandLCM(0x38,0);Delay5Ms();WriteCommandLCM(0x38,1);//显示模式设置,开始要求每次检测忙信号WriteCommandLCM(0x08,1);//关闭显示WriteCommandLCM(0x01,1);//显示清屏WriteCommandLCM(0x06,1);//显示光标移动设置WriteCommandLCM(0x0C,1);//显示开及光标设置}//按指定位置显示一个字符voidDisplayOneChar(unsignedcharX,unsignedcharY,unsignedcharDData){Y&=0x1;X&=0xF;//限制X不能大于15,Y不能大于1if(Y)X|=0x40;//当要显示第二行时地址码+0x40;X|=0x80;//算出指令码WriteCommandLCM(X,0);//这里不检测忙信号,发送地址码WriteDataLCM(DData);}//按指定位置显示一串字符voidDisplayListChar(unsignedcharX,unsignedcharY,unsignedcharcode*DData){unsignedcharListLength; ListLength=0;Y&=0x1;X&=0xF;//限制X不能大于15,Y不能大于1while(DData[ListLength]>0x20)//若到达字串尾则退出 { if(X