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

QQ登錄

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

搜索
查看: 4029|回復(fù): 8
收起左側(cè)

利用STC15單片機(jī)的PCA進(jìn)行脈沖計(jì)數(shù)程序,計(jì)算頻率

[復(fù)制鏈接]
ID:739692 發(fā)表于 2022-7-3 17:22 | 顯示全部樓層 |閱讀模式
利用STC15的PCA進(jìn)行脈沖計(jì)數(shù),計(jì)算頻率

單片機(jī)源程序如下:
  1. /********************************************************
  2. *
  3. * 平臺(tái):keil5 + STC154K58S4
  4. *
  5. * 頻率:24MHz
  6. *
  7. * 功能:使用PCA模塊的捕獲功能,判斷脈沖個(gè)數(shù),計(jì)算頻率,使用串口輸出
  8. *
  9. * 作者: 李鐵
  10. *
  11. * 時(shí)間:2022年7月3日
  12. *
  13. ***********************************************************/
  14. #include "config.h"
  15. #include <stdio.h>
  16. #include "bsp_pca.h"
  17. #include "bsp_uart.h"
  18. #include "delay.h"
  19. #define uchar unsigned char
  20. #define uint unsigned int

  21. void Delay10ms();                //@24.000MHz

  22. sbit LED = P0^0;       //輸出脈沖端口

  23. bit flag;
  24. uint count;//脈沖數(shù)
  25. uchar count_L,count_H;
  26. uint over_count; //PCA計(jì)數(shù)器溢出次數(shù)
  27. float Fre;
  28. uint count1;
  29. void main()
  30. {
  31.         UART_InitConfig();
  32.         TI = 1; //使用printf()函數(shù)時(shí),TI必須為1
  33.         PCA_InitConfig();
  34.         EA = 1;
  35.         while(1)
  36.         {
  37.                 LED = ~LED;
  38. //                printf("hello,world!\n\r");
  39.                 printf("脈沖數(shù):%d\n\r",count);
  40.                 printf("溢出數(shù):%d\n\r",over_count);
  41.                 printf("頻率:%fHz\n\r",Fre);
  42.                 delay_ms(100);
  43.         }
  44. }

  45. void PCA(void) interrupt 7
  46. {
  47.         if(CCF0 == 1)
  48.         {
  49.                 CCF0 = 0;
  50.                 CCAP0H = 0;
  51.                 CCAP0L = 0;
  52.                 flag = 1;
  53.                 count++;
  54.                 if(flag)
  55.                 {
  56.                  flag = 0;
  57.                  count_H = CCAP0H;
  58.                  count_L = CCAP0L;
  59.                  count1 = count_H;
  60.                  count1 = (count1<<8)|count_L;
  61.                  Fre = 1/((65535*over_count+count1)*0.0000000416);
  62.                  over_count = 0;
  63.                 }
  64.                
  65.         }
  66.         if(CF == 1)
  67.         {
  68.                 CF = 0;
  69.                 over_count++;
  70.         }
  71. }
復(fù)制代碼

邏輯分析儀頻率

邏輯分析儀頻率

串口輸出頻率

串口輸出頻率

PCA脈沖計(jì)數(shù).zip

68.66 KB, 下載次數(shù): 90, 下載積分: 黑幣 -5

Keil代碼

評(píng)分

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

查看全部評(píng)分

回復(fù)

使用道具 舉報(bào)

ID:65956 發(fā)表于 2022-7-4 10:20 | 顯示全部樓層
你的程序能測(cè)到0.999997---1.000003Hz的范圍值嗎?
回復(fù)

使用道具 舉報(bào)

ID:739692 發(fā)表于 2022-7-4 10:40 | 顯示全部樓層
aking991 發(fā)表于 2022-7-4 10:20
你的程序能測(cè)到0.999997---1.000003Hz的范圍值嗎?

我沒(méi)試,你可以試試,輸入口在P1.1
回復(fù)

使用道具 舉報(bào)

ID:980937 發(fā)表于 2022-12-3 21:24 | 顯示全部樓層
aking991 發(fā)表于 2022-7-4 10:20
你的程序能測(cè)到0.999997---1.000003Hz的范圍值嗎?

可以的
回復(fù)

使用道具 舉報(bào)

ID:980937 發(fā)表于 2022-12-3 21:32 | 顯示全部樓層
芯片型號(hào) : IAP15W4K58S4-Student,這款單片機(jī)可以使用這個(gè)程序嗎
回復(fù)

使用道具 舉報(bào)

ID:980937 發(fā)表于 2022-12-4 19:31 | 顯示全部樓層
為什么頻率是這樣算的啊Fre = 1/((65535*over_count+count1)*0.0000000416);
回復(fù)

使用道具 舉報(bào)

ID:980937 發(fā)表于 2023-2-19 11:21 | 顯示全部樓層
怎么現(xiàn)在代碼用不了了
回復(fù)

使用道具 舉報(bào)

ID:387957 發(fā)表于 2023-5-5 13:08 | 顯示全部樓層
你好,15W404AS,用了你的例程,一直無(wú)法測(cè)出穩(wěn)定的輸入頻率,用廠家的例程也是,不知道問(wèn)題出在哪里,能指導(dǎo)一下嗎,V18913989589
回復(fù)

使用道具 舉報(bào)

ID:387957 發(fā)表于 2023-5-5 13:08 | 顯示全部樓層
你好,15W404AS,用了你的例程,一直無(wú)法測(cè)出穩(wěn)定的輸入頻率,用廠家的例程也是,不知道問(wèn)題出在哪里,能指導(dǎo)一下嗎,V18913989589
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

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

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

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