⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 text1.c

📁 用51单片机实现一个简单计算器的功能
💻 C
字号:
#include <regx52.h>
#define uchar unsigned char
#define uint unsigned int

unsigned char code dispcode[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07, 
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00};
  
unsigned char code dispbitcode[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0xff}; 


 //unsigned char dispbuf[9]={1,1,1,1,1,16,16,16,16};
unsigned char dispbuf[9]={16,16,16,16,16,16,16,16,16};

unsigned char dispbufaa[9]={16,16,16,16,16,16,16,16,16};
//unsigned char dispbufaa[9]={1,2,3,16,16,16,16,16,16};

unsigned char dispbufbb[9]={16,16,16,16,16,16,16,16,16}; 
//unsigned char dispbufbb[9]={1,2,3,16,16,16,16,16,16};

unsigned char dispbufccc[9]={16,16,16,16,16,16,16,16,16};

uint aa=0,bb=0,ccc=0;

uchar fuhao,T=1;

unsigned char key=16;

unsigned char dispbitcount,keypos,cc; 

//*******************************************************************

 void delay02ss(unsigned char m)
{
  unsigned char k;
  for(;m>0;m--)
  for(k=16;k>0;k--);
}




char keyscan1()
{
  unsigned char temp,ridx=0xef,cidx=0xf7,aa,bb,cc,key=16;
  unsigned int i,j,i1,j1;
  for(i=0;i<4;i++)
   { 
   	P3=0xff;
	P3=(ridx<<i)|0x0f;
	temp=P3&0x0f;
	if(temp!=0x0f)
	{
	      for(i1=50;i1>0;i1--)				                                                                                                                            
          for(j1=200;j1>0;j1--);
	      temp=P3&0x0f;
		  for(j=0;j<4;j++)
		  {
		  aa=temp|0xf0;
		  bb=cidx>>j;
		  cc=bb|0xf0;
			  if(aa==cc)
			  {
			    key=i*4+j;
			    break;
			  }	 
  
		  }
	
 
     }
	temp=P3;
	temp=temp & 0x0f;           
    while(temp!=0x0f)
    {
      temp=P3;
      temp=temp & 0x0f;              
    }
		

  }
  return key;
    
}


char keyscan2()
{
  unsigned char temp,ridx=0xef,cidx=0xf7,aa,bb,cc,key=16;
  unsigned int i,j,i1,j1;
  for(i=0;i<4;i++)
   { 
   	P3=0xff;
	P3=(ridx<<i)|0x0f;
	temp=P3&0x0f;
	if(temp!=0x0f)
	{
	      for(i1=50;i1>0;i1--)				                                                                                                                            
          for(j1=200;j1>0;j1--);
	      temp=P3&0x0f;
		  for(j=0;j<4;j++)
		  {
		  aa=temp|0xf0;
		  bb=cidx>>j;
		  cc=bb|0xf0;
			  if(aa==cc)
			  {
			    key=i*4+j;
			    break;
			  }	 
  
		  }
	
 
     }
	temp=P3;
	temp=temp & 0x0f;           
    while(temp!=0x0f)
    {
      temp=P3;
      temp=temp & 0x0f;              
    }
		

  }
  return key;
    
}
void change(unsigned char *p,unsigned char count) 
{ 
  while(count>0) 
    { 
      *(p+count)=*(p+count-1); 
      count--; 
    } 
}
void delete()
{
    uchar dispbuf1[9]={16,16,16,16,16,16,16,16,16};
	dispbuf1[0]=dispbuf[1];
	dispbuf1[1]=dispbuf[2];
	dispbuf1[2]=dispbuf[3];
	dispbuf1[3]=dispbuf[4];
	dispbuf1[4]=dispbuf[5];
	dispbuf1[5]=dispbuf[6];
	dispbuf1[6]=dispbuf[7];	
	dispbuf1[7]=dispbuf[8];
	dispbuf[0]=dispbuf1[0];
	dispbuf[1]=dispbuf1[1];
	dispbuf[2]=dispbuf1[2];
	dispbuf[3]=dispbuf1[3];
	dispbuf[4]=dispbuf1[4];
	dispbuf[5]=dispbuf1[5];
	dispbuf[6]=dispbuf1[6];
	dispbuf[7]=dispbuf1[7];
	dispbuf[8]=16;
	keypos--;
}
void clear()
{
	 dispbuf[0]=16;
	 dispbuf[1]=16;
	 dispbuf[2]=16;
	 dispbuf[3]=16;
	 dispbuf[4]=16;
	 dispbuf[5]=16;
	 dispbuf[6]=16;
	 dispbuf[7]=16;
	 dispbuf[8]=16;
	 keypos=0;
}

void valueaa()
{
    uint a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0;
	TR1=0;
    //if(dispbuf[7]!=16)a=dispbuf[7]*10000000;
	//if(dispbuf[6]!=16)b=dispbuf[6]*1000000;
	//if(dispbuf[5]!=16)c=dispbuf[5]*100000;
	if(dispbuf[4]!=16)d=dispbuf[4]*10000;
	if(dispbuf[3]!=16)e=dispbuf[3]*1000;
	if(dispbuf[2]!=16)f=dispbuf[2]*100;
	if(dispbuf[1]!=16)g=dispbuf[1]*10;
	if(dispbuf[0]!=16)h=dispbuf[0];	
    aa=a+b+c+d+e+f+g+h;
    dispbufaa[0]=dispbuf[0];
	dispbufaa[1]=dispbuf[1];
	dispbufaa[2]=dispbuf[2];
	dispbufaa[3]=dispbuf[3];
	dispbufaa[4]=dispbuf[4];
	dispbufaa[5]=dispbuf[5];
	dispbufaa[6]=dispbuf[6];
	dispbufaa[7]=dispbuf[7];
	dispbufaa[8]=dispbuf[8];
}

void valuebb()
{
    uint a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0;
	TR1=0;
    //if(dispbuf[7]!=16)a=dispbuf[7]*10000000;
	//if(dispbuf[6]!=16)b=dispbuf[6]*1000000;
	//if(dispbuf[5]!=16)c=dispbuf[5]*100000;
	if(dispbuf[4]!=16)d=dispbuf[4]*10000;
	if(dispbuf[3]!=16)e=dispbuf[3]*1000;
	if(dispbuf[2]!=16)f=dispbuf[2]*100;
	if(dispbuf[1]!=16)g=dispbuf[1]*10;
	if(dispbuf[0]!=16)h=dispbuf[0];
    bb=a+b+c+d+e+f+g+h;
	dispbufbb[0]=dispbuf[0];
	dispbufbb[1]=dispbuf[1];
	dispbufbb[2]=dispbuf[2];
	dispbufbb[3]=dispbuf[3];
	dispbufbb[4]=dispbuf[4];
	dispbufbb[5]=dispbuf[5];
	dispbufbb[6]=dispbuf[6];
	dispbufbb[7]=dispbuf[7];
	dispbufbb[8]=dispbuf[8];
} 

void fengaa()
{
	uchar a,b,c,d,e,f,g,h,i;
	i=16;
	h=16;
	g=16;
	f=16;
	e=aa/10000;
	d=aa%10000/1000;
	c=aa%10000%1000/100;
	b=aa%10000%1000%100/10;
	a=aa%10000%1000%100%10;
	dispbufaa[8]=i;
	dispbufaa[7]=h;
	dispbufaa[6]=g;
	dispbufaa[5]=f;
	dispbufaa[4]=e;
	dispbufaa[3]=d;
	dispbufaa[2]=c;
	dispbufaa[1]=b;
	dispbufaa[0]=a;
}

void fengbb()
{
	uchar a,b,c,d,e,f,g,h,i;
	i=16;
	h=16;
	g=16;
	f=16;
	e=bb/10000;
	d=bb%10000/1000;
	c=bb%10000%1000/100;
	b=bb%10000%1000%100/10;
	a=bb%10000%1000%100%10;
	dispbufbb[8]=i;
	dispbufbb[7]=h;
	dispbufbb[6]=g;
	dispbufbb[5]=f;
	dispbufbb[4]=e;
	dispbufbb[3]=d;
	dispbufbb[2]=c;
	dispbufbb[1]=b;
	dispbufbb[0]=a;
}


void fengccc()
{
	uchar a,b,c,d,e,f,g,h,i;
    if(fuhao==1)ccc=aa+bb;
	if(fuhao==2)ccc=aa-bb;
	if(fuhao==3)ccc=aa*bb;
	if(fuhao==4)ccc=aa/bb;
    T=1;
	i=16;
	h=16;
	g=16;
	f=16;
	e=ccc/10000;
	d=ccc%10000/1000;
	c=ccc%10000%1000/100;
	b=ccc%10000%1000%100/10;
	a=ccc%10000%1000%100%10;
	dispbufccc[8]=i;
	dispbufccc[7]=h;
	dispbufccc[6]=g;
	dispbufccc[5]=f;
	dispbufccc[4]=e;
	dispbufccc[3]=d;
	dispbufccc[2]=c;
	dispbufccc[1]=b;
	dispbufccc[0]=a;
}



void displayccc()	   //结果输出
{	  
      uchar	m=19,i,key,j;

		      if(dispbufccc[4]==0)
		        if(dispbufccc[3]==0)
	 	          if(dispbufccc[2]==0)
				    if(dispbufccc[1]==0)
					   if(dispbufccc[0]==0)
					   {
							dispbufccc[4]=16;
							dispbufccc[3]=16;
							dispbufccc[2]=16;
							dispbufccc[1]=16;
							dispbufccc[0]=16;
					   }
		      if(dispbufccc[4]==0)
		        if(dispbufccc[3]==0)
	 	          if(dispbufccc[2]==0)
				    if(dispbufccc[1]==0)
					   if(dispbufccc[0]!=0)
					   {
							dispbufccc[4]=16;
							dispbufccc[3]=16;
							dispbufccc[2]=16;
							dispbufccc[1]=16;
					   }
		      if(dispbufccc[4]==0)
		        if(dispbufccc[3]==0)
	 	          if(dispbufccc[2]==0)
				    if(dispbufccc[1]!=0)
					   {
							dispbufccc[4]=16;
							dispbufccc[3]=16;
							dispbufccc[2]=16;
					   }
		      if(dispbufccc[4]==0)
		        if(dispbufccc[3]==0)
	 	          if(dispbufccc[2]!=0)
					   {
							dispbufccc[4]=16;
							dispbufccc[3]=16;
					   }
		      if(dispbufccc[4]==0)
		        if(dispbufccc[3]!=0)
					   {
							dispbufccc[4]=16;
					   }
		
      while(1)
	  {
      TR1=0;
      
	  for(i=0;i<8;i++)
	  {
	  	P0=dispcode[dispbufccc[i]];
	    P2=dispbitcode[i];
	    delay02ss(m);
	  }
	  key=keyscan1();
	  if(key==15)return;
	  }

}

void displayaa()	   //aa结果输出
{	  
      uchar	m=19,i,keya;
      while(1)
	  {
      TR1=0;
      
	  for(i=0;i<8;i++)
	  {
	  	P0=dispcode[dispbufaa[i]];
	    P2=dispbitcode[i];
	    delay02ss(m);
	  }
	  keya=keyscan1();
	  if(keya==15)return;
	  }

}

void displaybb()	   //bb结果输出
{	  
      uchar	m=19,i,keyb;
      while(1)
	  {
      TR1=0;
      
	  for(i=0;i<8;i++)
	  {
	  	P0=dispcode[dispbufbb[i]];
	    P2=dispbitcode[i];
	    delay02ss(m);
	  }
	  keyb=keyscan1();
	  if(keyb==15)return;
	  }

}

void moni()
{
  char key1=16;
  TMOD=0x10; 
  TH1=(65536-4000) / 256; 
  TL1=(65536-4000) % 256; 
  TR1=1; 
  ET1=1; 
  EA=1; 
  while(1) 
    { 
	   
	   	key1=keyscan2();
			  if((key1>=0) && (key1<10)) 
              { 
			    cc=1;
                keypos++; 
                if(keypos<9) 
                  { 
				    change(dispbuf,keypos); 
                    dispbuf[0]=key1; 
					key1=16;
                  } 
                  else 
                    { 
                      keypos=9; 
                    } 
              }  
			  if(key1==10)
			  {
			  	delete();
				key1=16;
			  } 
			  if(key1==11)
			  {
			  	clear();
				key1=16;
			  }	
			  if(11<key1 && key1<16 && T==1)
			  {
			     T=2;
			  	 if(key1==12)fuhao=1;
				 if(key1==13)fuhao=2;
				 if(key1==14)fuhao=3;
				 if(key1==15)fuhao=4;
				 P0=0;
			  	 valueaa();
				 return;
			  } 
			  if(key1==15 && T==2)
			  {
			     T=3;
                 valuebb();
				 P0=0;
				 return;
			  }	 
      } 
}


 //*****************************************************
void main(void)
{
    //uchar key=16;
   while(1)
	{
		  		key=keyscan1();
				if(T==1||T==2) 
					{
						clear();
						moni();
						key=16;
					}
				if(T==3)
						{
							fengccc();
							displayccc();
							key=16;
						}
		
	}

}
 //***********************************
void t0(void) interrupt 3 using 0 
{ 
  TH1=(65536-4000) / 256; 
  TL1=(65536-4000) % 256; 
  P0=dispcode[dispbuf[dispbitcount]]; 
  P2=dispbitcode[dispbitcount]; 
  dispbitcount++; 
  if (dispbitcount==9) 
    { 
      dispbitcount=0; 
    } 
} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -