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
我要赚赏金打赏帖 |
|
|---|---|
| 低功耗超声波液位测距仪(ESP32S3)被打赏¥27元 | |
| 在K230DBOX上实现多时段语音提示功能被打赏¥14元 | |
| 使K230DBOX实现相机持续拍照功能被打赏¥12元 | |
| 基于K230DBOX的文本管理器设计被打赏¥14元 | |
| 基于K230DBOX的彩色画板功能实现被打赏¥15元 | |
| 在K230DBOX上实现音乐播放器功能被打赏¥15元 | |
| NXP MCU系列开发板MCULink固件升级方法【VSCode】被打赏¥28元 | |
| 三分钟快速搭建NXP MCU开发环境(VSCode)被打赏¥22元 | |
| 为遥控小车巧添功能被打赏¥26元 | |
| 【S32K3XX】FlexCAN 模块配置使用被打赏¥30元 | |
我要赚赏金
