找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 4782|回復(fù): 2
打印 上一主題 下一主題
收起左側(cè)

51單片機(jī)秒表顯示程序(注釋很詳細(xì))Proteus仿真

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)


單片機(jī)源程序如下:
  1. #include<reg51.h>    //包含單片機(jī)寄存器的頭文件
  2. #include<intrins.h>  //包含_nop_()函數(shù)定義的頭文件
  3. #define uint unsigned int
  4. #define uchar unsigned char
  5. sbit RS=P2^0;    //寄存器選擇位,將RS位定義為P2.0引腳
  6. sbit RW=P2^1;    //讀寫選擇位,將RW位定義為P2.1引腳
  7. sbit E=P2^2;     //使能信號(hào)位,將E位定義為P2.2引腳
  8. sbit BF=P0^7;    //忙碌標(biāo)志位,,將BF位定義為P0.7引腳
  9. unsigned char code string[ ]={"Miao Biao"};
  10. unsigned char code string1[ ]={"0123456789"};
  11. sbit key1=P1^5;                                                                          //定義開(kāi)關(guān)1
  12. sbit key2=P1^6;                                                                          //定義開(kāi)關(guān)2
  13. sbit key3=P1^7;                                                                          //定義開(kāi)關(guān)3
  14. uchar  temp,i;
  15. /*****************************************************
  16. 函數(shù)功能:延時(shí)1ms
  17. (3j+2)*i=(3×33+2)×10=1010(微秒),可以認(rèn)為是1毫秒
  18. ***************************************************/
  19. void delay1ms()
  20. {
  21.    unsigned char i,j;       
  22.          for(i=0;i<10;i++)
  23.           for(j=0;j<33;j++);                 
  24. }
  25. /*****************************************************
  26. 函數(shù)功能:延時(shí)若干毫秒
  27. 入口參數(shù):n
  28. ***************************************************/
  29. void delay(unsigned char n)
  30. {
  31.    unsigned char i;
  32.         for(i=0;i<n;i++)
  33.            delay1ms();
  34. }
  35. /*****************************************************
  36. 函數(shù)功能:判斷液晶模塊的忙碌狀態(tài)
  37. 返回值:result。result=1,忙碌;result=0,不忙
  38. ***************************************************/
  39. unsigned char BusyTest(void)
  40.   {
  41.     bit result;
  42.         RS=0;       //根據(jù)規(guī)定,RS為低電平,RW為高電平時(shí),可以讀狀態(tài)
  43.     RW=1;
  44.     E=1;        //E=1,才允許讀寫
  45.     _nop_();   //空操作
  46.     _nop_();
  47.     _nop_();
  48.     _nop_();   //空操作四個(gè)機(jī)器周期,給硬件反應(yīng)時(shí)間       
  49.     result=BF;  //將忙碌標(biāo)志電平賦給result
  50.         E=0;
  51.     return result;
  52.   }
  53. /*****************************************************
  54. 函數(shù)功能:將模式設(shè)置指令或顯示地址寫入液晶模塊
  55. 入口參數(shù):dictate
  56. ***************************************************/
  57. void WriteInstruction (unsigned char dictate)
  58. {   
  59.     while(BusyTest()==1); //如果忙就等待
  60.          RS=0;                  //根據(jù)規(guī)定,RS和R/W同時(shí)為低電平時(shí),可以寫入指令
  61.          RW=0;   
  62.          E=0;                   //E置低電平(根據(jù)表8-6,寫指令時(shí),E為高脈沖,就是讓E從0到1發(fā)生正跳變,所以應(yīng)先置"0"
  63.          _nop_();
  64.          _nop_();             //空操作兩個(gè)機(jī)器周期,給硬件反應(yīng)時(shí)間
  65.          P0=dictate;            //將數(shù)據(jù)送入P0口,即寫入指令或地址
  66.          _nop_();
  67.          _nop_();
  68.          _nop_();
  69.          _nop_();               //空操作四個(gè)機(jī)器周期,給硬件反應(yīng)時(shí)間
  70.          E=1;                   //E置高電平
  71.          _nop_();
  72.          _nop_();
  73.          _nop_();
  74.          _nop_();               //空操作四個(gè)機(jī)器周期,給硬件反應(yīng)時(shí)間
  75.          E=0;                  //當(dāng)E由高電平跳變成低電平時(shí),液晶模塊開(kāi)始執(zhí)行命令
  76. }
  77. /*****************************************************
  78. 函數(shù)功能:指定字符顯示的實(shí)際地址
  79. 入口參數(shù):x
  80. ***************************************************/
  81. void WriteAddress(unsigned char x)
  82. {
  83.      WriteInstruction(x|0x80); //顯示位置的確定方法規(guī)定為"80H+地址碼x"
  84. }
  85. /*****************************************************
  86. 函數(shù)功能:將數(shù)據(jù)(字符的標(biāo)準(zhǔn)ASCII碼)寫入液晶模塊
  87. 入口參數(shù):y(為字符常量)
  88. ***************************************************/
  89. void WriteData(unsigned char y)
  90. {
  91.     while(BusyTest()==1);  
  92.           RS=1;           //RS為高電平,RW為低電平時(shí),可以寫入數(shù)據(jù)
  93.           RW=0;
  94.           E=0;            //E置低電平(根據(jù)表8-6,寫指令時(shí),E為高脈沖,
  95.                        // 就是讓E從0到1發(fā)生正跳變,所以應(yīng)先置"0"
  96.           P0=y;           //將數(shù)據(jù)送入P0口,即將數(shù)據(jù)寫入液晶模塊
  97.           _nop_();
  98.           _nop_();
  99.           _nop_();
  100.      _nop_();       //空操作四個(gè)機(jī)器周期,給硬件反應(yīng)時(shí)間
  101.           E=1;          //E置高電平
  102.           _nop_();
  103.           _nop_();
  104.           _nop_();
  105.          _nop_();        //空操作四個(gè)機(jī)器周期,給硬件反應(yīng)時(shí)間
  106.          E=0;            //當(dāng)E由高電平跳變成低電平時(shí),液晶模塊開(kāi)始執(zhí)行命令
  107. }
  108. /*****************************************************
  109. 函數(shù)功能:對(duì)LCD的顯示模式進(jìn)行初始化設(shè)置
  110. ***************************************************/
  111. void LcdInitiate(void)
  112. {
  113.     delay(15);             //延時(shí)15ms,首次寫指令時(shí)應(yīng)給LCD一段較長(zhǎng)的反應(yīng)時(shí)間
  114.     WriteInstruction(0x38);  //顯示模式設(shè)置:16×2顯示,5×7點(diǎn)陣,8位數(shù)據(jù)接口
  115.         delay(5);   //延時(shí)5ms 
  116.         WriteInstruction(0x38);
  117.         delay(5);
  118.         WriteInstruction(0x38);
  119.         delay(5);
  120.         WriteInstruction(0x0f);  //顯示模式設(shè)置:顯示開(kāi),有光標(biāo),光標(biāo)閃爍
  121.         delay(5);
  122.         WriteInstruction(0x06);  //顯示模式設(shè)置:光標(biāo)右移,字符不移
  123.         delay(5);
  124.         WriteInstruction(0x01);  //清屏幕指令,將以前的顯示內(nèi)容清除
  125.         delay(5);
  126. }
  127. void main(void)            //主函數(shù)
  128. {
  129.   unsigned char i,a,k,o,p,z;
  130.   LcdInitiate();         //調(diào)用LCD初始化函數(shù)  
  131.   delay(10);
  132.   z=2;
  133.   while(1)
  134.      {
  135.                  WriteAddress(0x04);  // 設(shè)置顯示位置為第一行的第1個(gè)字
  136.                        i = 0;
  137.                 while(string[i] != '\0')//只要沒(méi)有寫到字符串結(jié)束標(biāo)志,就繼續(xù)寫
  138.                         {                // 顯示字符
  139.                                         WriteData(string[i]);
  140.                                         i++;
  141.                                         delay(150);
  142.                         }       
  143.         if(key1==0)                                                                          //判斷開(kāi)關(guān)1是否按下 按下開(kāi)始執(zhí)行
  144.                 {
  145.                 P1=0xff;
  146.                 for( i=0;i<=99;i++)                                                  //顯示00-99,共100次
  147.                 {
  148.                
  149.                 if(key2==0)                                                                  //判斷開(kāi)關(guān)2是否按下
  150.                 {
  151.                 z=z+1;                                                                          //z+1
  152.                 }
  153.                 if(z%2==0)
  154.                 {                                                                  //判斷z是否是偶數(shù),是則執(zhí)行                          
  155.                 for(k=0;k<10;k++)                                                  //循環(huán)25次 每次消耗40ms  一共1000ms=1s
  156.                 {                                                                                  
  157.        
  158.                                  WriteAddress(0x47);                  // 設(shè)置顯示位置為第二行的第7個(gè)字
  159.                                  a=0;
  160.                                         while(1)                                //只要沒(méi)有寫到字符串結(jié)束標(biāo)志,就繼續(xù)寫
  161.                                         {                                        // 顯示字符
  162.                                         o=i/10           ;                //十位
  163.                                         WriteData(string1[o]);
  164.                                         p=i%10        ;                //個(gè)位
  165.                                         WriteData(string1[p]);
  166.                                         a++;
  167.                                         delay(80);
  168.                                         temp=i;                        //將值傳到暫存器
  169.                                         if(a==1) break;
  170.                                         }
  171.                                 }                       
  172.                         } else{
  173.                                         while(1)
  174.                                         {
  175.                                          WriteAddress(0x47);                  // 設(shè)置顯示位置為第二行的第7個(gè)字
  176.                                          a = 0;
  177.                                         while(1)                                //只要沒(méi)有寫到字符串結(jié)束標(biāo)志,就繼續(xù)寫
  178.                                         {                                        // 顯示字符
  179.                                         o=temp/10           ;                //十位
  180.                                         WriteData(string1[o]);
  181.                                         p=temp%10        ;                //個(gè)位
  182.                                         WriteData(string1[p]);
  183.                                         a++;
  184.                                         delay(150);
  185.                                         if(a==1)break;
  186.                                         }
  187.                                         if(key3==0)
  188.                                         {
  189.                                                  while(1)
  190.                                         {
  191.                                          WriteAddress(0x47);                  // 設(shè)置顯示位置為第二行的第7個(gè)字
  192.                                          a=0;
  193.                                         while(1)                                //只要沒(méi)有寫到字符串結(jié)束標(biāo)志,就繼續(xù)寫
  194.                                         {                                        // 顯示字符
  195.                                         WriteData(string1[0]);
  196.                                         WriteData(string1[0]);
  197.                                         a++;
  198.                                         delay(150);
  199.                                         if(a==1)break;
  200.                                         }
  201.                                         }
  202.                                         }
  203.                         }
  204.                         }
  205.                         }
  206. }
  207. }
  208. }
復(fù)制代碼

所有資料51hei提供下載:
秒表顯示.zip (58.71 KB, 下載次數(shù): 45)


評(píng)分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:560381 發(fā)表于 2019-6-11 21:49 | 只看該作者
一直在找這個(gè),感謝
回復(fù)

使用道具 舉報(bào)

板凳
ID:653096 發(fā)表于 2019-12-5 18:48 | 只看該作者
沒(méi)有仿真附件啊,下載了就一個(gè)程序
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表