#include "reg52.h"
#include "stdio.h"
#include
sbit RS = P2^6;
sbit RW = P2^5;
sbit EN = P2^7;
void DelayMs(x)
{
int y;
for(;x>0;x--)
for(y = 110;y>0;y--);
}
void LCD_Write_com(char com) //写入命令
{
RS = 0;
RW = 0;
EN = 0;
P0 = com;
DelayMs(5);
EN = 1;
DelayMs(5);
EN = 0;
}
void LCD_Write_data(char dat) //写入单个数据
{
RS = 1;
RW = 0;
EN = 0;
P0 = dat;
DelayMs(5);
EN = 1;
DelayMs(5);
EN = 0;
}
void LCD_Write_Char(int x,int y,char dat) //决定显示位置
{
if(y == 0)
LCD_Write_com(0x80+x);
else
LCD_Write_com(0x80+0x40+x);
LCD_Write_data(dat);
}
void Write_String(char x,char y,char *s)
{
while (*s)
{
LCD_Write_Char(x,y,*s);
s++;
x++;
}
}
void LCD_Init()
{
EN = 0;
LCD_Write_com(0x38);
LCD_Write_com(0x0c);
LCD_Write_com(0x06);
LCD_Write_com(0x01);
}
char KeyScan() //键盘扫描函数,使用行列反转扫描法
{
unsigned char cord_h,cord_l;//行列值中间变量 cord_h行,l为列
P1=0x0f; //行线输出全为0
cord_h=P1&0x0f; //读入列线值
if(cord_h!=0x0f) //先检测有无按键按下
{
DelayMs(10); //去抖
if((P1&0x0f)!=0x0f)
{
cord_h=P1&0x0f; //读入列线值
P1=cord_h|0xf0; //输出当前列线值
cord_l=P1&0xf0; //读入行线值
while((P1&0xf0)!=0xf0);//等待松开并输出
return(cord_h+cord_l);//键盘最后组合码值
}
}
return(0xff); //返回该值
}
char KeyPro()
{
switch(KeyScan())
{
case 0x77 :return '1' ;break;
case 0x7b :return '2' ;break;
case 0x7d :return '3' ;break;
case 0x7e :return '+' ;break;
case 0xbd :return '6' ;break;
case 0xbb :return '5' ;break;
case 0xb7 :return '4' ;break;
case 0xbe :return '-' ;break;
case 0xdb :return '8' ;break;
case 0xd7 :return '7' ;break;
case 0xde :return 'x' ;break;
case 0xdd :return '9' ;break;
case 0xe7 :return '.' ;break;
case 0xeb :return '0' ;break;
case 0xee :return '/' ;break;
case 0xed :return '=' ;break;
default:return 0xff;break;
}
}
main()
{
char i,sign,s;
unsigned char num;
char temp[16]; //最大输入16个
bit firstflag;
float a=0,b=0;
LCD_Init();
// LCD_Write_com(0x01); //清屏
while(1)
{
num = KeyPro();
if(num!=0xff)
{
if(i == 0)
LCD_Write_com(0x01);
if(('+' == num) || ('-' == num) || ('x' == num) || ('/' == num) || ('=' == num))
{
i =0;
if(firstflag==0) //flag=1为第一个数,0为第二个数
{
sscanf(temp,"%f " ,&a);
firstflag = 1;
}
else
sscanf(temp," %f " ,&b);
for(s = 0;s<16;s++)
temp[s] = 0;
LCD_Write_Char(0,1,num);
//////////////////
if(num != '=')
sign = num;
else
{
firstflag = 0;
switch(sign)
{
case '+' :a = a+b;break;
case '-' :a = a-b;break;
case 'x' :a = a*b;break;
case '/' :a = a/b;break;
default:break;
}
//LCD_Write_Char(1,1,answer);
sprintf(temp,"%g",a); //输出浮点型,无用的0不输出
Write_String(1,1,temp);//显示到液晶屏
sign = 0;
a = 0;
b = 0;
for(s=0;s<16;s++)
temp[s]=0;
}
}
else if(i<16)
{
if((1==i)&&(temp[0]=='0'))
{
if(num == '.')
{
temp[1]=num;
LCD_Write_Char(1,0,num);//输出数据
i++;
}
else
{
temp[0] = num;
LCD_Write_Char(0,0,num);
}
}
else
{
temp[i] = num;
LCD_Write_Char(i,0,num);
i++;
}
}
}
}
}