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

QQ登錄

只需一步,快速開始

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

基于51單片機(jī)LCD1602和數(shù)碼管顯示倒計(jì)時(shí)程序Proteus仿真圖

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
使用8051單片機(jī)控制的倒計(jì)時(shí)器程序,通過(guò)連接LCD顯示屏和數(shù)碼管來(lái)實(shí)現(xiàn)倒計(jì)時(shí)功能
系統(tǒng)接1個(gè)按鍵用于啟動(dòng),1個(gè)蜂鳴器用于提示計(jì)時(shí)時(shí)間到,兩個(gè)數(shù)碼管用于顯示時(shí)間。
功能:系統(tǒng)上電數(shù)碼管顯示60;當(dāng)按鍵按下時(shí),系統(tǒng)開始倒計(jì)時(shí),同時(shí)數(shù)碼管顯示當(dāng)前時(shí)間;當(dāng)計(jì)時(shí)為0時(shí),蜂鳴器響。
仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)


單片機(jī)源程序如下:
  1. #include<reg52.h>
  2. sbit buzzer=P3^3;
  3. sbit key=P3^2;
  4. sbit m=P3^0;
  5. sbit n=P3^1;
  6. sbit RS=P3^4;
  7. sbit RW=P3^5;
  8. sbit E=P3^6;
  9. unsigned char a[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
  10. unsigned char b[10]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};
  11. unsigned char DJS=0;
  12. unsigned int count;
  13. unsigned char s = 60; // 倒計(jì)時(shí)初始值
  14. void delay(unsigned char t)
  15. {
  16.          unsigned char i,j;
  17.          for (i = 0; i < t; i++)
  18.     for (j = 0; j < 250; j++);
  19. }
  20. void writecommand(unsigned char command)
  21. {
  22.                                 RS=0;
  23.                                 RW=0;
  24.                                 P1=command;
  25.                                 delay(5);
  26.                                 E=1;
  27.                                 delay(5);
  28.                                 E=0;
  29. }
  30. void writecount(unsigned char count)
  31. {
  32.                                 RS=1;
  33.                                 RW=0;
  34.                                 P1=count;
  35.                                 delay(5);
  36.                                 E=1;
  37.                                 delay(5);
  38.                                 E=0;
  39. }
  40. void cursor(unsigned char H,m)
  41. {
  42.                         if(H==0)
  43.         {
  44.                                 writecommand(0x80+m);
  45.         }
  46.                         else if(H==1)
  47.         {
  48.                                 writecommand(0x80+0x40+m);
  49.         }
  50. }
  51. void LCDstring(unsigned char *string)
  52. {
  53.                                 while(*string!='\0')
  54.                                 writecount(*string++);
  55. }
  56. void LCDInit()
  57. {
  58.                                 E=0;
  59.                                 writecommand(0x38);
  60.                                 writecommand(0x0c);
  61.                                 writecommand(0x06);
  62.                                 writecommand(0x01);
  63. }
  64. //void on()
  65. //{
  66. //                                writecommand(0x0c);
  67. //}
  68. //void off()
  69. //{
  70. //                                writecommand(0x08);
  71. //}
  72. void ShowInit()
  73. {        
  74.                                 cursor(0,0);
  75.                                 LCDstring("LCD");
  76.                                 cursor(0,5);
  77.                                 writecount(0x31);
  78.                                 cursor(0,6);
  79.                                 writecount(0x36);
  80.                                 cursor(0,7);
  81.                                 writecount(0x30);
  82.                                 cursor(0,8);
  83.                                 writecount(0x32);
  84.                                 cursor(0,10);
  85.                                 writecount(b[6]);
  86.                                 cursor(0,11);
  87.                                 writecount(b[0]);
  88.                                 cursor(1,0);
  89.                                 LCDstring("Test--Program--");
  90. }
  91. void display()
  92. {
  93.         if (DJS == 1)                                                //倒計(jì)時(shí)進(jìn)行中
  94. {
  95.                         if (s> 0)
  96.                 {
  97.                                 cursor(0,10);
  98.                                 writecount(b[s/10]);    //顯示定時(shí)器T0產(chǎn)生的 分(十位)
  99.                                 writecount(b[s%10]);    //顯示定時(shí)器T0產(chǎn)生的 分(個(gè)位)
  100.                                 n = 0;                                         //開數(shù)碼管1
  101.                                 m = 1;
  102.                                 P2 = a[s/10];                         //顯示十位數(shù)字
  103.                                 delay(10);                      //延時(shí)10毫秒
  104.                                 m = 0;                          //開數(shù)碼管2
  105.                                 n = 1;
  106.                                 P2 = a[s%10];                         //顯示個(gè)位數(shù)字
  107.                                 delay(10);                             //延時(shí)10毫秒
  108.                                 
  109.                 }
  110.                         else
  111.                 {
  112.                                 buzzer = 0;
  113.                                 delay(250);
  114.                                 buzzer = 1;
  115.                                 DJS = 0;                                 //倒計(jì)時(shí)結(jié)束,清零
  116.                                 cursor(0,10);
  117.                                 writecount(b[6]);            //LCD顯示數(shù)字為6
  118.                                 writecount(b[0]);            //LCD顯示數(shù)字為0
  119.                 }
  120. }
  121.                 else                                                         //倒計(jì)時(shí)為0,顯示60秒
  122.         {
  123.                         n = 0;                                        //開數(shù)碼管1
  124.                         m = 1;
  125.                         P2 = a[6];                                     //顯示數(shù)字為6
  126.                         delay(20);                                    // 延時(shí)10毫秒
  127.                         m = 0;                                         //開數(shù)碼管2
  128.                         n = 1;
  129.                         P2 = a[0];                                     // 顯示數(shù)字為0
  130.                         delay(20);                                     // 延時(shí)10毫秒
  131.                         s= 60;                                       // 重置倒計(jì)時(shí)初始值
  132.                
  133.         }
  134. }
  135. void main()
  136. {
  137.                 TMOD= 0x01;        
  138.                 TL0 = (65536 - 1000)%256;   
  139.                 TH0 = (65536 - 1000)/256;                  
  140.                 EA = 1;                                             
  141.                 ET0 = 1;                                            
  142.                 TR0 = 1;                                            
  143.                 EX0=1;
  144.                 IT0=1;
  145.                 LCDInit();
  146.                 ShowInit();
  147.                 while(1)               
  148. {
  149.                 display();
  150. }
  151. }
  152. void Int0() interrupt 0
  153. {
  154.                 delay(10);
  155.                 if(key==0)
  156. {        
  157.                 DJS=1;
  158. }
  159. }
  160. void Int1() interrupt 1
  161. {        
  162.                 TL0 = (65536-1000)%256;   
  163.                 TH0 = (65536-1000)/256;   
  164.                 count++;
  165.                 if(count == 500)
  166. {
  167.         count = 0;
  168.         s--;
  169. }
  170. }
復(fù)制代碼

Keil代碼與Proteus仿真下載:
LCD1602.zip (31.88 KB, 下載次數(shù): 29)


評(píng)分

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

查看全部評(píng)分

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

使用道具 舉報(bào)

本版積分規(guī)則

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

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

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