main.c如下
#include<reg52.h> #include"1602.h" #include"delay.h" #define uc unsigned char uc a=0,b=0,c=0,d=0,h=0,l=0,m=0,flag=0,s; //a,b:两项;c:+-*/; d:运算结果 void delay(uc c1) { uc a1,b1; for(;c1>0;c1--) for(a1=38;a1>0;a1--) for(b1=130;b1>0;b1--); } void main() { LCD_Init(); //初始化液晶 DelayMs(20); //延时有助于稳定 LCD_Clear(); //清屏 while(1) { P1=0x0F; if(P1!=0x0F) { P1=0x0F; switch(P1) // 行判断 { case 0x0E: h=0;break; case 0x0D: h=1;break; case 0x0B: h=2;break; case 0x07: h=3;break; } P1=0xF0; switch(P1) // 列判断 { case 0xE0: l=0;break; case 0xD0: l=1;break; case 0xB0: l=2;break; case 0x70: l=3;break; } m=h*4+l; if(m>=0&&m<=9) { if(flag==0) { LCD_Clear(); //清屏 LCD_Write_Com(0x80) ; a=m; LCD_Write_Data(a+'0') ; } else { LCD_Write_Com(0x83) ; b=m; LCD_Write_Data(b+'0') ; } } if(m>9&&m<14) { flag=1; c=m; LCD_Write_Com(0x82) ; switch(m) { case 10:s=0x2B;break; case 11:s=0x2D;break; case 12:s=0x2A;break; case 13:s=0x2F;break; } LCD_Write_Data(s) ; } if(m==15) { a=0,b=0,c=0,d=0,flag=0; LCD_Write_String(0,0," "); LCD_Write_String(0,1," "); } if(m==14) { switch(c) { case 10:d=a+b;break; case 11:if(a>b) d=a-b; else d=b-a;break; case 12:d=a*b;break; case 13:d=a/b;break; } LCD_Write_Com(0xc0) ; LCD_Write_Data('='); LCD_Write_Data(d/10+'0') ; LCD_Write_Data(d%10+'0') ; c=0,flag=0; } delay(1); P1=0xf0; while(P1!=0xf0) { delay(1); P1=0xf0; } delay(1); } } }
如果需要 1602.c 和 delay.c 的话可问老师
接线:
LCD1602数据线接P0
其他按照LCD1602.c的引脚定义连接
4*4矩阵键盘接P1(注意正反)