基于FPGA的频率计设计

频率计是一种专门对被测信号频率进行测量的电子测量仪器。本实验是基于FPGA的频率计,所以主要用于测量方波,若需要测其他波形,则需要加入前级信号处理电路。

一、原理介绍

1.什么是频率计

频率计是一种专门对被测信号频率进行测量的电子测量仪器。本实验是基于FPGA的频率计,所以主要用于测量方波,若需要测其他波形,则需要加入前级信号处理电路。

2.测量方法与基本原理

1)FPGA普通测量法

直接计数单位时间内的脉冲数。在1s闸门时间内,记录被测信号的脉冲个数Nx,则待测频率Fx=Nx,此方法对低频信号测量误差较大,舍弃。

基于FPGA的频率计设计 - 第1张

普通测量法(图未画基准时钟)

测量时,门宽不一定为1s,所以给出通常情况下的计算公式:

基于FPGA的频率计设计 - 第2张

其中,clk_fs为基准时钟,clk_fx为待测信号,fs_cnt为基准时钟个数,fx_cnt为待测时钟个数

2)FPGA等精度测量法

本方案除给定闸门时间外,还由被测信号再生成一路计数允许信号。计数允许信号在闸门时间内第一个被测信号的上升沿开启,在闸门时间结束后被测信号的第一个上升沿结束,最后在计数允许信号的有效时间内,分别对标准频率信号个数、被测信号个数和被测信号高电平时间内标准频率信号个数计数后,再经过相关运算即可得所求频率、占空比和时间间隔。

基于FPGA的频率计设计 - 第3张

等精度测量法(同样未画基准时钟)

3)FPGA代码设计

FPGA主要完成测量待测信号频率,对输入信号个数和标准信号个数进行计数。本实验针对这部分进行设计。

3.待测信号如何输入FPGA

1)管脚图:GPIO部分(FPGA型号:Cyclone III    EP3C16F484C6N)

FPGA主要完成测量待测信号频率,对输入信号个数和标准信号个数进行计数。本实验针对这部分进行设计。

基于FPGA的频率计设计 - 第4张

原则:一定!一定!一定注意所配管脚与实际管脚的位置,配哪个用哪个,用哪个配哪个。

2)使用杜邦线连接信号源,为防止FPGA被烧坏,信号源要设置为输出3.3Vpp,偏移1.65V。

二、实验程序

1.RTL图

基于FPGA的频率计设计 - 第5张

显而易见,频率计有三个部分:门信号,边沿检测,计数器

2.Verilog(参考设计)

1.门控部分:

  1. module gate
  2. (
  3. input clk_fs , // 基准时钟信号
  4. input rst_n , // 复位信号
  5. //cymometer interface
  6. input clk_fx ,//待测信号
  7. output reg gate , //门控信号
  8. output reg gate_fs // 同步到基准时钟的门控信号
  9. );
  10. localparam GATE_TIME = 16'd5_000; // 门控时间设置
  11. reg [15:0] gate_cnt ; // 门控计数
  12. reg gate_fs_r ; // 用于同步gate信号的寄存器
  13. //门控信号计数器,使用被测时钟计数
  14. always @(posedge clk_fx or negedge rst_n) begin
  15. if(!rst_n)
  16. gate_cnt <= 16'd0;
  17. else if(gate_cnt == GATE_TIME + 5'd20)
  18. gate_cnt <= 16'd0;
  19. else
  20. gate_cnt <= gate_cnt + 1'b1;
  21. end
  22. //门控信号,拉高时间为GATE_TIME个实测时钟周期
  23. always @(posedge clk_fx or negedge rst_n) begin
  24. if(!rst_n)
  25. gate <= 1'b0;
  26. else if(gate_cnt < 4'd10)
  27. gate <= 1'b0;
  28. else if(gate_cnt < GATE_TIME + 4'd10)
  29. gate <= 1'b1;
  30. else if(gate_cnt <= GATE_TIME + 5'd20)
  31. gate <= 1'b0;
  32. else
  33. gate <= 1'b0;
  34. end
  35. //将门控信号同步到基准时钟下
  36. always @(posedge clk_fs or negedge rst_n) begin
  37. if(!rst_n) begin
  38. gate_fs_r <= 1'b0;
  39. gate_fs <= 1'b0;
  40. end
  41. else begin
  42. gate_fs_r <= gate;
  43. gate_fs <= gate_fs_r;
  44. end
  45. end
  46. endmodule

2.边沿检测

  1. module pexg(
  2. input clk_fs , // 基准时钟信号
  3. input rst_n , // 复位信号
  4. input clk_fx ,
  5. input gate,
  6. input gate_fs ,
  7. output neg_gate_fs,
  8. output neg_gate_fx
  9. );
  10. reg gate_fs_d0 ; // 用于采集基准时钟下gate下降沿
  11. reg gate_fs_d1 ; //
  12. reg gate_fx_d0 ; // 用于采集被测时钟下gate下降沿
  13. reg gate_fx_d1 ; //
  14. //wire define
  15. //边沿检测,捕获信号下降沿
  16. assign neg_gate_fs = gate_fs_d1 & (~gate_fs_d0);
  17. assign neg_gate_fx = gate_fx_d1 & (~gate_fx_d0);
  18. //打拍采门控信号的下降沿(被测时钟)
  19. always @(posedge clk_fx or negedge rst_n) begin
  20. if(!rst_n) begin
  21. gate_fx_d0 <= 1'b0;
  22. gate_fx_d1 <= 1'b0;
  23. end
  24. else begin
  25. gate_fx_d0 <= gate;
  26. gate_fx_d1 <= gate_fx_d0;
  27. end
  28. end
  29. //打拍采门控信号的下降沿(基准时钟)
  30. always @(posedge clk_fs or negedge rst_n) begin
  31. if(!rst_n) begin
  32. gate_fs_d0 <= 1'b0;
  33. gate_fs_d1 <= 1'b0;
  34. end
  35. else begin
  36. gate_fs_d0 <= gate_fs;
  37. gate_fs_d1 <= gate_fs_d0;
  38. end
  39. end
  40. endmodule

3.计数器

  1. module CNT
  2. #(parameter CLK_FS = 26'd50_000_000,// 基准时钟频率
  3. parameter MAX = 10'd64) // 定义数据位宽
  4. ( //system clock
  5. input clk_fs , // 时钟信号
  6. input rst_n , // 复位信号
  7. //cymometer interface
  8. input clk_fx , // 待测信号
  9. input gate, // 门控信号(与待测时钟同步)
  10. input gate_fs, // 与基准时钟同步的门控信号
  11. input neg_gate_fx,//
  12. input neg_gate_fs,//
  13. output reg [MAX-1:0] fs_cnt , //门控时间内基准时钟信号的个数
  14. output reg [MAX-1:0] fx_cnt , // 门控时间内待测信号的个数
  15. output reg [MAX-1:0] data_fx_temp // 待测信号的频率值
  16. );
  17. reg [MAX-1:0] fs_cnt_temp ; // fs_cnt 计数
  18. reg [MAX-1:0] fx_cnt_temp ; // fx_cnt 计数
  19. //门控时间内待测信号的计数,设置的为5000个,这里重新计数,只是用于检验信号是否正确
  20. always @(posedge clk_fx or negedge rst_n) begin
  21. if(!rst_n) begin
  22. fx_cnt_temp <= 32'd0;
  23. fx_cnt <= 32'd0;
  24. end
  25. else if(gate)begin
  26. fx_cnt_temp <= fx_cnt_temp + 1'b1;
  27. end
  28. else if(neg_gate_fx) begin
  29. fx_cnt_temp <= 32'd0;
  30. fx_cnt <= fx_cnt_temp;
  31. end
  32. end
  33. //门控时间内基准时钟的计数
  34. always @(posedge clk_fs or negedge rst_n) begin
  35. if(!rst_n) begin
  36. fs_cnt_temp <= 32'd0;
  37. fs_cnt <= 32'd0;
  38. end
  39. else if(gate_fs)
  40. begin
  41. fs_cnt_temp <= fs_cnt_temp + 1'b1;
  42. end
  43. else if(neg_gate_fs) begin
  44. fs_cnt_temp <= 32'd0;
  45. fs_cnt <= fs_cnt_temp;
  46. end
  47. end
  48. //计算待测信号的频率值
  49. always @(posedge clk_fs or negedge rst_n) begin
  50. if(!rst_n) begin
  51. data_fx_temp <= 64'd0;
  52. end
  53. else if(gate_fs == 1'b0)
  54. data_fx_temp <=CLK_FS*fx_cnt/fs_cnt;
  55. end
  56. endmodule

4.顶层

  1. module top_cymometer(
  2. //system clock
  3. input sys_clk , // 时钟信号
  4. input sys_rst_n, // 复位信号
  5. //spi没有使用
  6. // input CS_N,
  7. // input SCLK,
  8. // output MISO,
  9. //cymometer interface
  10. input clk_fx , // 被测时钟
  11. // output clk_out , // 输出时钟
  12. // output [7:0] led0,
  13. // output [7:0] led1,
  14. // output [7:0] led2,
  15. // output [7:0] led3,
  16. output [63:0]data_fx
  17. );
  18. //parameter define
  19. parameter CLK_FS = 26'd50000000; // 基准时钟频率值
  20. gate//生成门控信号
  21. (
  22. .clk_fs (sys_clk ), // 基准时钟信号
  23. .rst_n (sys_rst_n), // 复位信号
  24. //cymometer interface
  25. .clk_fx (clk_fx ), //待测信号
  26. .gate(gate ) , //门控信号
  27. .gate_fs(gate_fs) // 同步到基准时钟的门控信号
  28. );
  29. pexg//边沿捕获
  30. (
  31. .clk_fs (sys_clk ), // 基准时钟信号
  32. .rst_n (sys_rst_n), // 复位信号
  33. .gate(gate ) , //门控信号
  34. .gate_fs(gate_fs), // 同步到基准时钟的门控信号
  35. .clk_fx (clk_fx), //待测信号
  36. .neg_gate_fs(neg_gate_fs),
  37. .neg_gate_fx(neg_gate_fx)
  38. );
  39. CNT
  40. (
  41. //system clock
  42. .clk_fs (sys_clk ), // 基准时钟信号
  43. .rst_n (sys_rst_n), // 复位信号
  44. //cymometer interface
  45. .clk_fx (clk_fx ), //待测信号
  46. .gate(gate ) ,//门控信号
  47. .gate_fs(gate_fs) ,// 同步到基准时钟的门控信号
  48. .neg_gate_fs(neg_gate_fs),
  49. .neg_gate_fx(neg_gate_fx),
  50. .fs_cnt(fs_cnt) , // 门控时间内基准时钟的计数值
  51. .fx_cnt(fx_cnt) , // 门控时间内被测时钟的计数值
  52. .data_fx_temp(data_fx)
  53. );
  54. //通过SPI模块实现与单片机通信
  55. //spi(.clk(sys_clk),
  56. // .rst_n(sys_rst_n),
  57. // .CS_N(CS_N),
  58. // .SCLK(SCLK),
  59. // .MOSI(MOSI),
  60. // .txd_data(data_fx),
  61. // .MISO (MISO)
  62. // );
  63. //实现比较,最后调试是通过signaltap分析
  64. //seg_led u_seg_led(
  65. // //module clock
  66. // .clk (sys_clk ), // 数码管驱动模块的驱动时钟
  67. // .rst_n (sys_rst_n), // 复位信号
  68. //user interface
  69. // .data (data_fx ), // 被测频率值
  70. // .seg_led0 (led0),
  71. // .seg_led1 (led1),
  72. // .seg_led2 (led2),
  73. // .seg_led3 (led3)
  74. //);
  75. endmodule

3.指派引脚(很坑)

原则:就是上述原则,用哪个配哪个,要看清楚

  1. to, location
  2. # 50 MHz clock input
  3. sys_clk , PIN_G21
  4. sys_rst_n , PIN_G3
  5. clk_fx , PIN_AB11

由于输出使用SignalTap查看,所以就不指派输出的引脚了,关于SignalTap文件的设置:

基于FPGA的频率计设计 - 第6张

sys_clk为50MHz系统时钟,stp主要是为了查看data_fx信号,其他的不重要。

二、调试验证

1.输入不同频率的方波

基于FPGA的频率计设计 - 第7张

信号源调整为如图,原则为上述原则,后续只需要更改频率,频率过低的话,测试太慢,从2k开始

2kHz:

基于FPGA的频率计设计 - 第8张

100kHz:

基于FPGA的频率计设计 - 第9张

1MHz:

基于FPGA的频率计设计 - 第10张

10MHz:

基于FPGA的频率计设计 - 第11张

7355608Hz:

基于FPGA的频率计设计 - 第12张

频率为1k的整数倍时,测量结果精确,频率非整数时,信号源输出可能会有问题,导致一定的误差。

OVER结束

本文原创,作者:FPGA小白,其版权均为FPGA线上课程平台|最全栈的FPGA学习平台|FPGA工程师认证培训所有。
如需转载,请注明出处:https://z.shaonianxue.cn/1198.html

"愿我的文字能带给您一丝美好"

还没有人赞赏,支持一下

评论

A 为本文作者,G 为游客总数:0
加载中…

提交评论

游客,您好,欢迎参与讨论。

我的购物车

购物车为空

优惠券

没有优惠券