module dou(clk,a,b);
input clk,a;
output b;
reg b;
reg [31:0] count; //记录输入方波的脉宽
reg [31:0] wide; //计算输出方波的脉宽
reg [31:0] rega;
reg [31:0] regb;
reg [31:0] regc;
reg [31:0] regd;
reg [1:0] state; //状态位
parameter ready=2'b00,high=2'b01,low=2'b10;
initial
begin
count=32'h0000;
wide=32'h0000;
rega=32'h0000;
regb=32'h0000;
state=ready;
end
////////////////////////////////////测量输入脉宽并计算输出脉宽
always @(posedge clk or posedge a)
begin
if(a)
count<=count+1;
else
begin
if(wide==32'h0000)
begin
wide<=(count>>1);
regc<=count;
regd<=count;
end
else if(regd!=regc)
begin
count<=32'h0000;
wide<=32'h0000;
end
else begin
regd<=count;
count<=32'h0000;
end
end
end
////////////////////////////////产生输出方波的状态机
always @(posedge clk)
begin
if(wide==32'h0000)
begin
b<=0;
state<=ready;
end
else
begin
case(state)
ready: begin
rega<=32'h0000;
regb<=32'h0000;
state<=high;
end
high: begin
b<=1;
rega<=rega+1;
if(rega==wide)
state<=low;
else state<=high;
end
low: begin
b<=0;
regb<=regb+1;
if(regb==wide)
state<=ready;
else state<=low;
end
endcase
end
end
endmodule
有奖活动 | |
---|---|
“我踩过的那些坑”主题活动——第002期 | |
【EEPW电子工程师创研计划】技术变现通道已开启~ | |
发原创文章 【每月瓜分千元赏金 凭实力攒钱买好礼~】 | |
【EEPW在线】E起听工程师的声音! | |
高校联络员开始招募啦!有惊喜!! | |
【工程师专属福利】每天30秒,积分轻松拿!EEPW宠粉打卡计划启动! | |
送您一块开发板,2025年“我要开发板活动”又开始了! | |
打赏了!打赏了!打赏了! |