(1)方法一
偶数分频,假设N(偶数)分频,只需要计数到N/2-1,然后时钟翻转、计数器清零,如此循环就可以得到N(偶)分频。
`timescale 1ns / 1ps
module oufenpindianlu(
input clk ,
input rst_n ,
output reg out_clk
);
parameter N=4;
reg [N/2-1:0] cnt;
always@(posedge clk or negedge rst_n)begin
if (!rst_n)begin
cnt<=0;
out_clk<=0;
end
else begin
if(cnt==N/2-1)begin
out_clk<=~out_clk;
cnt<=0;
end
else
cnt<=cnt+1;
end
end
endmodule
程序仿真结果如下:
(2)方法二
直接使用计数器分频
module CNT(
input clk,
input rst_n,
output reg [2:0] y
);
always @(posedge clk or negedge rst_n)begin
if (rst_n==1'b0)
y<=3'b0;
else
y<=y+1'b1;
end
endmodule
仿真结果: