基于HD7279和51單片機(jī)的計(jì)算器
/////////////////////////////****************程序*****************///////////////////////
#includereg51.h>#includemath.h>sbit cs=P2^6;sbit clk=P1^0;sbit dat=P1^1; sbit key=P3^3; bit add,sub,mul,div,equ,clr;unsigned char times,st,l,data_jp,flag,d;unsigned int tmr;unsigned long num[8],sn[8],disp[8],sh,result,hp=100000000;void send(unsigned char);unsigned char receive(void);unsigned long real(unsigned long ,unsigned char);unsigned long calcu(unsigned long, unsigned long);void display(unsigned long);unsigned long write_key(unsigned char);void char_flk(void);void test(void);void error(void);void long_delay(void);void short_delay(void);void delay10ms(unsigned char); /////////**************主函數(shù)****************//////////void main (){times=0;l=times;st=0;flag=0;test();while(1){unsigned char i;if(!key){ send(0x15);data_jp=receive();cs=1;d=write_key(data_jp);if (data_jp=9){flag=0;num[times]=d;}else{flag=1;//times-=1;}if(flag!=1){ if(num[0]==0){times=0;continue;}if(7>times>=1){send(0xa1);}send(0x80);send(num[times]);while(!key); delay10ms(1);times=times+1;cs=1;if(times>7){send(0xa4);error() ;times=0;send(0xa4); }}else{send(0xa4);char_flk();times-=1;l=times;for(i=0;i=times;i++) {num[i]=real(num[i],l);sh=sh+num[i];l--; }sn[st]=sh;if(st>=1){result=calcu(sn[st],sn[st-1]);sn[st-1]=result;st--; }st++;times=0;flag=0;sh=0;if(equ==1){ st=0; send(0xa4);display(result);equ=0;}if(clr==1){st=0;times=0;flag=0;sh=0;add=0;sub=0;mul=0;div=0;equ=0;clr=0;send(0xa4);}while(!key);}}}}/////////*********************測(cè)試子函數(shù)****************////////void test(void){ char i;delay10ms(2);send(0xbf);cs=1; for (i=0;i=3;i++) {delay10ms(100);}send(0xa4);cs=1;delay10ms(20);}
///////***********************讀鍵盤程序**************///////unsigned long write_key(unsigned char data_jp){unsigned char shuzi;if(data_jp10){shuzi=data_jp;}else{if(data_jp==10){add=1;}else if(data_jp==11){sub=1;}else if(data_jp==12){mul=1;}else if(data_jp==13){div=1;}else if(data_jp==14){equ=1;}else if(data_jp==15){clr=1;}shuzi=0xff;}return(shuzi) ;}///////***********************發(fā)送子函數(shù)*************//////void send(unsigned char data_out) {unsigned char i;cs=0;long_delay();for(i=0;i=7;i++){if(data_out0x80){dat=1;}else{dat=0;}clk=1;short_delay();clk=0;short_delay();data_out=data_out1;}dat=0;}/////************************接收鍵盤子函數(shù)************//////unsigned char receive(void){unsigned char i,data_in;dat=1;long_delay();for(i=0;i=7;i++){clk=1;short_delay();data_in=data_in1;if(dat){data_in=data_in|0x01;}else{data_in=data_in|0x00;}clk=0;short_delay();}dat=0;return(data_in);}/////////***************************顯示數(shù)字處理子函數(shù)***********/////////unsigned long real(unsigned long so, unsigned char s){unsigned char z;if(s!=0){for(z=0;z=(s-1);z++){if(s!=0){so=so*10;}else{so=so;}}}else{so=so;}return(so);}//////////*************************計(jì)算子函數(shù)*******************///////////unsigned long calcu(unsigned long sn0,unsigned long sn1 ) {if(add==1){sn0=sn0+sn1;add=0;}if(sub==1){sn0=sn1-sn0;sub=0;}if(mul==1){sn0=sn0*sn1;mul=0;}if(div==1){if(sn0>0)sn0=sn1/sn0;else error();div=0;}return(sn0);}////////****************************報(bào)錯(cuò)錯(cuò)誤子函數(shù)***************/////////void error(void){while(key){send(0x96);send(0x4f);delay10ms(2);send(0x95);send(0x05);delay10ms(2);send(0x94);send(0x05);delay10ms(2);send(0x93);send(0x1d);delay10ms(2);send(0x92);send(0x05);delay10ms(2); }}///////////********************顯示子函數(shù)*********////////////void display(unsigned long display){unsigned char i,asus=8;if(display>99999999)error(); else{ for(i=0;i=7;i++){if((display/hp)>=1)break;else{hp=hp/10;asus-=1;}}for(i=0;i=asus;i++){disp[i]=display/hp;if(hp>1){display=display%hp;hp=hp/10;}}for(i=0;i=asus;i++){if(i>0){ send(0xa1); }send(0x80);send(disp[i]);}}}///////////********************符號(hào)判斷閃爍顯示*********////////////void char_flk(void){if(add==1){send(0x97);send(0x77);}else if(sub==1){send(0x97);send(0x01);}else if(mul==1){send(0x97);send(0x37);}else if(div==1){send(0x97);send(0x49);}else if(clr){;}send(0x88);send(0x7f);}//////************************延時(shí)子函數(shù)*************///////void long_delay(void){char i;for(i=0;i=25;i++) ;}void short_delay(void){char j;for(j=0;j=4;j++);}void delay10ms(unsigned char time){unsigned char i;unsigned int j;for (i=0;i=time;i++){for(j=0;j=0x390;j++);}}
///////////////////////////************效果圖*************////////////////////
以上演示的為9876543÷234,其本來(lái)的結(jié)果是42207.4487179……無(wú)法顯示小數(shù)位,所以結(jié)果為42207 。
評(píng)論