簡單的加法計算器單片機c程序設計
//----------簡單的加法計算器-----------------
//------用數(shù)碼管顯示兩位數(shù)的加法計算器;----------
//P0作為數(shù)碼管的段碼
//P1作為數(shù)碼管的位碼
//P2口作為矩陣鍵盤0----9,+,=;//本實訓箱只有12個按鍵,P20---P26;P27未用;
先輸入第一個數(shù),當按+時,清屏,再顯示輸入的第二個數(shù),
按=時,顯示他們的和,再次按=數(shù)據(jù)清0;
//本程序在普中單片機實訓箱上通過
*/
#include"reg51.h"
#include
#define uint unsigned int
#define uchar unsigned char
uchar code dula[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,
0x7d,0x07,0x7f,0x6f,0x00};
uchar wula[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
uchar table[8]={0,0,10,10,10,10,10,10};
uchar add1[2]={0,10};
uchar add2[2]={0,10};
uchar sum[3]={0,10,10};
uchar sum1,aa,bb,cc;
bit flagsum,flagadd,flag;
uchar x,y,z,k,keyword;
void dispadd1();
void dispadd2();
void dispsum();
void delay(uint z);
void display();
void jisuan();
void keyscan();
void main()
{
keyword=0;
k=0;
while(1)
{
keyscan();
if((flagadd==0)&&(flagsum==0))
dispadd1();
if((flagadd==1)&&(flagsum==0))
dispadd2();
if((flagsum==1))
dispsum();
}
}
void keyscan()
{
uchar temp1,temp2;
P2=0xf0;
if(P2!=0xf0)
{
delay(100);
P2=0xf0;
if(P2!=0xf0)
{
temp1=P2;
P2=0x0f;
temp2=P2;
while(P2!=0x0f);
keyword=temp1|temp2;
switch(keyword)
{
case 0xed: keyword=0;flag=0;break;
case 0xdd: keyword=1;flag=0;break;
case 0xbd: keyword=2;flag=0;break;
case 0x7d: keyword=3;flag=0;break;
case 0xeb: keyword=4;flag=0;break;
case 0xdb: keyword=5;flag=0;break;
case 0xbb: keyword=6;flag=0;break;
case 0x7b: keyword=7;flag=0;break;
case 0xe7: keyword=8;flag=0;break;
case 0xd7: keyword=9;flag=0;break;
case 0xb7: {flagadd=1;
flag=1;}break;
case 0x77: {flagsum=~flagsum;cc++;}break;
default: break;
}
if((flagadd==0)&&(flagsum==0))
{
add1[k]=keyword;
if(k>1)
k=1;//}
else
k++;}
if((flagadd==1)&&(flagsum==0))
{if(flag==1)
{k=0;flag=0;}
else
{ add2[k]=keyword;
if(k>1)
k=1;
else
k++;}}
if(flagsum==1)
{flagadd=0;flag=0;k=0;
if(cc==2)
{cc=0;add1[0]=0;add1[1]=10;
add2[0]=0;add2[1]=10;
sum[0]=0;sum[1]=10;sum[2]=10;}
if(add1[1]!=10)
aa=add1[0]*10+add1[1];
else
aa=add1[0];
//
if(add2[1]!=10)
bb=add2[0]*10+add2[1];
else
bb=add2[0];
sum1=aa+bb;
sum[0]=sum1/100;
sum[1]=sum1%100/10;
sum[2]=sum1%10;
}
if(flagsum==0)
{if(cc==2)
{cc=0;flagadd=0;flag=0;k=0;
add1[0]=0;add1[1]=10;
add2[0]=0;add2[1]=10;
sum[0]=0;sum[1]=10;sum[2]=10;}
}
}
}
}
void dispadd1()
{ uchar i;
for(i=0;i<2;i++)
{ P0=dula[add1[i]] ;
P1=wula[i];
delay(5);
// P0=0;
}
}
//-----------------
void dispadd2()
{ uchar i;
for(i=0;i<2;i++)
{ P0=dula[add2[i]] ;
P1=wula[i];
delay(5);
P0=0;
}
}
//-------------------
void dispsum()
{ uchar i;
for(i=0;i<3;i++)
{ P0=dula[sum[i]] ;
P1=wula[i];
delay(5);
P0=0;
}
}
//=====================
void delay(uint z)
{
uint x,y;
for(x=0;x
}
評論