mega16单片机科学计算器程序源代码
2020-02-03 来源:elecfans
支持最大八位的无符号长整型数值计算
#include《iom16v.h》
#include《macros.h》
//#include《stdio.h》
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
#define EA SREG|=BIT(7)
uchar temp,old,key_turn,flag=0;
ulong datA=0,datB=0;
const uchar wela[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
const uchar tab[11]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x00};
extern uchar dat[8]={0,0,0,0,0,0,0,0};
extern uchar dat_right[]={0x0a,0x0a,0x0a,0x0a,0x0a,0x0a,0x0a,0x0a};
void delay(uint z)
{
while(z--);
}
void display(uchar z)
{
uchar i;
while(z--)
for(i=0;i《8;i++)
{
PORTB=wela[i];
PORTD=tab[dat_right[i]];
delay(30);
PORTB=0XFF;
}
}
void clean_junk()
{
uchar i,p=0;
for(i=0;i《8;i++)
{
dat_right[i]=0x0a;
}
for(i=0;i《8;i++)
{
if(dat[i]》0)p=1;
if(p)dat_right[i]=dat[i];
}
}
void delete()
{
uchar i;
for(i=0;i《8;i++)
{
dat[i]=0;
}
clean_junk();
}
void disan_he()
{
ulong i;
i=dat[0]*1e7+dat[1]*1e6+dat[2]*1e5+dat[3]*1e4+dat[4]*1e3+dat[5]*1e2+dat[6]*1e1+dat[7];
if(flag)datB=i;
else datA=i;
flag=1;
delete();
}
void disan_fen()
{
dat[0]=datA/1e7;
dat[1]=datA%10000000/1000000;
dat[2]=datA%1000000/100000;
dat[3]=datA%100000/10000;
dat[4]=datA%10000/1000;
dat[5]=datA%1000/100;
dat[6]=datA%100/10;
dat[7]=datA%10;
flag=0;
}
void move()
{
uchar i;
if(temp》0&temp《0x0b)
{
for(i=0;i《8;i++)
{
dat[i]=dat[i+1];
}
if(temp==0x0a){dat[0]=0;temp=0;}
dat[7]=temp;temp=0;
}
}
void clean()
{
uchar i;
flag=0;
for(i=0;i《8;i++)
{
dat[i]=0;
}
temp=0;
old=0;
datA=0;
datB=0;
}
void disan()
{
if(temp》0&temp《0x11)switch(temp)
{
case 0x0e:clean();break;
case 0x0b:old=temp;temp=0;disan_he();break;
case 0x0c:old=temp;temp=0;disan_he();break;
case 0x0d:old=temp;temp=0;disan_he();break;
case 0x10:old=temp;temp=0;disan_he();break;
case 0x0f:switch(old)
{
case 0x0b:if(flag)disan_he();datA=datA/datB;old=0;temp=0;disan_fen();break;
case 0x0c:if(flag)disan_he();datA=datA*datB;old=0;temp=0;disan_fen();break;
case 0x0d:if(flag)disan_he();datA=datA-datB;old=0;temp=0;disan_fen();break;
case 0x10:if(flag)disan_he();datA=datA+datB;old=0;temp=0;disan_fen();break;
default:temp=0;old=0;break;
}break;
default:move();temp=0;break;
}
// disan_fen();delete();
}
void diskey()
{
uchar i,n;
const uchar code[]={0xFE,0xFD,0xFB,0xF7};
if(temp==0x00)for(i=0;i《4;i++)
{
PORTA=code[i];
temp=PINA;
if(~temp&0xf0)i=6;
while(~PINA&0xf0)display(1);
PORTA=0xff;
}
if(temp》0x10)switch(temp)
{
case 0xEE:temp=0x07;break;
case 0xDE:temp=0x08;break;
case 0xBE:temp=0x09;break;
case 0x7E:temp=0x0b;break;
/////////////////////////
case 0xED:temp=0x04;break;
case 0xDD:temp=0x05;break;
case 0xBD:temp=0x06;break;
case 0x7D:temp=0x0c;break;
//////////////////////////
case 0xEB:temp=0x01;break;
case 0xDB:temp=0x02;break;
case 0xBB:temp=0x03;break;
case 0x7B:temp=0x0d;break;
////////////////////////
case 0xE7:temp=0x0e;break;
case 0xD7:temp=0x0a;break;
case 0xB7:temp=0x0f;break;
case 0x77:temp=0x10;break;
/////////////////////////
default:temp=0x00;break;
}
disan();
}
void main()
{
MCUCSR|= 1《《JTD; //禁用jtag功能
MCUCSR|= 1《《JTD; //再次禁用jtag功能生效
DDRC=0XFF;
DDRB=0XFF;
DDRD=0XFF;
DDRA=0X0F;
while(1)
{
display(1);
diskey();
clean_junk();
}
}