- #include<reg51.h>
- #include<intrins.h>
- #include<stdio.h>
- #include<string.h>
- #include<math.h>
- #include<absacc.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define DataPort P0 //数据端口
- #define Busy 0x80
- sbit RS =P1^0; //控制引脚定义
- sbit RW =P1^1;
- sbit Elcm =P1^2;
- code char exampl[]="Hello Every Body";
- unsigned char tem1,t;
- unsigned char c1=10;
- //*********************************一毫秒延時**********************
- void Delay1Ms(void)
- {
- uint i=552;
- while(i--);
- }
- //*********************************五毫秒延時**********************
- void Delay5Ms(void)
- {
- uint i=5552;
- while(i--);
- }
- //*********************************等待允许函数**********************
- void WaitForEnable(void)
- {
- DataPort=0xff;
- RS=0;RW=1;_nop_();
- Delay1Ms();
- Elcm=1;_nop_();_nop_();
- Delay1Ms();
- while(DataPort&Busy);
- Elcm=0;
- }
- //*********************************写控制字符函数**********************
- void LcdWriteCommand(uchar CMD,uchar AttribC)
- {
- if(AttribC)WaitForEnable(); //检测忙信号
- RS=0;RW=0;_nop_();
- DataPort=CMD;_nop_(); //送控制字子程序
- Elcm=1;_nop_();_nop_();Elcm=0;//操作允许脉冲信号
- }
- //*********************************当前位置写字符函数**********************
- void LcdWriteData(char dataW)
- {
- WaitForEnable(); //检测忙信号
- RS=1;RW=0;_nop_();
- DataPort=dataW;_nop_();
- Elcm =1;_nop_();_nop_;Elcm =0; //操作允许脉冲信号
- }
- //*********************************显示光标定位函数**********************
- void LocateXY(char posx,char posy)
- {
- uchar temp;
- temp=posx&0xf;
- posy&=0x1;
- if(posy)temp|=0x40;
- temp|=0x80;
- LcdWriteCommand(temp,0);
- }
- //*********************************单字符显示函数**********************
- void DispOneChar(uchar x,uchar y,uchar Wdata)
- {
- LocateXY(x,y); //定位显示字符的x,y位置
- LcdWriteData(Wdata); //写字符
- }
- //*********************************显示字符串函数**********************
- void ePutstr(uchar x,uchar y,uchar j,uchar code*ptr)
- { uchar i;
- for(i=0;i<j;i++)
- {
- DispOneChar(x++,y,ptr[i]);
- if(x==16)
- {
- x=0;y^=1;
- }
- }
- }
- //*********************************LCD初始化函数**********************
- void LcdReset(void)
- {
- LcdWriteCommand(0x38,0); //检测模式设置(不检测忙信号)
- Delay5Ms();
- LcdWriteCommand(0x38,0); //共三次
- Delay5Ms();
- LcdWriteCommand(0x38,0);
- Delay5Ms();
- LcdWriteCommand(0x38,1); //显示模式设置(以后均显示忙信号)
- LcdWriteCommand(0x38,1); //显示关闭
- LcdWriteCommand(0x38,1); // 显示清屏
- LcdWriteCommand(0x38,1); // 显示光标移动设置
- LcdWriteCommand(0x38,1); // 显示开及光标设置
- }
- //*********************************四百毫秒延时函数**********************
- void Delay400Ms(void)
- {
- uchar i=5;
- uint j;
- while(i--)
- {
- j=7269;
- while(j--);
- }
- }
- //*********************************主函数**********************
- void main(void)
- {
- LcdReset();
- Delay400Ms();
- ePutstr(0,0,16,exampl); //第一行从零位开始显示Hello Every Body
- while(1);
- }
复制代码 |