|
发表于 2014-3-4 10:03:39
|
显示全部楼层
我写了一段Verilog的代码,可以实现你说的功能:
因为你是32KHz的时钟,所以当脉冲宽度小于1/32kHz时,有可能采样不到!
`define time_1ms 32 //32KHz system clock
module signal_process( clk, rst, din, dout );
input clk;
input rst;
input din;
output dout;
reg dout;
reg [1:0] state;
reg [5:0] cnt_high;
reg [5:0] cnt_low;
//*******************************************************************************************
//state transfer: input(state and din), output(state)
//*******************************************************************************************
always @( posedge clk )
if(rst) begin
state <= 2'd0;
cnt_high <= 6'd0;
cnt_low <= 6'd0;
end
else begin
case(state)
2'd0: begin
if(din) begin
cnt_low <= 6'd0;
if(cnt_high == `time_1ms) begin
cnt_high <= 6'd0;
state <= 2'd1;
end
else
cnt_high <= cnt_high + 1'b1;
end
else begin
cnt_high <= 6'd0;
if(cnt_low == `time_1ms) begin
cnt_low <= 6'd0;
state <= 2'd2;
end
else
cnt_low <= cnt_low + 1'b1;
end
end
2'd1: begin
if(!din) begin
cnt_high <= 6'd0;
if(cnt_low == `time_1ms) begin
cnt_low <= 6'd0;
state <= 2'd2;
end
else
cnt_low <= cnt_low + 1'b1;
end
else begin
cnt_low <= 6'd0;
end
end
2'd2: begin
if(din) begin
cnt_low <= 6'd0;
if(cnt_high == `time_1ms) begin
cnt_high <= 6'd0;
state <= 2'd1;
end
else
cnt_high <= cnt_high + 1'b1;
end
else begin
cnt_high <= 6'd0;
end
end
default: ;
endcase
end
//*******************************************************************************************
//give a value to dout according to state
//*******************************************************************************************
always @( posedge clk )
if(rst)
dout <= 1'b0;
else
case(state)
2'd0: dout <= 1'b0;
2'd1: dout <= 1'b1;
2'd2: dout <= 1'b0;
default: ;
endcase
endmodule
波形1
|
|