Verilog HDL建模(二)

    第二天,开始重新学习按键消抖实验。

module detect(clk,rstn,pin_in,H2L,L2H);

input clk;

input rstn;

input pin_in;

output H2L;

output L2H;

/*第一部分:100us的延迟*/

reg Enable;

reg [10:0]count1;

always @ (posedge clk or negedge rstn)

    if(!rstn)  begin count1 <= 11'd0;

                 Enable <= 0;end

    else if(count1 = 11'd1999) Enable <= 1;

           else count1 <= count1 + 11'd1;

/*第二部分:对按键的操作做出判断*/ 

reg H2L_1;

reg H2L_2;

reg L2H_1;

reg L2H_2;

always @ (posedge clk or negedge rstn)

    if(!rstn) begin H2L_1 <= 0;H2L_2 <= 0;L2H_1 <=0;L2H_2 <= 0; end

    else begin

           H2L_1 <= Pin_in;H2L_2 <= H2L_1;L2H_1 <= Pin_in;L2H_2 <= L2H_1;

           end

assign H2L =Enable? (H2L_2 && ~H2L_1):0;

assign L2H =Enable? (~L2H_2 && L2H_1):0;

/*assign H2L =Enable? (H2L_2 & !H2L_1):1'b0;

assign L2H =Enable? (!L2H_2 & L2H_1):1'b0;这是教材上的*/

endmodule

 紧接着是该实验的重头戏:延时模块。根据学习可以了解到按键消抖实验要求有10ms的延迟来避免按键抖动。

 由于这里做的是一个锁定按键,即按下后会被机械锁定。

module delay(clk,rstn,H2L,L2H,Pin_out);

input clk;

input H2L;

input L2H;

input rstn;

output Pin_out;

reg [15:0]cnt2;

parameter T1MS=16'd19999;

always @ (posedge clk or negedge rstn)

    if(!rstn) cnt2 <=16'd0;

   else if (isCount&&Cnt2 = T1MS);

   Cnt2 <= 16'd0;

   else if (isCount) cnt2<=cnt2+16'd1;

   else if(!isCount)cnt2<=16'd0;

reg [3:0]cnt3;

always @ (posedge clk or negedge rstn)

    if(!rstn) cnt3<=4'd0;

    else if(isCount&cnt2<=T1MS) cnt3<= cnt3 +4'd1;

    else if(isCount==0) cnt3<=4'd0;

reg [1:0]i;

reg rPin_out;

case(i)

     2'd0:
if(H2L) i <= 2
'd1; else(L2H) i <= 2'd2; 2'd1:
if(cnt3==4
'd10) begin isCount<=0;rPin_out <= 1;i<=2'd0;end else isCount <= 1; 2'd2:
if(cnt3== 4
'd10) begin isCount<=0;rPin_out <= 0;i <= 2'd0;end else isCount <=1 ; assign Pin_out = rPin_out;

 接下来是介绍协调模块和管理运作对各个模块之间分工的影响。

SOS信号实验(1)

这里由一个“控制模块”control.v产生一个sos_en的信号驱动“SOS产生模块”sos.v对Pin_out输出一个蜂鸣信号

整个模块是sos_generator.v

sos信号即有次序的输出莫斯科密码“点”、“画”和“间隔”。control.v是一个简单的定时触发器。

原文地址:https://www.cnblogs.com/jast/p/2873508.html