標題: 單片機4*4矩陣鍵盤簡易計算器Proteus仿真代碼 [打印本頁]

作者: 迷路者666    時間: 2022-4-8 10:04
標題: 單片機4*4矩陣鍵盤簡易計算器Proteus仿真代碼
在設計4*4矩陣鍵盤基礎上,完成兩個數(shù)的加、減、乘、除運算,結果通過數(shù)碼管或液晶顯示。

仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)


單片機源程序如下:
  1. #include"reg52.h"
  2. #include"math.h"

  3. #define uchar unsigned char
  4. #define uint unsigned int

  5. #define key P1
  6. #define duan P2
  7. #define wei P3

  8. sbit led=P0^0;
  9. sbit buzzer=P0^1;

  10. uchar num,temp,num1,i=0;
  11. long bczs=0,czs=0,jiguo=0,jiguo1=0;
  12. uchar czs12=1;          //輸入數(shù)的選擇標志位,1為第一個數(shù),2為第二個數(shù)
  13. uchar yiweiflag,qinflag;  //移位標志

  14. uchar code shuzi[12]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xbf};         //0~9,-,
  15. uchar code weizi[9]={0x00,0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
  16. uchar code error[10]={0xff,0xaf,0xc0,0xaf,0xaf,0x86,0xff,0xff,0xff};//error
  17. uchar playshuzu[9],fuhao;
  18. uchar temp1[8],mm[4],temp2[8];
  19. uchar zhi,weishu=0;

  20. void init();                  //函數(shù)申明
  21. uchar keyscan();
  22. uchar scanzhi();
  23. void display();
  24. void duzhichuli();
  25. void zhuandisplayjiguo();
  26. void jiafa();
  27. void jianfa();
  28. void chengfa();
  29. void chufa();
  30. void qin();
  31. void displayerror();
  32. //======================================
  33. void main()                          //主函數(shù)
  34. {
  35.         init();
  36.         display();
  37.         while(1)
  38.         {
  39.                 duzhichuli();
  40.         }
  41. }
  42. //===============================================================================
  43. void init()
  44. {
  45.         uchar i;
  46.         czs12=1;
  47.         yiweiflag=1;  //1不移
  48.         qinflag=0;    //不清除
  49.         duan=shuzi[8];
  50.         wei=weizi[5];
  51.         playshuzu[1]=shuzi[0];
  52.         for(i=2;i<9;i++)
  53.                 playshuzu[i]=shuzi[10];
  54.                
  55.         bczs=0;czs=0;jiguo=0,jiguo1=0;               
  56. }
  57. //===============================================================================
  58. void delay(uint x)
  59. {
  60.         uchar y;
  61.         for(x;x>0;x--)
  62.                 for(y=112;y>0;y--);
  63. }

  64. //====================================
  65. uchar keyscan()
  66. {
  67. unsigned char recode,i,j;
  68.         for(i=0;i<4;i++)
  69.                 {
  70.                         P1=~(0x01<<i);
  71.                         recode=P1>>4;
  72.                         if(recode<0x0f)
  73.                         {
  74.                         for(j=0;j<4;j++)
  75.                         {if(!(recode&0x01<<j))break;}
  76.                         return (i<<2)+(j+1); //返回鍵值 1-16
  77.                         }
  78.                         delay(5); //延時5ms
  79.                 }
  80.    return 0; //無按鍵返回
  81. }
  82. //========================================
  83. uchar scanzhi()
  84. {
  85.         uchar zhi,k;
  86.         k=keyscan();
  87.         switch(k)
  88.         {
  89.                 case 0: zhi=27;break;
  90.                 case 1: zhi=7;break;
  91.                 case 2: zhi=8;break;
  92.                 case 3: zhi=9;break;
  93.                 case 4: zhi=24;break;   // /

  94.                 case 5: zhi=4;break;
  95.                 case 6: zhi=5;break;
  96.                 case 7: zhi=6;break;
  97.                 case 8: zhi=23;break;   //*

  98.                 case 9: zhi=1;break;
  99.                 case 10: zhi=2;break;
  100.                 case 11: zhi=3;break;
  101.                 case 12: zhi=22;break;   //-

  102.                 case 13: zhi=26;break;        //nc
  103.                 case 14: zhi=0;break;
  104.                 case 15: zhi=25;break;  //=
  105.                 case 16: zhi=21;break;  //+
  106.         }
  107.         return(zhi);
  108. }
  109. //===========================================
  110. void duzhichuli()
  111. {
  112.         uchar tt,u;

  113.         tt=scanzhi();
  114.         if(tt!=27)
  115.         {
  116.                 led=0;
  117.                 buzzer=0;
  118.                 if(tt<=9)//數(shù)字輸入
  119.                 {
  120.                         if(qinflag==1)  qin();
  121.                                 qinflag=0;
  122.                         if(yiweiflag!=1)
  123.                         {
  124.                                 for(u=8;u>1;u--)                         //移位輸入
  125.                                 {
  126.                                         playshuzu[u]=playshuzu[u-1];
  127.                                 }
  128.                         }
  129.                         yiweiflag=0;    //移位
  130.                         playshuzu[1]=shuzi[tt];        
  131.                         if(czs12==1)   bczs=bczs*10+tt;                        //
  132.                         if(czs12==2)   czs=czs*10+tt;               
  133.                 }
  134.                 if(tt>20)//符號輸入
  135.                 {
  136.                         switch(tt)
  137.                         {
  138.                                 case 21:           jiafa();break;                //+
  139.                                 case 22:    jianfa();break;                //-
  140.                                 case 23:    chengfa();break;        //*
  141.                                 case 24:    chufa();break;
  142.                                 case 25:    zhuandisplayjiguo();break;                //=
  143.                                 case 26:    init();break;                //nc
  144.                         }               
  145.                 }
  146.         }
  147.   //-----------------------------------------------
  148.         while(tt!=27)           //松手檢測
  149.         {
  150.                 led=0;
  151.                 buzzer=0;
  152.                 tt=scanzhi();
  153.                 delay(1);
  154.                 if(tt==27) goto chu;
  155.         }

  156.         chu: delay(1);
  157.         led=1;
  158.         buzzer=1;
  159. }

  160. //============================================
  161. void display()
  162. {

  163.         TMOD=0x01;
  164.         TH0=(65536-200)/256;
  165.         TL0=(65536-200)%256;
  166.         EA=1;
  167.         ET0=1;
  168.         TR0=1;

  169. }
  170. void TIME0(void) interrupt 1
  171. {
  172.         TH0=(65536-200)/256;
  173.         TL0=(65536-200)%256;
  174.         i++;
  175.         if(i==9) i=1;
  176.             wei=weizi[i];
  177.             duan=playshuzu[i];        
  178. }
  179. //===============================================           //顯示數(shù)轉操作數(shù)
  180. void zhuandisplayjiguo()
  181. {
  182.         uchar i,k;
  183.         long ttt;

  184.         for(i=1;i<9;i++)
  185.                 playshuzu[i]=shuzi[10];
  186.     //----------------------------------------
  187.         switch(fuhao)
  188.         {
  189.                 case 1: jiguo=bczs+czs;break;
  190.                 case 2: jiguo=bczs-czs;break;
  191.                 case 3: jiguo=bczs*czs;break;
  192.                 case 4: jiguo=bczs/czs;break;
  193.                 case 0: jiguo=jiguo1;break;
  194.         }
  195.         fuhao=0;

  196.         //----------------------------------------
  197.         ttt=jiguo;
  198.         jiguo1=jiguo;                                                           //顯示處理
  199.         k=1;                                        //取數(shù)的個數(shù)
  200.     for(i=1;i<=8;i++)
  201.            {
  202.                            if(ttt/10!=0)  k++;
  203.                         ttt=ttt/10;        
  204.            }        

  205.         for(i=8;i>k;i--)
  206.            {
  207.                            playshuzu[i]=shuzi[10];
  208.            }
  209.         if(jiguo<0)  { jiguo=fabs(jiguo);playshuzu[k+1]=shuzi[11]; }   //取正加負號
  210.         
  211.         for(i=1;i<=k;i++)
  212.            {
  213.                            playshuzu[i]=shuzi[jiguo%10];
  214.                                 jiguo=jiguo/10;        
  215.            }
  216.         if(jiguo1>99999999) displayerror();
  217.         
  218. }
  219. //===================================================
  220. void qin()
  221. {
  222.         uchar i;
  223.         yiweiflag=1;  //1不移
  224.         playshuzu[1]=shuzi[0];
  225.         for(i=2;i<9;i++)
  226.                 playshuzu[i]=shuzi[10];
  227. }
  228. //===================================================
  229. void displayerror()
  230. {
  231.         uchar i;
  232.         for(i=1;i<=8;i++)
  233.                 playshuzu[i]=error[i];
  234. }
  235. //===================================================
  236. void jiafa()   //+
  237. {

  238.         qinflag=1;
  239.     if(jiguo1!=0) bczs=jiguo1;
  240.         czs=0;
  241.         czs12=2;
  242.         fuhao=1;   //+               
  243. }

  244. void jianfa()
  245. {

  246.         qinflag=1;
  247.         if(jiguo1!=0) bczs=jiguo1;
  248.         czs=0;
  249.         czs12=2;
  250.         fuhao=2;   //-
  251. }

  252. void chengfa()
  253. {

  254.         qinflag=1;
  255.         if(jiguo1!=0) bczs=jiguo1;
  256.         czs=0;
  257.         czs12=2;
  258.         fuhao=3;   //*        
  259. }

  260. void chufa()
  261. {

  262.         qinflag=1;
  263.         if(jiguo1!=0) bczs=jiguo1;
  264.         czs=0;
  265.         czs12=2;
  266.         fuhao=4;   // /
  267. }
復制代碼
Keil代碼與Proteus仿真下載:
實驗20.7z (223.76 KB, 下載次數(shù): 203)


作者: wukouyyq    時間: 2022-4-12 14:50
今天剛注冊,沒有下載權限,只能看看。

自己寫了一些初步入門的小程序,想上傳上去請老大們支點一二,不知有沒有這個權限
作者: 斷城殘夢    時間: 2022-6-14 21:49
感謝大佬的指點

作者: 斷城殘夢    時間: 2022-6-16 00:26
感謝大佬的指點




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1