没运算级别,程序如下:
#include <reg51.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define slong signed long
#define KeyPort P1
#define LCDPort P3
#define RadixMax 4
sbit RS = P2^5;
sbit RW = P2^6;
sbit EN = P2^7;
enum Sign{Null=10,Add,Sub,Mul,Div,Enter};
enum Sign Opera = Null;
bit DelayFlag = 0;
bit KeyFlag = 0; //键按下标志
uchar DelayBase = 0; //延时基数
uchar KeyData = 0; //按键值
uchar KeyCnt= 0; //按键次数
float Data = 0;
float Result = 0;
uchar code LCDTab[] = {"0123456789.+-*/="};
uint code PowTab[] = {10,100,1000,10000,10000};
void TimerInit(void);
uchar KeyRead(void);
void LCDInit(void);
void LCD_EN(void);
void LCDWriteCmd(uchar);
void LCDWriteData(uchar);
void LCDSet_XY(uchar, uchar);
void LCDWriteChar(uchar,uchar,uchar *);
void DisplayResult(float);
void DisplaySign(enum Sign);
void DisplayMum(uchar);
void DisplayClear(void);
void main(void)
{
KeyPort = 0XFF;
TimerInit();
EA = 1;
LCDInit();
while(1)
{
if(KeyFlag)
{
KeyFlag = 0;
switch(KeyData)
{
case 0: break;
case 0XEE: DisplayMum(7); break;
case 0XDE: DisplayMum(8); break;
case 0XBE: DisplayMum(9); break;
case 0X7E: DisplaySign(Div); break;
case 0XED: DisplayMum(4); break;
case 0XDD: DisplayMum(5); break;
case 0XBD: DisplayMum(6); break;
case 0X7D: DisplaySign(Mul); break;
case 0XEB: DisplayMum(1); break;
case 0XDB: DisplayMum(2); break;
case 0XBB: DisplayMum(3); break;
case 0X7B: DisplaySign(Sub); break;
case 0XE7: DisplayClear(); break;
case 0XD7: DisplayMum(0); break;
case 0XB7: DisplaySign(Enter);
DisplayResult(Result); break;
case 0X77: DisplaySign(Add); break;
}
}
}
}
void DisplayMum(uchar Index) //按下数字键调用此函数,Index为相应的键值
{
LCDWriteChar(KeyCnt-1,0,&LCDTab[Index]);
Data = Data*10 + Index;
}
void DisplaySign(enum Sign Index) //按下运算符键调用此函数,Index为相应的运算,包括加减乘除等于
{
LCDWriteChar(KeyCnt-1,0,&LCDTab[Index]); //显示运算符号
switch(Opera) //判断上一次按下的运算符,执行相应的操作
{
case Null: Result = Data; break;
case Add: Result = Result + Data; break;
case Sub: Result = Result - Data; break;
case Mul: Result = Result * Data; break;
case Div: Result = Result / Data; break;
case Enter: Result = Result;
}
Data = 0;
Opera = Index;
}
void DisplayResult(float Index) //显示计算结果
{
uchar i = 0;
uchar j = 0;
slong IndexTemp = 0;
bit NegativeFlag = 0; //负数标志
bit RadixFlag = 0; //小数标志
IndexTemp = (slong)(Index * 10);
if(IndexTemp%10 != 0)
{
RadixFlag = 1;
for(j=0;j<RadixMax;j++)
{
IndexTemp = (slong)(Index * PowTab[j]);
if(IndexTemp%10 == 0)
{
IndexTemp = IndexTemp/10;
break;
}
}
}
else
IndexTemp = IndexTemp/10;
if(IndexTemp == 0)
LCDWriteChar(15,1,&LCDTab[0]);
if(IndexTemp < 0) //如果IndexTemp为负数
{
IndexTemp = (~IndexTemp) + 1; //则将IndexTemp取反再加以,转化为正数
NegativeFlag = 1;
}
if(IndexTemp > 0)
{
while(IndexTemp != 0)
{
LCDWriteChar(15-i,1,&LCDTab[IndexTemp%10]);
IndexTemp = IndexTemp/10;
i ++;
if(RadixFlag) //如果是小数,则显示.
{
if(i == j)
{
RadixFlag = 0;
LCDWriteChar(15-i,1,&LCDTab[10]);
i = i + 1;
if(IndexTemp == 0)
LCDWriteChar(15-i,1,&LCDTab[0]);
}
}
}
if(NegativeFlag) //如果是负数,则显示-号
{
NegativeFlag = 0;
LCDWriteChar(15-i,1,&LCDTab[12]);
}
}
}
仿真图
(原文件名:仿真图.jpg)
一周热门 更多>