[英]How do I toggle a sample clock every n clock cycles?
I am new to Verilog, so I am not sure how to go about doing this. 我是Verilog的新手,所以我不确定该怎么做。 I have a clock, 'samp_clk', that toggles every 10 clock cycles of the system clock, 'clock' (or that's what I tried to do).
我有一个时钟“ samp_clk”,它会在系统时钟“时钟”的每10个时钟周期之间切换一次(或者这就是我试图做的)。 This is what I have so far:
这是我到目前为止的内容:
//'counter' counts the number of rising edges for system clock
//'samp_clk' is the sample clock, 'clock' is system clock
always @ (posedge clock)begin
if(~reset)begin
if(counter == 10)begin
samp_clk <= 1;
counter <= 0;
end
else begin
samp_clk <= 0;
counter <= counter + 1;
end
end
end
The way I wrote it, I feel like my samp_clk will only stay asserted for one clock cycle. 以我写的方式,我觉得我的samp_clk只会在一个时钟周期内保持置位状态。 How can I make it so that it toggles between 1 and 0 every ten clock cycles?
如何使它每10个时钟周期在1和0之间切换?
You want to toggle it, so toggle it. 您想要切换它,所以切换它。
Also note that to toggle every 10 clocks, you will have to set your counter to 0 when its value is 10-1. 还要注意,要每10个时钟切换一次,当计数器的值为10-1时,必须将计数器设置为0。
Try this (not tested): 试试看(未测试):
//'counter' counts the number of rising edge s for system clock
//'samp_clk' is the sample clock, 'clock' is sy stem clock
always @ (posedge clock)begin
if(~reset)begin
if(counter == 9)begin
samp_clk <= ~samp_clk;
counter <= 0;
end
else begin
counter <= counter + 1;
end
end
else begin
samp_clk <= 0;
end
end
From your code: 从您的代码:
if(counter == 10)begin
samp_clk <= 1;
counter <= 0;
end
This will result to 11 clock cycles since we start counting from 0 to 10. 由于我们从0开始计数到10,因此这将导致11个时钟周期。
First step, define a counter wherein it resets to a certain number (clock cycles). 第一步,定义一个计数器,将其重置为一定数量(时钟周期)。 For example, you want to detect 10 clock cycles (n = 10), when
counter
is more than or equal to 9, it sets back to 0. 例如,您要检测10个时钟周期(n = 10),当
counter
大于或等于9时,它将设置回0。
always @ (posedge clk)begin
if(~reset)begin
counter <= 0;
end
else begin
if(counter >= 9)begin
counter <= 0;
end
else begin
counter <= counter + 1;
end
end
end
Then simply, toggle samp_clk
based from the counter
when it's equal to n-1 (10 - 1 = 9). 然后,简单地,当它等于n-1(
samp_clk
= 9)时,从counter
切换samp_clk
。
always @(posedge clk) begin
if (~reset) begin
samp_clk <= 0;
end
else begin
if (counter == 9) begin
samp_clk <= ~samp_clk;
end
end
end
Notice that I've separated two flip-flops to make debugging easy and clear enough to understand its logic. 请注意,我已经分离了两个触发器,以使调试变得简单明了,足以理解其逻辑。
Here is the code with a test bench included. 这是包含测试台架的代码。
module ten_clock(input clk, reset, output reg samp_clk);
reg [7:0] counter;
//'counter' counts the number of rising edges for system clock
always @ (posedge clk)begin
if(~reset)begin
counter <= 0;
end
else begin
if(counter == 10)begin
//samp_clk <= 1;
counter <= 0;
end
else begin
//samp_clk <= 0;
counter <= counter + 1;
end
end
end
//'samp_clk' is the sample clock, 'clock' is system clock
always @(posedge clk) begin
if (~reset) begin
samp_clk <= 0;
end
else begin
if (counter == 9) begin
samp_clk <= ~samp_clk;
end
end
end
endmodule
module test;
reg clk, reset;
wire samp_clk;
ten_clock ten_clock(.*);
initial begin
clk = 0;
forever #1 clk = !clk;
end
initial begin
reset <= 1;
repeat (2) @(posedge clk);
reset <= 0;
repeat (2) @(posedge clk);
reset <= 1;
repeat (100) @(posedge clk);
$finish;
end
initial begin
$dumpfile("dump.vcd"); $dumpvars;
end
endmodule
You can try to run this code and see the wave form if this behavior is what you expect. 如果您期望的是这种行为,则可以尝试运行此代码并查看波形。
You are correct, this code sets samp_clk
to be 1
when the counter
is 10
and otherwise sets it to 0
. samp_clk
,当counter
为10
时,此代码将samp_clk
设置为1
,否则将其设置为0
。 This means you will have a signal which is asserted for 1 clock cycle and low for 10 clock cycles. 这意味着您将获得一个信号,该信号在1个时钟周期内有效,并在10个时钟周期内处于低电平状态。 The basic logic is correct (count for 10 clock cycles) but the value given to
samp_clk
is incorrect. 基本逻辑是正确的(计数10个时钟周期),但给
samp_clk
的值不正确。
What you want to have is that samp_clk
is the same value as it was in the previous cycle if counter
ins't 10
and to flip samp_clk
when it is. 你想拥有什么是
samp_clk
是相同的值,因为它是在前面的周期,如果counter
ins't 10
和翻转samp_clk
当它。 To flip a signal you want to assign the signal to the inverse of a signal: samp_clk <= ~samp_clk
. 要翻转信号,您需要将信号分配给信号的逆信号:
samp_clk <= ~samp_clk
。
After you have that working you might need to refactor your code because I think it is going to produce latches in its current state. 完成这些工作后,您可能需要重构代码,因为我认为它将在当前状态下生成闩锁。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.