#include
#include
#include "lcd_drive.h"
#define uchar unsigned char
#define uint unsigned int
void main()
{
uint key();
void deplay(uchar j_p);
void deplay_s(long a);
uchar i=1;
uchar jp=-1;
uchar flat;
uint a,b;
delay_ms(10);
lcd_init();
lcd_clr();
lcd_wcmd(0x80);
while(1)
{
while(jp==-1)
jp=key();
deplay(jp);
switch (jp)
{
case '=': switch(flat)
{
case '+': a+=b; deplay_s(a); break;
case '-': a=b-a; deplay_s(a); break;
case '*': a*=b; deplay_s(a); break;
case '/': a=b/a; deplay_s(a); break;
default:break;
}
break;
case '+':flat='+';b=a;i=0;break;
case '-':flat='-';b=a;i=0;break;
case '*':flat='*';b=a;i=0;break;
case '/':flat='/';b=a;i=0;break;
case 'c':a=0;b=0;lcd_clr();break;
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:a=a*10*i+jp;i=1;break;
}
jp=-1;
}
}
//-------矩阵键盘扫描-------
uint key()
{
uchar h = 0,l = 0;//矩阵行h,列l
uchar j_p;
P3 = 0x0f;
if(P3 != 0x0f)
{
delay_ms( 2 );
if(P3!=0x0f)
{
l = P3 & 0x0f ;
P3 = 0xf0;
h = P3 & 0xf0;
}
}
switch(l | h)
{
case 0x7e:j_p=0;break; //按键0
case 0x7d:j_p=1;break; //按键1
case 0x7b:j_p=2;break; //按键2
case 0x77:j_p=3;break; //按键3
case 0xbe:j_p=4;break; //按键4
case 0xbd:j_p=5;break; //按键5
case 0xbb:j_p=6;break; //按键6
case 0xb7:j_p=7;break; //按键7
case 0xde:j_p=8;break; //按键8
case 0xdd:j_p=9;break; //按键9
case 0xdb:j_p='+';break; //按键a
case 0xd7:j_p='-';break; //按键b
case 0xee:j_p='=';break; //按键Enter
case 0xed:j_p='*';break; //按键c
case 0xeb:j_p='/';break; //按键e
case 0xe7:j_p='c';break; //按键取消
default:return -1;break;
}
P3=0x0f;
while(P3!=0x0f);
return j_p;
}
void deplay(uchar j_p)
{
if(j_p != -1)
{
if(j_p >= 0 && j_p <= 9)
j_p+=48;
lcd_wdat(j_p);
}
}
void deplay_s(long a)
{
int n[10];
uint i=0;
while(a)
{
n[i]=a%10;
a/=10;
i++;
}
while(i)
deplay(n[--i]);
}
加减和几位乘除的话勉强吧,网上有很多