您的位置 首页 开关

1602LCD显示器的运用

LCDE==P27LCD使能DB==P0数据位RD==P26写模式选择WR==P25读写使能指令码:二进制十六进制清屏:100000000×80;光标归位:

LCDE==P2.7 LCD使能

DB==P0 数据位
RD==P2.6 写形式挑选
WR==P2.5 读写使能

指令码: 二进制 十六进制

清屏: 10000000 0x80;

光标归位: X1000000 0x40或0xC0

。。。。。。。。

读状况输入:RS=L,RW=H,E=H 输出:DB0~DB7=状况字
写指令输入:RS=L,RW=L,E=下降沿脉冲,DB0~DB7=指令码输出:无
读数据输入:RS=H,RW=H,E=H 输出:DB0~DB7=数据
写数据输入:RS=H,RW=L,E=下降沿脉冲,DB0~DB7=数据输出:无

前次写的驱动:

#include

#include”LCD1602.h”

#include

#include

#include

#include

#include

sbit lcdrs =P3^0;//数据/指令挑选端(H/L)

sbit lcdrw =P3^1;//读/写挑选端(H/L)

sbit lcden =P3^2;//使能信号端

char theDisplayLength=0,//保存应该在当时显现的位数

cursorPosition=0;//保存光标地点显现屏的方位

char startAddress=0,endAddress=0;//保存显现的开端和完毕地址

char LCDBuffer[16]={“”};

char ctrlStr[7]={“.4f”};

void delayms(int xms) //延时X毫秒

{

int i, j;

for(i=xms;i>0;i–)

for(j=121;j>0;j–);

}

void write_com(char com)//写指令

{

lcdrs=0;

P0=com;

delayms(1);

lcden=1;

delayms(1);

lcden=0;

}

void write_data(char mydata)////写数据

{

lcdrs=1;

P0=mydata;

delayms(1);

lcden=1;

delayms(1);

lcden=0;

}

void initLCD()//初始化

{

lcdrw=0;

lcden=0;

write_com(0x38); //设置16×2显现,5×7点阵,8位数据接口

write_com(0x0e); //00001dcb,d=1开显现,c=1显现光标,b=1光标闪耀

write_com(0x06); //000001ns,n=1字符后地址指针加一,且光标加

//s=1写字符,正屏显现左移(n=1)

write_com(0x80); //设置数据地址指针(0-27H,40-67H)

write_com(0x01); //01,显现清屏:1,数据指针清零。2,一切显现清零

//02,显现回车:1,数据指针清零。

startAddress=0;

endAddress=-1;

}

void write_position(char position)//字符要显现在LCD哪个方位

{

lcdrs=0;

lcdrw=0;

lcden=0;

P0=position;

delayms(1);

lcden=1;

delayms(1);

lcden=0;

}

///功用函数部分

void displayOneCharacter(char mydata,char position)

{

write_position(position);

write_data(mydata);

}

void loadAndDisplay(bit judge)

{

if(judge==0)

{

//显现上面的16个字符

displayOneCharacter(LCDBuffer[0],0x80);

displayOneCharacter(LCDBuffer[1],0x81);

displayOneCharacter(LCDBuffer[2],0x82);

displayOneCharacter(LCDBuffer[3],0x83);

displayOneCharacter(LCDBuffer[4],0x84);

displayOneCharacter(LCDBuffer[5],0x85);

displayOneCharacter(LCDBuffer[6],0x86);

displayOneCharacter(LCDBuffer[7],0x87);

displayOneCharacter(LCDBuffer[8],0x88);

displayOneCharacter(LCDBuffer[9],0x89);

displayOneCharacter(LCDBuffer[10],0x8A);

displayOneCharacter(LCDBuffer[11],0x8B);

displayOneCharacter(LCDBuffer[12],0x8C);

displayOneCharacter(LCDBuffer[13],0x8D);

displayOneCharacter(LCDBuffer[14],0x8E);

displayOneCharacter(LCDBuffer[15],0x8F);

}

if(judge==1)

{

//显现下面的16个字符

displayOneCharacter(LCDBuffer[0],0xC0);

displayOneCharacter(LCDBuffer[1],0xC1);

displayOneCharacter(LCDBuffer[2],0xC2);

displayOneCharacter(LCDBuffer[3],0xC3);

displayOneCharacter(LCDBuffer[4],0xC4);

displayOneCharacter(LCDBuffer[5],0xC5);

displayOneCharacter(LCDBuffer[6],0xC6);

displayOneCharacter(LCDBuffer[7],0xC7);

displayOneCharacter(LCDBuffer[8],0xC8);

displayOneCharacter(LCDBuffer[9],0xC9);

displayOneCharacter(LCDBuffer[10],0xCA);

displayOneCharacter(LCDBuffer[11],0xCB);

displayOneCharacter(LCDBuffer[12],0xCC);

displayOneCharacter(LCDBuffer[13],0xCD);

displayOneCharacter(LCDBuffer[14],0xCE);

displayOneCharacter(LCDBuffer[15],0xCF);

}

}

void setCursorPosition(char position) //移动光标方位

{

char i;

write_com(0x02);//光标归位

for(i=0;i

{

write_com(0x14);//右移

}

}

//下面的代码用于动态显现字符的,首要的东西在上面

void moveLeftOneStep(void)//左右移动

{

if(cursorPosition==0)//标明要经过翻页左移

{

if(startAddress!=0)//标明还可左移一次 剩余状况无法移动

{

startAddress–;//确认显现的完毕方位

if(expressLength-startAddress>=16)//标明显现满了

{

endAddress=startAddress+15;

return;

}

else//标明后边还有几个空要填满

{

endAddress=expressLength-1;

}

}

}

else//标明无需翻页

{

cursorPosition–;

}

}

void moveRightOneStep(void)

{

if(startAddress+cursorPosition==endAddress+1)

return;

if(cursorPosition==15)//标明要考虑是否进行翻页

{

if(startAddress+16

//标明 startAddress+15得到的是LCD最终一位对应express的地址 再加1标明长度

{

startAddress++;

endAddress=startAddress+15;

displayExpress();

}

else if(startAddress+16==expressLength)//标明当时已是最终一个字符

{

startAddress++;

endAddress=startAddress+14;//光标处应该是空

displayExpress();

}

else//标明光标处并没有显现字符

{

return;

}

}

else//标明无需翻页

{

if(cursorPosition==endAddress+1)//已到最终一个方位,光标不能再动了

{

return;

}

cursorPosition++;

endAddress=expressLength-1;

setCursorPosition(cursorPosition);

}

}

void moveLeft(void)//startAddress+cursorPosition是光标对应express中的地址

{

if(startAddress+cursorPosition==0)//跳转到最终

{

if(expressLength>15)

{

endAddress=expressLength-1;

startAddress=endAddress-14;

}

cursorPosition=endAddress-startAddress+2;

}

if(startAddress+cursorPosition-1>=0&&express[startAddress+cursorPosition-1]==n)//tan,sin,ln

{

if(startAddress+cursorPosition-6>=0&&express[startAddress+cursorPosition-6]==a&&express[startAddress+cursorPosition-6+1]==r) //arctan,arcsin……

{

moveLeftOneStep();

moveLeftOneStep();

moveLeftOneStep();

moveLeftOneStep();

moveLeftOneStep();

moveLeftOneStep();

}

else if(express[startAddress+cursorPosition-2]==l)//标明为ln

{

moveLeftOneStep();

moveLeftOneStep();

}

else //标明为sin或tan

{

moveLeftOneStep();

moveLeftOneStep();

moveLeftOneStep();

}

}

else if(startAddress+cursorPosition-1>=0&&startAddress+cursorPosition-1>=0&&express[startAddress+cursorPosition-1]==g)//lg

{

moveLeftOneStep();

moveLeftOneStep();

}

else if(startAddress+cursorPosition-1>=0&&express[startAddress+cursorPosition-1]==s)

{

if(startAddress+cursorPosition-6>=0&&express[startAddress+cursorPosition-6]==a&&express[startAddress+cursorPosition-6+1]==r) //arctan,arcsin……

{

moveLeftOneStep();

moveLeftOneStep();

moveLeftOneStep();

moveLeftOneStep();

moveLeftOneStep();

moveLeftOneStep();

}

else

{

moveLeftOneStep();

moveLeftOneStep();

moveLeftOneStep();

}

}

else//标明为单个字符

{

moveLeftOneStep();

}

}

void moveRight(void)

{

if(endAddress-startAddress+1==cursorPosition)//标明光标到了最终

{

if(expressLength>15)

{

endAddress=15;

startAddress=0;

}

cursorPosition=0;

}

else if(express[startAddress+cursorPosition]==s||express[startAddress+cursorPosition]==t||express[startAddress+cursorPosition]==c)//sin,tan

{

moveRightOneStep();

moveRightOneStep();

moveRightOneStep();

}

else if(express[startAddress+cursorPosition]==a)//arctan,arcsin之类的

{

moveRightOneStep();

moveRightOneStep();

moveRightOneStep();

moveRightOneStep();

moveRightOneStep();

moveRightOneStep();

}

else if(express[startAddress+cursorPosition]==l)//lg,ln

{

moveRightOneStep();

moveRightOneStep();

}

else//标明为单字符

moveRightOneStep();

}

void displayExpress(void)//从startAddress显现到endAddress

{

char i;

write_position(0x80);//从这儿开端显现

for(i=startAddress;i<=endAddress;i++)

{

write_data(express[i]);

}

for(i=endAddress+1;i<16+startAddress;i++)//后边的写空格

{

write_data( );

}

}

void insertOneAndDisplay(void)

{

expressLength++;

if(cursorPosition>=0&&cursorPosition<15)//标明是在和前面傍边刺进的 startAddress能够不变

{

//检查当时显现状况

if(endAddress-startAddress==15)//标明当时现已满显了

{

endAddress–;

}

else//标明未满显

{

endAddress++;

}

cursorPosition++;

}

else if(cursorPosition==15)//标明光标在显现器第16个显现单元处 此刻 startAddress得变

{

if(endAddress-startAddress==15)//标明当时现已满显未满显的状况已在isModifyInput中处理

{

startAddress++;

endAddress++;

}

}

}

void deleteOneAndDisplay(void)

{

moveExpressForward(cursorPosition+startAddress,1);

expressLength–;

if(cursorPosition==0)

{

startAddress–;

//确认endAddress的方位

if(expressLength-startAddress>=16)//标明显现满了

{

endAddress=startAddress+15;

}

else//标明后边还有几个空要填满

{

endAddress=expressLength-1;

}

}

else

{

cursorPosition–;//光标方位要–

//确认endAddress的方位

if(expressLength-startAddress>=16)//标明显现满了

{

endAddress=startAddress+15;

}

else//标明后边还有几个空要填满

{

endAddress=expressLength-1;

}

}

}

void deleteAndDisplay(void)

{

if(startAddress==0&&cursorPosition==0)//标明无法删去了

{

return;

}

if(express[cursorPosition-1+startAddress]==s)//标明为cos

{

if(cursorPosition-6+startAddress>=0&&express[cursorPosition-6+startAddress]==a)

{

deleteOneAndDisplay();

deleteOneAndDisplay();

deleteOneAndDisplay();

deleteOneAndDisplay();

deleteOneAndDisplay();

deleteOneAndDisplay();

}

else

{

deleteOneAndDisplay();

deleteOneAndDisplay();

deleteOneAndDisplay();

}

}

else if(express[cursorPosition-1+startAddress]==n)//标明为sin tan 或许ln

{

if(express[cursorPosition-2+startAddress]==l)//标明为ln

{

deleteOneAndDisplay();

deleteOneAndDisplay();

}

else if(cursorPosition-6+startAddress>=0&&express[cursorPosition-6+startAddress]==a)//标明为arcsin或许acrtan运算

{

deleteOneAndDisplay();

deleteOneAndDisplay();

deleteOneAndDisplay();

deleteOneAndDisplay();

deleteOneAndDisplay();

deleteOneAndDisplay();

}

else//标明为sin、tan

{

deleteOneAndDisplay();

deleteOneAndDisplay();

deleteOneAndDisplay();

}

}

else if(express[cursorPosition-1+startAddress]==g)

{

deleteOneAndDisplay();

deleteOneAndDisplay();

}

else//标明删去一个

{

deleteOneAndDisplay();

}

}

void displayInputExpress(void)//输入时调用的

{

if(keyCode===)

{

return;

}

if(cursorPosition==15&&startAddress+1<=100)

{

startAddress++;

endAddress=14+startAddress;

return;

}

else //仅能供给第一次输入时的显现

{

endAddress=expressLength-1;

cursorPosition++;

return;

}

warning(“error 1”);

}

void displayModifyExpress(void)

{

if(keyCode===)

{

return;

}

if(keyCode==129||keyCode==130||keyCode==131)//标明为三个字符要输入

{

insertOneAndDisplay();

insertOneAndDisplay();

insertOneAndDisplay();

}

else if(keyCode==139||keyCode==140)

{

insertOneAndDisplay();

insertOneAndDisplay();

}

else if(keyCode>=141&&keyCode<=142)

{

insertOneAndDisplay();

insertOneAndDisplay();

insertOneAndDisplay();

insertOneAndDisplay();

insertOneAndDisplay();

insertOneAndDisplay();

}

else

{

insertOneAndDisplay();

}

}

void dispaly_dataA(void);

void dispaly_dataA(void)

{

if(dataA>=pow(10,36))

{

warning(“Answer Over Flow!”);

dataA=0;

dispaly_dataA();

}

else

{

sprintf(LCDBuffer,”g”,dataA);

loadAndDisplay(1);

}

}

void warning(char *p)

{

sprintf(LCDBuffer,”s”,p);

loadAndDisplay(0);

delayms(500);

sprintf(LCDBuffer,”s”,” “);//清屏

loadAndDisplay(0);

displayExpress();

}

void moveLogo(void)//把字符向前移动

{

unsigned char i,temp;

temp=LCDBuffer[0];

for(i=0;i<15;i++)

{

LCDBuffer[i]= LCDBuffer[i+1];//前移动

}

LCDBuffer[15]=temp;

}

void showLogo(void)

{

char i;

strcpy(LCDBuffer,” HuaQiang_PCB”);//前次参与竞赛时用的Logo

for(i=0;i<19;i++)

{

loadAndDisplay(0);

moveLogo();

delayms(100);

}

}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/dianyuan/kaiguan/262927.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部