[英]How do I toggle a sample clock every n clock cycles?
我是Verilog的新手,所以我不確定該怎么做。 我有一個時鍾“ samp_clk”,它會在系統時鍾“時鍾”的每10個時鍾周期之間切換一次(或者這就是我試圖做的)。 這是我到目前為止的內容:
//'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
以我寫的方式,我覺得我的samp_clk只會在一個時鍾周期內保持置位狀態。 如何使它每10個時鍾周期在1和0之間切換?
您想要切換它,所以切換它。
還要注意,要每10個時鍾切換一次,當計數器的值為10-1時,必須將計數器設置為0。
試試看(未測試):
//'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
從您的代碼:
if(counter == 10)begin
samp_clk <= 1;
counter <= 0;
end
由於我們從0開始計數到10,因此這將導致11個時鍾周期。
第一步,定義一個計數器,將其重置為一定數量(時鍾周期)。 例如,您要檢測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
然后,簡單地,當它等於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
請注意,我已經分離了兩個觸發器,以使調試變得簡單明了,足以理解其邏輯。
這是包含測試台架的代碼。
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
如果您期望的是這種行為,則可以嘗試運行此代碼並查看波形。
samp_clk
,當counter
為10
時,此代碼將samp_clk
設置為1
,否則將其設置為0
。 這意味着您將獲得一個信號,該信號在1個時鍾周期內有效,並在10個時鍾周期內處於低電平狀態。 基本邏輯是正確的(計數10個時鍾周期),但給samp_clk
的值不正確。
你想擁有什么是samp_clk
是相同的值,因為它是在前面的周期,如果counter
ins't 10
和翻轉samp_clk
當它。 要翻轉信號,您需要將信號分配給信號的逆信號: samp_clk <= ~samp_clk
。
完成這些工作后,您可能需要重構代碼,因為我認為它將在當前狀態下生成閂鎖。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.