基于51单片机的简易计算器



//S4:+    S8:-   S12:*   S16:/   S15: =   S13:  REMOVE

//

#include
#define DIG P0
#define KEY P1
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
unsigned long int count=0,sum=1;
int a[11]={
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x00};
unsigned  char KeyDate;
unsigned char symbol;
void delay();  
void KeyDown();  
void DigDisplay();
void conversion();
void DigDisplayNumble();
void main(void)
{
 while(1)
 {
  KeyDown();
 // conversion();//
  DigDisplay();
 }    
}
void KeyDown()//
{
 KEY=0x0f;
 if(KEY!=0x0f)
 {
  delay();
  if(KEY!=0x0f)
  { 
   KEY=0X0F;
   switch(KEY)
   {
    case(0X07): KeyDate=1;break;
    case(0X0b): KeyDate=2;break;
    case(0X0d): KeyDate=3;break;
    case(0X0e): KeyDate=4;break;
   }  
   KEY=0XF0;
   switch(KEY)
   {
    case(0X70): KeyDate=KeyDate;break;
    case(0Xb0): KeyDate=KeyDate+4;break;
    case(0Xd0): KeyDate=KeyDate+8;break;
    case(0Xe0): KeyDate=KeyDate+12;break;
   }
   while(KEY!=0xf0) ;
    delay();
   conversion();//
  }
 }
}
void DigDisplay()
{
 unsigned int j,p=0; 
 for(j=0;j<8;j++)
 {
  switch(j)
  {
   case(0):
    LSA=0;LSB=0;LSC=0;DIG=a[count%10]; break;
   case(1):
    LSA=1;LSB=0;LSC=0;
    if(count>=10)
     DIG=a[count%100/10];
    else
     DIG=a[10];
    break;
   case(2):
    LSA=0;LSB=1;LSC=0;
    if(count>=100)
     DIG=a[count%1000/100];
    else
     DIG=a[10];
    break;
   case(3):
    LSA=1;LSB=1;LSC=0;
    if(count>=1000)
     DIG=a[count%10000/1000];
    else
     DIG=a[10];
    break;
   case(4):
    LSA=0;LSB=0;LSC=1;
    if(count>=10000)
     DIG=a[count%100000/10000];
    else
     DIG=a[10];
    break;
   case(5):
    LSA=1;LSB=0;LSC=1;
    if(count>=100000)
     DIG=a[count%1000000/100000];
    else
     DIG=a[10];
    break;
   case(6):
    LSA=0;LSB=1;LSC=1;
    if(count>=1000000)
     DIG=a[count%10000000/1000000];
    else
     DIG=a[10];
    break;
   case(7):
    LSA=1;LSB=1;LSC=1;
    if(count>=10000000)
     DIG=a[count%100000000/10000000];
    else
     DIG=a[10];
    break; 
  }
  p=10;      
  while(p--); 
 
  DIG=0x00;
 }
}

void conversion()
{
 unsigned keydate_count;
   if(KeyDate%4!=0)
  {
   if(KeyDate!=15)//
    {
     switch(KeyDate)//
     {
       case 1:
       case 2:
       case 3:keydate_count=KeyDate;break;
       case 5:
       case 6:
       case 7:keydate_count=KeyDate-1;break;
       case 9:
       case 10:
       case 11: keydate_count=KeyDate-2;break;
       case 14 :keydate_count=0;break;
       default :break;      
     }
     count=count*10+keydate_count;
    }
    else//
    {
     if(symbol=='+')
      sum+=count;
     if(symbol=='-')
      sum-=count;
     if(symbol=='*')
      sum*=count;
     if(symbol=='/')
      sum/=count;
     count=sum;
    }
  }
  else  //
  {
    switch(KeyDate)//
    {
     case 4: symbol='+';break;
     case 8: symbol='-';break;
     case 12: symbol='*';break;
     case 16: symbol='/';break;
    }
    sum=count;
    count=0;
   }  
}
void delay()  
{
 unsigned char b,c;
    for(c=200;c>0;c--)
        for(b=200;b>0;b--);
}

你可能感兴趣的:(基于51单片机的简易计算器)