基于51单片机计算器设计

目录

一、项目功能

二、仿真

三、程序

资料下载地址:基于51单品阿吉计算器

一、项目功能

1、利用键盘及LCD1602作为计算器的输入及显示模块;

2、能进行加、减、乘、除的基本运算;按键有0~9,“+-*/=”。

3、有清零“CR"按键;

4、有记忆功能键,按下该键,可以直接显示并运用存储数据进行计算。

二、仿真

基于51单片机计算器设计_第1张图片

基于51单片机计算器设计_第2张图片 

三、程序

#include
#define uint unsigned int
#define uchar unsigned char

sbit e=P2^3;
sbit rs=P2^4;
sbit rw=P2^0;
sbit busy=P0^7;

char i,j,temp,num,num_1;
long a,b,c;     
float a_c,b_c;
uchar flag,fuhao;

uchar code table[]={7,8,9,0,4,5,6,0,1,2,3,0,0,0,0,0};
uchar code table1[]={7,8,9,0x2f-0x30,4,5,6,0x2a-0x30,1,2,3,0x2d-0x30,0x01-0x30,0,0x3d-0x30,0x2b-0x30};

void delay(uchar z)
{
	uchar y;
	for(z;z>0;z--)
		for(y=0;y<110;y++);
}

void check()
{
	do{
		P0=0xFF;
		rs=0;     
		rw=1;    
    	e=0;     
    	delay(1); 
    	e=1;     
      }while(busy==1); 
}

void write_com(uchar com)
{
	P0=com; 
	rs=0;
	rw=0;
	e=0;
	check();
	e=1;
}

void write_date(uchar date)
{
	P0=date;
	rs=1;
	rw=0;
	e=0;
	check();
	e=1;
}

void init() 
{
	num=-1;
	e=1; 
	write_com(0X01);
	delay(1);
	write_com(0x38); 
	delay(1);
	write_com(0x0c); 
	delay(1);
	write_com(0x06); 
	delay(1);
	write_com(0x80); 
	delay(1);
	write_com(0x01); 
	delay(1);
	num_1=0;
	i=0;
	j=0;
	a=0;     
	b=0;    
	c=0;
	flag=0; 
	fuhao=0; 
}

void keyscan()
{
	P3=0xfe;
	if(P3!=0xfe)
	{
		delay(20);          
		if(P3!=0xfe)
		{
			temp=P3&0xf0;
			switch(temp)
			{
				case 0xe0:num=0;
					break;
				case 0xd0:num=1;
					break;
				case 0xb0:num=2;
					break;
				case 0x70:num=3;
					break;
			}

 		}

		while(P3!=0xfe);
		if(num==0||num==1||num==2)
		{
			if(j!=0)
			{
				write_com(0x01);
				j=0;
			}
			if(flag==0)
			{
				a=a*10+table[num];
			}
			else
			{
				b=b*10+table[num];
			}
		}
		else
		{
			flag=1;
			fuhao=4;
		}
		i=table1[num];
		write_date(0x30+i);
	}
	P3=0xfd;
	if(P3!=0xfd)
	{
		delay(5);
		if(P3!=0xfd)
		{
			temp=P3&0xf0;
			switch(temp)
			{
				case 0xe0:num=4;
					break;
				case 0xd0:num=5;
					break;
				case 0xb0:num=6;
					break;
				case 0x70:num=7;
					break;
			}
		}
		while(P3!=0xfd);
		if(num==4||num==5||num==6&&num!=7)
		{
			if(j!=0)
			{
				write_com(0x01);
				j=0;
			}
			if(flag==0)
			{
				a=a*10+table[num];
			}
			else
			{
				b=b*10+table[num];
			}
		}
		else
		{
			flag=1;
			fuhao=3;
		}
		i=table1[num];
		write_date(0x30+i);
	}
	P3=0xfb;
	if(P3!=0xfb)
	{
		delay(5);
		if(P3!=0xfb)
		{
			temp=P3&0xf0;
			switch(temp)
			{

     case 0xe0:num=8;

         break;

      

     case 0xd0:num=9;

         break;

      

     case 0xb0:num=10;

         break;

      

     case 0x70:num=11;

         break;

    }

   }

   while(P3!=0xfb);

   if(num==8||num==9||num==10)//??????'1','2'?'3'

   {

    if(j!=0)

        {

          write_com(0x01);

          j=0;

         }

      if(flag==0)//???????

    {

     a=a*10+table[num];

    }

    else//???????

    {

     b=b*10+table[num];

    }

   }

   else if(num==11)//??????'-'

   {

    flag=1;

    fuhao=2;//2??????

   }

   i=table1[num];

   write_date(0x30+i);

}

P3=0xf7;

if(P3!=0xf7)

{

   delay(5);

   if(P3!=0xf7)

   {

    temp=P3&0xf0;

    switch(temp)

    {

     case 0xe0:num=12;

         break;

      

     case 0xd0:num=13;

         break;

      

     case 0xb0:num=14;

         break;

      

     case 0x70:num=15;

         break;

    }

   }

   while(P3!=0xf7);

   switch(num)

   {

    case 12:{write_com(0x01);a=0;b=0;flag=0;fuhao=0;}//????"??"

     break;

    case 13:{                //????"0"

       if(flag==0)//???????

       {

        a=a*10;

        write_date(0x30);

        P1=0;

       }

       else if(flag==1)//???????

       {

        b=b*10;

        write_date(0x30);

        }

      }

     break;

    case 14:{j=1;

           if(fuhao==1){write_com(0x80+0x4f);//?????,???????????????

           write_com(0x04);     //?????????,???????,??????

           c=a+b;

           while(c!=0)

           {

             write_date(0x30+c%10);

             c=c/10;

           }

           write_date(0x3d);     //??"="

           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%10);

             c=c/10;

           }

           if(a-b<0)

             write_date(0x2d);

           write_date(0x3d);     //??"="           

           a=0;b=0;flag=0;fuhao=0;

          }

      else if(fuhao==3){write_com(0x80+0x4f);

            write_com(0x04);

             c=a*b;

             while(c!=0)

             {

             write_date(0x30+c%10);

             c=c/10;

             }

             write_date(0x3d);     

          a=0;b=0;flag=0;fuhao=0;

             }

      else if(fuhao==4){write_com(0x80+0x4f);

            write_com(0x04);

            i=0;

            c=(long)(((float)a/b)*1000);

            while(c!=0)

             {

               write_date(0x30+c%10);

               c=c/10;

           i++;

           if(i==3)

               write_date(0x2e);

             }

            if(a/b<=0)

              write_date(0x30);

            write_date(0x3d);                              

            a=0;b=0;flag=0;fuhao=0;

                    }

      }

     break;

     case 15:{write_date(0x30+table1[num]);flag=1;fuhao=1;}

     break;

   }

}

}

main()

{

	init();

	while(1)
 	{

  		 keyscan();

 	}
}

 

你可能感兴趣的:(proteus,c语言,单片机)