利用verilog写的一个分频计数器,输入时钟为10M,输出为50K,占空比为1:3。计数器具有异步清零和异步置数功能,能置入在技术范围内的任何数据。同时占空比可调。采用ISE设计,modelsim时序仿真。具体程序如下:
//=====================原程序=====================
module zy(clk,din,clr,set,count,out,fre,duty);
input clk,clr,set;
input [10:0] din;
input[3:0] fre;
input [2:0] duty;
output out;
output [10:0] count;
reg [10:0] count;
reg out;
initial begin
count<=0;
end
always @(posedge set or posedge clk or negedge clr)
begin
if(clr==1)//clr为1时不清零
begin
if(set==0)//置位,高有效
begin
if(count==(10000/fre))//可调频率,共有16种,如fre为5,即频率为5000
count<=0;
else
count<=count+1;
end
else
begin
count<=din; //置数,超过2000即将它变为0
if(count>(10000/fre))
count<=0;
end
end
else
count<=0;//清零
end
always @(posedge clk or negedge clr)//占空比
begin
if(clr==0)
out<=0;
else if(count<=((10000/fre)/duty))//占空比可调
out<=1;
else
out<=0;
end
endmodule
//========================测试文件=========================
module zycs;
// Inputs //端口声明
reg clk;
reg [10:0] din;
reg clr;
reg set;
reg[3:0] fre;
reg[2:0] duty;
// Outputs
wire [10:0] count;
wire out;
// Instantiate the Unit Under Test (UUT)
zy uut (//端口与实际端口的连接
.clk(clk),
.din(din),
.clr(clr),
.set(set),
.count(count),
.out(out),
.fre(fre),
.duty(duty)
);
initial begin //初始化
// Initialize Inputs
clk = 0;
din = 11'b00000010101;
clr = 1;
fre=4'b101;//设置频率 共有16种,如fre为5,即频率为5000
set = 0;
duty=3'b101;//设置占空比
#200050 set=1;//每延时200050 置数一次 ,置数高有效
#100000 set=0;
end
always #5 clk=~clk;//提供输入时钟 10M
always begin
#2000005 clr=0; //每延时2000005 清零一次 清零低有效
#1000000 clr=1;
end
endmodule
---------------------------------------转载请注明出处-------------------------------------------