標(biāo)題: 51單片機(jī)60秒顯示程序 [打印本頁(yè)]

作者: ww51    時(shí)間: 2018-12-6 00:19
標(biāo)題: 51單片機(jī)60秒顯示程序
此程序通過(guò)定時(shí)器0計(jì)時(shí),然后每計(jì)時(shí)一秒顯示在數(shù)碼管上,總共計(jì)時(shí)60秒,每過(guò)60秒led狀態(tài)反轉(zhuǎn)一次,數(shù)碼管位選信號(hào)引腳為P2口,并且是通過(guò)138譯碼器來(lái)控制的,即3位控制8位,138譯碼器輸入信號(hào)引腳為P2^2,P2^3,P2^4;斷選信號(hào)引腳為P0口,此程序采用的是共陰極數(shù)碼管,com口為低電平,給高電平亮。led小燈控制引腳為P2^7口。
  1. #include "reg52.h"
  2. typedef unsigned char u8;
  3. typedef unsigned int u16;
  4. #define SEG P0
  5. //#define SCANP P2
  6. sbit LED=P2^7;
  7. #define count_M1 50000
  8. #define TH_M1 (65536-count_M1)/256
  9. #define TL_M1 (65536-count_M1)%256
  10. int count_T0=0;
  11. #define count_M2 250
  12. #define TH_M2 (256-count_M2)
  13. #define TL_M2 (256-count_M2)
  14. u8 count_T1=0;
  15. u8 code TAB[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
  16. u8 disp[2]={0x3f,0x3f};
  17. u8 wei [2]={0x80,0x84};
  18. u8 seconds=0;
  19. //u8 scan=0x1c;
  20. void delay(u16 i)
  21. {
  22.         while(i--);
  23. }
  24. main()
  25. {
  26.             
  27.         IE=0x82;
  28.         TMOD=0x01;
  29.         TH0=TH_M1;TL0=TL_M1;
  30.         TR0=1;
  31.         while(1)
  32.         {
  33.         u8 i;        
  34.                 for(i=0;i<=1;i++)
  35.                 {
  36. //                count_T1=0;
  37.                 SEG=0x00;
  38.                 delay(10);
  39. //                SCANP=~scan;
  40.                 SEG=disp[i];
  41.                 P2=wei[i];
  42.                 delay(100);
  43.                 }
  44. //        TH1=TH_M2;TL1=TL_M2;
  45. //        TR1=1;
  46. //        LED=1;
  47. }
  48. }        

  49. void T0_1s(void) interrupt 1
  50. {
  51.         TH0=TH_M1;TL0=TL_M1;
  52.         if(++count_T0==20)
  53.         {
  54.                 count_T0=0;
  55.                 seconds++;
  56.                 if(seconds==60)
  57.                 {
  58.                         seconds=0;
  59.                         LED=~LED;
  60.                         delay(10000);
  61.                 }
  62.         }
  63.         disp[1]=TAB[seconds/10];
  64.         disp[0]=TAB[seconds%10];
  65. }
復(fù)制代碼



作者: admin    時(shí)間: 2018-12-6 04:26
補(bǔ)全原理圖或者詳細(xì)說(shuō)明一下電路連接即可獲得100+黑幣




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