新聞中心

1602LCD顯示器的使用

作者: 時間:2016-11-21 來源:網(wǎng)絡(luò) 收藏
LCDE==P2.7 LCD使能

DB==P0 數(shù)據(jù)位
RD==P2.6 寫模式選擇
WR==P2.5 讀寫使能

本文引用地址:http://m.butianyuan.cn/article/201611/319276.htm


指令碼: 二進(jìn)制 十六進(jìn)制

清屏: 10000000 0x80;

光標(biāo)歸位: X1000000 0x40或0xC0

。。。。。。。。


讀狀態(tài)輸入:RS=L,RW=H,E=H 輸出:DB0~DB7=狀態(tài)字
寫指令輸入:RS=L,RW=L,E=下降沿脈沖,DB0~DB7=指令碼輸出:無
讀數(shù)據(jù)輸入:RS=H,RW=H,E=H 輸出:DB0~DB7=數(shù)據(jù)
寫數(shù)據(jù)輸入:RS=H,RW=L,E=下降沿脈沖,DB0~DB7=數(shù)據(jù)輸出:無

上次寫的驅(qū)動:

#include

#include"LCD1602.h"

#include

#include

#include

#include

#include

sbit lcdrs =P3^0;//數(shù)據(jù)/命令選擇端(H/L)

sbit lcdrw =P3^1;//讀/寫選擇端(H/L)

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

char theDisplayLength=0,//保存應(yīng)該在當(dāng)前顯示的位數(shù)

cursorPosition=0;//保存光標(biāo)所在顯示屏的位置

char startAddress=0,endAddress=0;//保存顯示的起始和結(jié)束地址

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)////寫數(shù)據(jù)

{

lcdrs=1;

P0=mydata;

delayms(1);

lcden=1;

delayms(1);

lcden=0;

}

void initLCD()//初始化

{

lcdrw=0;

lcden=0;

write_com(0x38); //設(shè)置16x2顯示,5x7點(diǎn)陣,8位數(shù)據(jù)接口

write_com(0x0e); //00001dcb,d=1開顯示,c=1顯示光標(biāo),b=1光標(biāo)閃爍

write_com(0x06); //000001ns,n=1字符后地址指針加一,且光標(biāo)加

//s=1寫字符,正屏顯示左移(n=1)

write_com(0x80); //設(shè)置數(shù)據(jù)地址指針(0-27H,40-67H)

write_com(0x01); //01,顯示清屏:1,數(shù)據(jù)指針清零。2,所有顯示清零

//02,顯示回車:1,數(shù)據(jù)指針清零。

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;

}

///功能函數(shù)部分

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) //移動光標(biāo)位置

{

char i;

write_com(0x02);//光標(biāo)歸位

for(i=0;i

{

write_com(0x14);//右移

}

}

//下面的代碼用于動態(tài)顯示字符的,主要的東西在上面

void moveLeftOneStep(void)//左右移動

{

if(cursorPosition==0)//表明要通過翻頁左移

{

if(startAddress!=0)//表明還可左移一次 剩下情況沒法移動

{

startAddress--;//確定顯示的結(jié)束位置

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)//表明要考慮是否進(jìn)行翻頁

{

if(startAddress+16

//表明 startAddress+15得到的是LCD最后一位對應(yīng)express的地址 再加1表示長度

{

startAddress++;

endAddress=startAddress+15;

displayExpress();

}

else if(startAddress+16==expressLength)//表明當(dāng)前已是最后一個字符

{

startAddress++;

endAddress=startAddress+14;//光標(biāo)處應(yīng)該是空

displayExpress();

}

else//表明光標(biāo)處并沒有顯示字符

{

return;

}

}

else//表明無需翻頁

{

if(cursorPosition==endAddress+1)//已到最后一個位置,光標(biāo)不能再動了

{

return;

}

cursorPosition++;

endAddress=expressLength-1;

setCursorPosition(cursorPosition);

}

}

void moveLeft(void)//startAddress+cursorPosition是光標(biāo)對應(yīng)express中的地址

{

if(startAddress+cursorPosition==0)//跳轉(zhuǎn)到最后

{

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)//表明光標(biāo)到了最后

{

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)//表明是在和前面當(dāng)中插入的 startAddress可以不變

{

//查看當(dāng)前顯示狀態(tài)

if(endAddress-startAddress==15)//表明當(dāng)前已經(jīng)滿顯了

{

endAddress--;

}

else//表明未滿顯

{

endAddress++;

}

cursorPosition++;

}

else if(cursorPosition==15)//表明光標(biāo)在顯示器第16個顯示單元處 此時 startAddress得變

{

if(endAddress-startAddress==15)//表明當(dāng)前已經(jīng)滿顯未滿顯的情況已在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--;//光標(biāo)位置要--

//確定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運(yùn)算

{

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)//輸入時調(diào)用的

{

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);

}

}



關(guān)鍵詞: 1602LCD顯示

評論


技術(shù)專區(qū)

關(guān)閉