基于51单片机的计算器程序

用51单片机制作计算器

#include   //í·???t
#define uint unsigned int 
#define uchar unsigned char

sbit lcden=P2^7; //LCD1602????òy??
sbit rs=P2^6;
sbit rw=P2^5;

char i,j,temp,num,num_1;
long a,b,c;     //a,μúò???êy b,μú?t??êy c,μ?êy
float a_c,b_c;
uchar flag,fuhao;//flag±íê?ê?·?óD????·??ü°′??£?fuhao±í?÷°′??μ?ê?????????·?
//flag=1±íê?????·??ü°′??£?flag=0±íê?????·??ü??óD°′??£?
//fuhao=1?a?󷨣?fuhao=2?a??·¨£?fuhao=3?a3?·¨£?fuhao=4?a3y·¨?£

uchar code table[]={//????êy×?ê?è?êy×é
7,8,9,0,
4,5,6,0,
1,2,3,0,
0,0,0,0};
uchar code table1[]={	//?-′|àíoó??DD?üê?è???ê?×?±?μ?êy×é
7,8,9,0x2f-0x30,	//7£?8£?9£???
4,5,6,0x2a-0x30,	//4, 5, 6£??á
1,2,3,0x2d-0x30,	//1, 2, 3£?£-
0x01-0x30,0,0x3d-0x30,0x2b-0x30//C£?0£?=£?£?
};
void delay(uchar z) // ?ó3ùoˉêy
{
uchar y;
for(z;z>0;z--)
   for(y=0;y<110;y++);
}
void write_com(uchar com)//?üá?×óoˉêy
{
	rs=0;
	rw=0;
	P0=com;
	delay(5);
	lcden=1;
	delay(5);
	lcden=0;
}
void write_date(uchar date)//êy?Y×óoˉêy
{
	rs=1;
	rw=0;
	P0=date;
	delay(5);
	lcden=1;
	delay(5);
	lcden=0;
}
void init()
{
	lcden=0;
	write_com(0x38);//??????,0x38???16x2??,5x7??,8?????
	write_com(0x0c);//???
	write_com(0x06);//?????,?????
	write_com(0x01);//??
	write_com(0x80);//???????
	i=0; j=0;
	a=0;     //μúò???2?ó?????μ?êy
	b=0;     //μú?t??2?ó?????μ?êy
	c=0;
	flag=0; //ê?·?óD·?o?°′?? 
	fuhao=0; //°′??????·?o?
}

void keyscan() // ?ü?ìé¨?è3ìDò
{
	P1=0xfe; 
	if(P1!=0xfe)
	{
	   delay(20);// ?ó3ù20ms
	   if(P1!=0xfe) {  temp=P1&0xf0;
	    switch(temp)
	    {
	     case 0xe0:num=0;   break;	//7
	     case 0xd0:num=1;   break; 	//8
	     case 0xb0:num=2;   break;	//9
	     case 0x70:num=3;   break;	//??
	    }
	   }   while(P1!=0xfe);
	   if(num==0||num==1||num==2)//è?1?°′??μ?ê?'7','8'?ò'9
	   { 
	       if(j!=0){write_com(0x01); j=0;  }
	       if(flag==0)//??óD°′1y????·??ü
		    { a=a*10+table[num];    } //°′??êy×?′?′¢μ?a
		    else//è?1?°′1y????·??ü
		    {    b=b*10+table[num];   }//°′??êy×?′?′¢μ?b
	   }
	   else//è?1?°′??μ?ê?'/'  	  3y·¨
	   {
	    flag=1;	 //°′??????·?
	    fuhao=4;//4±íê?3yo?ò?°′
	   }
	   i=table1[num];     //êy?Y??ê?×?×?±?
	   write_date(0x30+i);//??ê?êy?Y?ò2ù×÷·?o?
	}

	P1=0xfd;
	if(P1!=0xfd)
	{
	   delay(20);
	   if(P1!=0xfd){  temp=P1&0xf0;
	    switch(temp)
	    {
	     case 0xe0:num=4; break; //4
	     case 0xd0:num=5; break; //5
	     case 0xb0:num=6; break; //6
	     case 0x70:num=7; break; //?á
	    }
	   }   while(P1!=0xfd);//μè′y°′?üêí·?
	   if(num==4||num==5||num==6&&num!=7)//è?1?°′??μ?ê?'4','5'?ò'6'
	   { 
	    if(j!=0){ write_com(0x01);  j=0;   }
	       if(flag==0)//??óD°′1y????·??ü
	    { a=a*10+table[num];     }
	    else//è?1?°′1y????·??ü
	    { b=b*10+table[num];   }
	   }
	   else//è?1?°′??μ?ê?'?á'
	   {  flag=1;
	      fuhao=3;//3±íê?3?o?ò?°′
	   }
	   i=table1[num];      //êy?Y??ê?×?×?±?
	   write_date(0x30+i);//??ê?êy?Y?ò2ù×÷·?o?
	}

	P1=0xfb;
	if(P1!=0xfb){  delay(20);
	   if(P1!=0xfb) { temp=P1&0xf0;
	    switch(temp)
	    {
	     case 0xe0:num=8;    break;	 //1
	     case 0xd0:num=9;    break;	 //2
	     case 0xb0:num=10;   break;	 //3
	     case 0x70:num=11;   break;	 //-
	    }
	   }   while(P1!=0xfb);
	   if(num==8||num==9||num==10)//è?1?°′??μ?ê?'1','2'?ò'3'
	   { 
	    if(j!=0){ write_com(0x01);  j=0;  }
	      if(flag==0)//??óD°′1y????·??ü
	    { a=a*10+table[num];   }
	    else//è?1?°′1y????·??ü
	    { b=b*10+table[num]; }
	   }
	   else if(num==11)//è?1?°′??μ?ê?'-'
	   {
	    flag=1;
	    fuhao=2;//2±íê???o?ò?°′
	   }
	   i=table1[num];  //êy?Y??ê?×?×?±?
	   write_date(0x30+i);//??ê?êy?Y?ò2ù×÷·?o?
	}

	P1=0xf7;
	if(P1!=0xf7){   delay(20);
	   if(P1!=0xf7){ temp=P1&0xf0;
	    switch(temp)
	    {
	     case 0xe0:num=12; break;  //??0?ü    
	     case 0xd0:num=13; break;  //êy×?0    
	     case 0xb0:num=14; break;  //μèóú?ü    
	     case 0x70:num=15; break;  //?ó
	    }								   
		} while(P1!=0xf7);

	   switch(num)
	   {
	    case 12:{write_com(0x01);a=0;b=0;flag=0;fuhao=0;}//°′??μ?ê?"??á?"
	    		break;
	    case 13:{                //°′??μ?ê?"0"
	       if(flag==0)//??óD°′1y????·??ü
	       { a=a*10;  write_date(0x30);     P1=0;       }
	       else if(flag>=1)//è?1?°′1y????·??ü
	       {  b=b*10;   write_date(0x30);       }
	      }     break;
	    case 14:{j=1; //°′??μèóú?ü£??ù?Y????·?o???DD2?í?μ???ê?′|àí
	           if(fuhao==1)	   //?ó·¨????
			   {
			   		write_com(0x80+0x4f);//??DD
	           		write_com(0x04);//′óoóíù?°D′êy?Y	
	           		c=a+b;
	           		while(c!=0){write_date(0x30+c);	} 
	           		write_date(0x3d);     //?ùD′"="
	           		a=0;b=0;flag=0;fuhao=0;
	           }
	     	 else if(fuhao==2)	 //??·¨????
		  	   {
								write_com(0x80+0x4f);
	            	write_com(0x04);     
	           		if(a-b>0)         	c=a-b;
	           		else              	c=b-a;
	           		while(c!=0)	{ write_date(0x30+c); } 
	           		if(a-b<0) 	 write_date(0x2d);
	          		 write_date(0x3d);     //?ùD′"="           
	          		 a=0;b=0;flag=0;fuhao=0;
	           }
	      	 else if(fuhao==3)	//3?·¨???? 
			 	{write_com(0x80+0x4f);    	write_com(0x04);
	             	c=a*b;
	             	while(c!=0)	{write_date(0x30+c);}
	             	write_date(0x3d);  	 a=0;b=0;flag=0;fuhao=0;
	             }
	      	  else if(fuhao==4)//3y·¨????	
			  		{
									write_com(0x80+0x4f);
	            		write_com(0x04);
	            		i=0;
					  	if(b!=0)
						{
							c=(long)(((float)a/b)*1000);
		            		while(c!=0)
		             		{ 
		              			 write_date(0x30+c%10);
												i++; 
											if(i==3)
												write_date(0x2e);
		               			 c=c/10;
		               		 }   
											if(i==1)
											{write_date(0x30);write_date(0x30);write_date(0x2e);write_date(0x30);}
											if(i==2)
											{write_date(0x30);write_date(0x2e);write_date(0x30);}
											if(i==3)
											{	write_date(0x30);}										
		           			 a=0;b=0;flag=0;fuhao=0;
						}
						else
						{
							write_date('!');write_date('R');write_date('O');
							write_date('R');write_date('R');write_date('E');
						}
	                 }
	      }  break;
	     case 15:{write_date(0x30+table1[num]);flag=1;fuhao=1;}     break;	//?ó?ü	 éè???ó±ê??fuhao=1;
	   }
	}//P1!=0xf7
}

void main()
{
	init();		//?μí33?ê??ˉ
	while(1)
	{
	   keyscan();//?üé¨?è
	}
}
矩阵按键,LCD显示
昨天在组长帮助下完成了计算器的数值简单显示,组长还帮我分析了程序存在的问题

今天在实验室写程序的时候,在计算器运算的除法方面有问题,很困惑,所以找了组长帮忙看看程序,他告诉我,我的除法程序1,只考虑了特殊情况;2,结果被重复显示。

在组长的帮助下,我修改好了程序,经测试后,程序可以正常运行。

收获:

1,对C语言关键字及其用途有了更深的理解与把握。

2,写程序不能急于求成,要一步一步地改。

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