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

QQ登錄

只需一步,快速開始

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

51單片機(jī)+Proteus仿真實(shí)時(shí)1秒鐘讀取的脈沖數(shù)量源程序

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:298663 發(fā)表于 2019-10-25 16:07 | 只看該作者 |只看大圖 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)


單片機(jī)源程序如下:
  1. /************************************************************************************************************\
  2. **文件名稱:xx.c
  3. **功能描述:計(jì)算一秒鐘延時(shí)瞬間脈沖數(shù)量
  4. **日期:2019.10.25
  5. **版本:v1
  6. ***************************************************************************************************************/

  7. #include <reg51.h>

  8. #define uchar unsigned char
  9. #define uint  unsigned int
  10. uchar code numtab[16]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};//定義共陽(yáng)數(shù)碼管0-9


  11. uint pulse_time;    //定義一個(gè)定時(shí)中斷變量
  12. uint pulse_count;  //定義脈沖低電平變量
  13. uint pulse;        //定義1秒脈沖數(shù)變量

  14. /***************************************************************************************************\
  15. **函數(shù)名稱;初始化定時(shí)和外部中斷
  16. **
  17. ****************************************************************************************************/
  18. void MCU_init()
  19. {
  20.     EX0=1;//開外部中斷0
  21.     PX0=1;// 外部中斷0高優(yōu)先級(jí)
  22.     IT0=1;//觸發(fā)模式0= 電平 1=邊沿觸發(fā)
  23.     TMOD = 0x11;
  24.     ET1=1;
  25.     TR1=1;

  26.          TH1 = (65535-25000)/256;   //載入初值,設(shè)置定時(shí)器1 25ms中斷一次
  27.          TL1 = (65535-25000)%256;
  28.          EA=1;                                                                                
  29.         
  30.         
  31. }
  32. /*******************************************************************************************\
  33. **函數(shù)名稱:定時(shí)器1入口
  34. **功能:50us中斷一次
  35. ***************************************************************************************************/
  36. void TIME1() interrupt 3
  37. {
  38.         
  39.         pulse_time++;
  40.         if(pulse_time==40)  //達(dá)到1000ms,即1s鐘
  41.            {
  42.                     pulse_time=0;   //定時(shí)中斷變量復(fù)位
  43.                     pulse=pulse_count/2;    //一個(gè)周期脈沖有兩次低電平信號(hào),所以1秒鐘脈沖數(shù)量為總低電平次數(shù)除以2
  44.                           pulse_count=0;      //復(fù)位脈沖低電平變量
  45.             }
  46.         
  47.         
  48. }
  49. /*******************************************************************************************\
  50. **函數(shù)名稱:外部中斷入口
  51. **功能:檢測(cè)沒(méi)沖低電平信號(hào)
  52. ***************************************************************************************************/
  53. void IINT0() interrupt 0
  54. {
  55.         
  56.         pulse_count++;  //當(dāng)脈沖信號(hào)低電平時(shí)候+1;
  57.                
  58. }
  59. /*********************************************************************\
  60. **函數(shù)名稱:delay(uint m)
  61. **功能:延時(shí)函數(shù)
  62. **********************************************************************/
  63. void delay(uchar m)
  64. {
  65.         uchar i,y;
  66.         for(i=m;i>0;i--)
  67.          for(y=110;y>0;y--);
  68. }
  69. /*********************************************************************\
  70. **函數(shù)名稱:display(uint xx)
  71. **功能:4位數(shù)碼管顯示程序
  72. **********************************************************************/
  73. void display(uint xx)
  74. {
  75.         if(xx>=0&&xx<10)
  76.         {
  77.                 P2=0X08;
  78.                 P0=numtab[xx];        
  79.                 delay(3);
  80.                 P0=0XFF;
  81.         }
  82.         if(xx>=10&&xx<100)
  83.         {
  84.                 P2=0X04;
  85.                 P0=numtab[xx/10];
  86.                 delay(3);P0=0XFF;
  87.                 P2=0X08;
  88.                 P0=numtab[xx%10];
  89.                 delay(3);P0=0XFF;
  90.         }
  91.         if(xx>=100&&xx<1000)
  92.         {
  93.                 P2=0X02;
  94.                 P0=numtab[xx/100];
  95.                 delay(3);P0=0XFF;
  96.                 P2=0X04;
  97.                 P0=numtab[xx%100/10];
  98.                 delay(3);P0=0XFF;
  99.                 P2=0X08;
  100.                 P0=numtab[xx%100%10];
  101.                 delay(3);P0=0XFF;
  102.         }
  103.         if(xx>=1000&&xx<10000)
  104.         {
  105.                 P2=0X01;
  106.                 P0=numtab[xx/1000];
  107.                 delay(3);P0=0XFF;
  108.                 P2=0X02;
  109.                 P0=numtab[xx%1000/100];
  110.                 delay(3);P0=0XFF;
  111.                 P2=0X04;
  112.                 P0=numtab[xx%1000%100/10];
  113.                 delay(3);P0=0XFF;
  114.                 P2=0X08;
  115.                 P0=numtab[xx%1000%100%10];
  116.                 delay(3);P0=0XFF;
  117.         }
  118.         

  119. }
  120. //主函數(shù)入口
  121. void main()
  122. {

  123.          MCU_init(); //初始化
  124.          delay(1000);
  125.         while(1)
  126.         {
  127.          
  128.                 display(pulse);
  129.         }
  130. }
復(fù)制代碼

所有資料51hei提供下載:
脈沖計(jì)數(shù).zip (79.66 KB, 下載次數(shù): 51)


評(píng)分

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

查看全部評(píng)分

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

使用道具 舉報(bào)

沙發(fā)
ID:801963 發(fā)表于 2020-7-17 16:07 | 只看該作者
emmmm說(shuō)好的Proteus工程文件可在附件中下載呢?下載了沒(méi)找到Proteus的文件
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

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

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

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