#include<reg51.h>
#define uint unsigned int
#define uchar unsigned char
sbit lcden=P1^1; //LCD1602控制引脚,使能引脚
sbit rs=P1^0;
sbit rw=P1^2;
sbit busy=P0^7;// LCD忙
uchar mode=0; //记录清零键状态
char i, j, temp, num, num_1;
long a, b, c; // a:第一个操作数,b:第二个操作数,c:得数
float a_c, b_c;
uchar flag, signal;// flag:运算符号// signal: 1:+ 2:- 3:* 4:/
uchar code num_table[]={
7,8,9,0,
4,5,6,0,
1,2,3,0,
0,0,0,0
};
uchar code display_table[]={
0x37,0x38,0x39,0x2f, //7,8,9,/
0x34,0x35,0x36,0x2a, //4,5,6,*
0x31,0x32,0x33,0x2d, //1,2,3,-
0x01,0x00,0x3d,0x2b //C,0,=,+
};
void delay(unsigned int z) {// 延时函数
unsigned char i, j;
while(z--){
i = 2;j = 239;
do{
while(--j);
}while(--i);
}
}
void write_com(uchar com){//命令
rs=0;P0=com;
delay(5);lcden=1;
delay(5);lcden=0;
}
void write_date(uchar date) { // 数据
rs=1; P0=date;
delay(5);lcden=1;
delay(5);lcden=0;
}
void init() {//初始化
num=-1;lcden=1; rw=0;
write_com(0x38); delay(5);
write_com(0x38); delay(5);
write_com(0x0c); delay(1);
write_com(0x06); delay(1);
write_com(0x80);delay(1);
write_com(0x01);
num_1=0;i=0;j=0;
a=0;b=0;c=0;flag=0;signal=0;
}
void keyscan() {//键盘扫描
P2 = 0xef; // 高四位代表行,第四位代表列,检测低电平//1110 1111 - 第一行
if(P2 != 0xef) {// 判断按键
delay(20);
if(P2 != 0xef) { // 再次判断
temp = P2 & 0x0f; // 屏蔽高四位,只考虑第四位确定是哪一列// 1110 1111 & 0000 1111 = 0000 1111
switch(temp){ // 判断第四位哪一位为0,对应列按下
case 0x0e: num = 0; break;// 0000 1110 -- 7
case 0x0d: num = 1; break;// 0000 1101 -- 8
case 0x0b: num = 2; break;// 0000 1011 -- 9
case 0x07: num = 3; break;// 0000 0111 -- ÷
}
}
while(P2 != 0xef); // 继续判断
if(num == 0 || num == 1 || num == 2){ //'7','8','9'
if(j != 0){
write_com(0x01); j = 0; // 清屏
}
if(flag == 0) // 检测是否按过+-×÷按键
a = a * 10 + num_table[num];// 没有按过,第一个操作数
else
b = b * 10 + num_table[num];// 按过,第二个操作数
}
else{
flag = 1;signal = 4; // 不是7,8,9,是÷
}
i = display_table[num];write_date(i);
}
P2 = 0xdf;
if(P2 != 0xdf){
delay(20);
if(P2 != 0xdf){
temp = P2 & 0x0f;
switch(temp){
case 0x0e: num = 4; break; //4
case 0x0d: num = 5; break; //5
case 0x0b: num = 6; break; //6
case 0x07: num = 7; break; //*
}
}
while(P2 != 0xdf);
if(num == 4 || num == 5 || num == 6){ //'4','5','6'
if(j != 0){
write_com(0x01); j = 0;
}
if(flag == 0)
a = a * 10 + num_table[num];
else
b = b * 10 + num_table[num];
}
else{
flag = 1;signal = 3;//×
}
i = display_table[num]; write_date(i);
}
P2 = 0xbf;
if(P2 != 0xbf){
delay(20);
if(P2 != 0xbf){
temp = P2 & 0x0f;
switch(temp){
case 0x0e: num=8; break; //1
case 0x0d: num=9; break; //2
case 0x0b: num=10;break; //3
case 0x07: num=11;break; //-
}
}
while(P2 != 0xbf);
if(num == 8 || num == 9 || num == 10){ //'1','2','3'
if(j != 0){
write_com(0x01); j = 0;
}
if(flag == 0)
a = a * 10 + num_table[num];
else
b = b * 10 + num_table[num];
}
else{
flag=1;signal=2;
}
i = display_table[num]; write_date(i);
}
P2 = 0x7f;
if(P2 != 0x7f){
delay(20);
if(P2 != 0x7f){
temp = P2 & 0x0f;
switch(temp){
case 0x0e:num=12; break; //AC
case 0x0d:num=13; break; //0
case 0x0b:num=14; break; //=
case 0x07:num=15; break; //+
}
}
while(P2 != 0x7f);
switch(num){
case 12:{// ??
if(mode == 0){
write_com(0x0f);
a = 0;b = 0;flag = 0;signal = 0;
mode++;
}
if(mode != 0){
write_com(0x01);
a = 0;b = 0;flag = 0;signal = 0;
mode++;write_com(0x0f);
}
}break;
case 13: { // 0
if(flag==0){
a = a*10; write_date(0x30); P2 = 0;
}
else if(flag >= 1){
b = b * 10; write_date(0x30);
}
} break;
case 14:{// =
j = 1; write_com(0x0c);
if(signal == 1) {
write_com(0x80+0x4f);write_com(0x04);
c = a + b;
while(c!=0){
write_date(0x30+c%10); c=c/10;
}
write_date(0x3d);
a=0;b=0;flag=0;signal=0;
}
else if(signal==2) {
write_com(0x80+0x4f);write_com(0x04);
if(a-b>0)
c=a-b;
else
c=b-a;
while(c!=0) {
write_date(0x30+c%10);c=c/10;
}
if(a-b<0)
write_date(0x2d);
if(a==b)
write_date(0x30+0);
write_date(0x3d);
a=0;b=0;flag=0;signal=0;
}
else if(signal==3) {
write_com(0x80+0x4f); write_com(0x04);
c=a*b;
while(c!=0) {
write_date(0x30+c%10);c=c/10;
}
write_date(0x3d);
a=0;b=0;flag=0;signal=0;
}
else if(signal==4)
{
write_com(0x80+0x4f);write_com(0x04);
i=0;
if(b!=0){
c=(long)(((float)a/b)*1000);
while(c!=0){
write_date(0x30+c%10);
c=c/10;i++;
if(i==3)
write_date(0x2e);
}
if(a/b<=0){
if(i<=2){
if(i==1)
write_date(0x30);
write_date(0x2e);write_date(0x30);
}
write_date(0x30);
}
write_date(0x3d);
a=0;b=0;flag=0;signal=0;
}
else
{
write_date('!');write_date('R');
write_date('O');write_date('R');
write_date('R');write_date('E');
}
}
}break;
case 15:{
write_date(display_table[num]);flag=1;signal=1;
} break;
}
}
}
void main()
{
init();
while(1){
keyscan();
}
}