程序放在了壓縮包里,我是用c語言寫的,要求是要使用8255,但我可能還是不太會(huì)用,我把代碼貼在下面了,求大佬相助~
- #include <stdio.h>
- #include <reg51.h>
- #include <intrins.h>
- #include <math.h>
- #include <ctype.h>
- #include <absacc.h>
- #define PA PBYTE[0X00]
- #define PB PBYTE[0X20]
- #define Cont PBYTE[0X60]
- #define E 1000000
- #define uchar unsigned char
- #define uint unsigned int
- #define Lint unsigned long int
- uchar operand1[7], operand2[7]; /* 操作數(shù) */
- uchar operator; /* 操作符 */
-
- Lint number(uchar* );//將字符串化為整數(shù)
- uchar keyscan(); //鍵盤掃描
- void buf(Lint value); //更新顯示緩存
- void disp(void);//顯示
- void delay(uint);//延時(shí)
- Lint compute(Lint va1,Lint va2,uchar opera);//運(yùn)算
- uchar code table[] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71, 0x00};
- uchar dbuf[6] = {16,16,16,16,16,16}; /* 顯示緩存 */
- /* 延時(shí)函數(shù) */
- void delay(uint z)
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=110;y>0;y--);
- }
- /*******************************************
- 鍵盤掃描程序
- 將按鍵轉(zhuǎn)化為字符并作為輸出
- ','#'分別表示清零鍵和沒有鍵按下
- *******************************************/
-
- uchar keyscan()
- {
- uchar key;/* 按鍵值標(biāo)記變量 */
-
- P1=0xfe; //掃描鍵盤第1行
- while((P1 & 0xf0) != 0xf0) /* 有按鍵按下 */
- {
- delay(3); /* 去抖動(dòng)延時(shí) */
- while((P1 & 0xf0) != 0xf0) /* 再次確認(rèn) */
- {
- switch(P1)
- {
- case 0xee: key='7';break;
- case 0xde: key='8';break;
- case 0xbe: key='9';break;
- case 0x7e: key='/';break;
- default: key='#';
- }
- while((P1&0xf0)!=0xf0);
- }
- }
-
- P1=0xfd; //掃描鍵盤第2行
- while((P1 & 0xf0) != 0xf0) /* 有按鍵按下 */
- {
- delay(3); /* 去抖動(dòng)延時(shí) */
- while((P1 & 0xf0) != 0xf0) /* 再次確認(rèn) */
- {
- switch(P1)
- {
- case 0xed: key='4';break;
- case 0xdd: key='5';break;
- case 0xbd: key='6';break;
- case 0x7d: key='*';break;
- default: key='#';
- }
- while((P1&0xf0)!=0xf0);
- }
- }
-
- P1=0xfb; //掃描鍵盤第3行
- while((P1 & 0xf0) != 0xf0) /* 有按鍵按下 */
- {
- delay(3); /* 去抖動(dòng)延時(shí) */
- while((P1 & 0xf0) != 0xf0) /* 再次確認(rèn) */
- {
- switch(P1)
- {
- case 0xeb: key='1';break;
- case 0xdb: key='2';break;
- case 0xbb: key='3';break;
- case 0x7b: key='-';break;
- default: key='#';
- }
- while((P1&0xf0)!=0xf0);
- }
- }
-
- P1=0xf7; //掃描鍵盤第4行
- while((P1 & 0xf0) != 0xf0) /* 有按鍵按下 */
- {
- delay(3); /* 去抖動(dòng)延時(shí) */
- while((P1 & 0xf0) != 0xf0) /* 再次確認(rèn) */
- {
- switch(P1)
- {
- case 0xe7: key=';break;
- case 0xd7: key='0';break;
- case 0xb7: key='=';break;
- case 0x77: key='+';break;
- default: key='#';
- }
- while((P1&0xf0)!=0xf0);
- }
- }
-
- return key;
-
- }
- void main()
- {
-
- Lint value1, value2, result; /* 數(shù)值1,數(shù)值2,結(jié)果 */
- uchar ckey, cut1 = 0, cut2 = 0; /* ckey鍵盤輸入字符 */
- uchar operator; /* 運(yùn)算符 */
- uchar i, bool = 0, t=0 ;
- Cont=0x81;
- P1=0xff;
- init:
- buf(0);//初始化
- disp();
- value1=value2=0 ;
- result = 0;
- cut1 = cut2 = 0;
- bool = 0; /*操作數(shù)標(biāo)識(shí)符*/
-
- t = 0 ; /*連續(xù)運(yùn)算*/
-
- for(i = 0;i < 7;i++)
- {
- operand1[i] = '\0';
- operand2[i] = '\0';
- } /* 初始化 */
-
- while(1)
- {
- ckey=keyscan();//讀鍵盤值
- if(ckey!='#')
- {
- if(isdigit(ckey))/* isdigit函數(shù),字符是阿拉伯?dāng)?shù)字返回非0值,否則返回0 */
- {
- switch(bool)
- {
- case 0:
- if(cut1>5)
- {
- value1=E;buf(value1);disp();break;
- }
- operand1[cut1] = ckey;
- operand1[cut1+1] = '\0';
- value1 = number(operand1); /* 將字符串轉(zhuǎn)化為整數(shù) */
- cut1++;
- buf(value1);
- disp();
- break;
- case 1:
- if(cut2 > 5)
- {
- value2 = E ; buf(value2); disp() ; break ;
- }
- t = 1 ;
- operand2[cut2] = ckey;
- operand2[cut2+1] = '\0';
- value2 = number(operand2);
- cut2++;
- buf(value2);
- disp();
- break;
- default: break;
- }
- }
- else if(ckey=='+'||ckey=='-'||ckey=='*'||ckey=='/')
- {
- bool=1;
- if(t==1) //判斷是否為連續(xù)運(yùn)算
- {
- value1=compute(value1,value2,operator); /* 顯示上一步運(yùn)算結(jié)果 */
- cut2 = 0 ;
- }
- buf(value1);
- disp();
- operator = ckey;
- //s = 0 ;
- }
- else if(ckey=='=') //計(jì)算結(jié)果
- {
- result= compute(value1,value2,operator);
- buf(result);
- disp();
- while(1)
- {
- ckey=keyscan();
- if(ckey==') //若按下清零鍵跳回開始
- goto init;
- else{
- buf(result);
- disp();
- }
- }
- }
- else if(ckey==') //若按下清零鍵跳回開始
- {
- goto init;
- }
- }
- disp();
- }
- }
- /******************************************
- number函數(shù)
- 將字符串轉(zhuǎn)化為整數(shù)
- *******************************************/
- Lint number(uchar operand[] )
- {
- Lint n=0, i, j ;
- for(i=0 ; i<6 ; i++ )
- {
- if(operand[i]=='\0'){ break ; }
- n=n*10 ;
- j=operand[i]-'0' ;
- n=n+j ;
- }
- return n ;
- }
- /******************************************
- 運(yùn)算函數(shù)
- 輸入:操作數(shù)和操作符
- 輸出:計(jì)算結(jié)果
- *******************************************/
- Lint compute(Lint va1,Lint va2,uchar opera)
- {
- Lint result;
- switch(opera)
- {
- case '+':
- result=va1+va2;
- break;
- case '-':
- if(va1<va2){
- result=E;
- break;
- }
- else{
- result=va1-va2;
- break;
- }
- case '*':
- result=va1*va2;
- break;
- case '/':
- if(va2==0){
- result=E;
- break;
- }
- else{
- result=va1/va2;
- break;
- }
- default:
- result=E;
- break;
- }
- return result;
- }
- /*******************************************
- 更新顯示緩存
- 輸入:無符號(hào)整數(shù)
- 輸出:將輸入送入顯示緩存,溢出或出錯(cuò)顯示E
- *******************************************/
- void buf(Lint value)
- {
- uchar i;
- if(value==0)
- {
- dbuf[5]=0;
- i=4;
- }
- else if(value==E||value>99999)
- {
- dbuf[5]=15;///////////////////////////////////////
- i=4;
- }
- else{
- for(i=5;value>0;i--){
- dbuf[i]=value%10;
- value/=10;
- }
- }
- for(i=i+1;i>0;i--){
- dbuf[i-1]=16;
- }
- }
- /*******************************************
- 顯示函數(shù)
- *******************************************/
- void disp(void)
- {
- uchar i,j;
- i=0xfe;
- for(j=0;j<6;j++)
- {
- PB=i;
- PA=table[dbuf[j]];
- i=_crol_(i,1);
- delay(3);
- PA=0xff;
- }
- }
復(fù)制代碼 |