51单片机实现三位十进制数加减乘除运算
生活随笔
收集整理的這篇文章主要介紹了
51单片机实现三位十进制数加减乘除运算
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
51單片機實現(xiàn)三位十進制數(shù)加減乘除運算
一.題目
51單片機IO接口作業(yè)
請將附件給出的Proteus圖用51單片機完成一個計算器功能。
1、顯示采用動態(tài)分時8位共陽數(shù)碼管輸出。
2、采用4*4矩陣鍵盤輸入,鍵盤上已經(jīng)標識對應鍵。
3、完成三位十進制數(shù)的加、減、乘、除運算。
4、開機顯示最右邊數(shù)碼管顯示‘0’,輸入按鍵值后依次左移。"+—*/"數(shù)碼管顯示分別“A B C D”。
按要求上交實驗報告(加、減、乘、除的對應的Proteus仿真截圖一定要有,下載到開發(fā)板圖也必須有)。
二.實驗思路
程序分為以下幾步:
三.程序代碼
#include<reg52.h>//共陽極數(shù)碼管 unsigned char code segCodeTable[16] = {0xc0,0xf9,0xa4,0xb0, 0x99,0x92,0x82,0xf8, 0x80,0x90,0x88,0x83, 0xc6,0xa1,0x86,0x8e}; //位碼表 unsigned char code posCodeTable[8] = { 0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01}; unsigned char code key_value[16]={0xD7,0xEB,0xDB,0xBB,0xED,0xDD,0xBD,0xEE,0xDE,0xBE,0x77,0x7B,0x7D,0x7E, 0xB7,0xE7};//鍵盤標識對應的鍵盤值 void dis_seg7(unsigned long num,unsigned char dec_hex); unsigned char keyscan(); void delayms (void); unsigned char pre_P1;void main() {unsigned char key0=0,key1=0,key2=0;unsigned char value = 0,j = 0;unsigned char state = 0;unsigned char dec_hex = 10;unsigned int NUMA = 0,NUMB = 0;unsigned long dis_num = 0;while(1){value = keyscan();//if((pre_P1 == 0xf0)&&(P1 != 0xf0))//實現(xiàn)數(shù)碼管顯示多位數(shù)字if((pre_P1 == 0xf0)&&(P1 != 0xf0)){if(value >= 0 && value <= 9){dec_hex = 10;key2 = key1;key1 = key0;key0 = value;dis_num = 100*key2 + 10* key1 + key0;}//按下操作符+-*/前,保存數(shù)碼管顯示數(shù)字dis_num到NUMA// key0 = 0;key1 = 0;//key2 = 0;dis_num = value;//上述四條語句的目的:按下操作符顯示對應的16進制數(shù);//實現(xiàn)數(shù)碼管的清零,即實現(xiàn)120+150,數(shù)碼管一次顯示120;//A(表示加號);150else if(value>=10&&value<=13){NUMA = dis_num;key0 = 0;key1 = 0;key2 = 0;dis_num = value;dec_hex = 16;if(value == 10 ){state ='+'; }else if(value == 11){state = '-';}else if (value == 12){state = '*';}else if (value == 13){state = '/';}}//按下等號時,保存數(shù)碼管顯示數(shù)字dis_num到NUMB,實現(xiàn)計算else if(value == 14){dec_hex = 10;key0 = 0;key1 = 0;key2 = 0;NUMB = dis_num;if(state == '+'){dis_num = NUMA + NUMB;}else if(state == '-'){dis_num = NUMA - NUMB;}else if(state == '*'){dis_num = NUMA * NUMB;}else if(state == '/'){dis_num = NUMA / NUMB;}} //數(shù)碼管清零else if(value == 15){dec_hex = 10;key0 = 0;key1 = 0;key2 = 0;NUMA = 0;NUMB = 0;dis_num = 0; }}pre_P1 = P1;dis_seg7(dis_num,dec_hex);for(j = 0;j < 200;j++);} }//七段數(shù)碼管顯示模塊 void dis_seg7(unsigned long num,unsigned char dec_hex) {char i = 0,count = 8;unsigned int j =0;char dis_data = 0;for(i = 0;i < count;i++){dis_data = num % dec_hex;num = num / dec_hex;P2 = segCodeTable[dis_data];P3 = posCodeTable[i];for(j = 0;j < 200;j++);P2 = 0xff; //清除余暉if(num == 0){break;}} }//延時模塊 void delayms (void) { unsigned char i; for (i=200;i>0;i--); }//按鍵掃描模塊 unsigned char keyscan() {unsigned char rowscan,colscan,row_col,i;static unsigned char value;P1 = 0xf0; //P1輸出 0xf0if((P1&0xf0) != 0xf0) //P1讀入數(shù)據(jù),判斷是否有按鍵按下{delayms();//延時消抖if((P1&0xf0) != 0xf0){//本代碼采用逐行掃描的方式來確定按下的按鍵rowscan = 0xfe;while((rowscan&0x10)!=0){P1 = rowscan;colscan = P1 & 0xf0;if(colscan != 0xf0){//確定按下按鍵的行號與列號,查表,返回按鍵值row_col = colscan | (rowscan&0x0f);for(i = 0;i<16;i++){if(row_col == key_value[i]){value = i;}}break;}else {rowscan = (rowscan << 1) | 0x1;}}}}return value; }四.運行結果
1.加法
123 + 456 = 579
2.減法
789 - 123 = 666
3.乘法
100 * 234 = 23400
4.除法
480/160 =3
github下載鏈接
代碼及proteus工程下載
總結
以上是生活随笔為你收集整理的51单片机实现三位十进制数加减乘除运算的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 原生js读取json文件
- 下一篇: php代码导入sql,php导入SQL文