1.设计一个10进制计数器,用七段数码管显示计数器的数值,以DE2板上1个拨动开关(switch)作为计数器的时钟输入,开关每上下拨动一次,相当于产生“一个时钟周期”,观察开关抖动情况。
module switch_bounce(clk, rst, switch, y);
input clk,rst;
input switch;
output [6:0] y;
reg [6:0] y;
reg [3:0] i;
reg [9:0] switchtemp;
always@(posedge switch or negedge rst)
if(0 == rst)
begin
i <= 0; switchtemp <= 0; y <= 7'hFF;
end
else
begin
switchtemp <= {switch, switchtemp [8:0]};
if(switchtemp == 10'b1000000000)
i <= i+1;
case(i)//译码程序
4'b0000: y <= 7'hC0;//0
4'b0001: y <= 7'hF9;//1
4'b0010: y <= 7'hA4;//2
4'b0011: y <= 7'hB0;//3
4'b0100: y <= 7'h99;//4
4'b0101: y <= 7'h92;//5
4'b0110: y <= 7'h82;//6
4'b0111: y <= 7'hF8;//7
4'b1000: y <= 7'h80;//8
4'b1001: y <= 7'h90;//9
default: y <= 7'hFF;
endcase
if(i == 10) i <= 0;
end
endmodule
2.查阅资料设计一个去抖电路,拨动开关接去抖电路后再作为计数器的时钟输入,观察去抖效果。
module switch_debounce(clk, rst, switch, y);
input clk,rst;
input switch;
output [6:0] y;
reg [6:0] y;
reg [3:0] i;
reg switchtemp = 0;
reg [17:0] high,low;//定义高低电平脉宽采样计数寄存器
/*
*时钟信号clk为50MHz,对应1s=1000ms
*按键抖动时间5~10ms,取最大值计算10ms,相当于1/100,也就是0.5MHz
*延时时间一般取10~20ms,取最大值计算20ms,相当于1/50,,也就是1Mz
*/
always@(posedge clk)
if(switch) high <= high + 1;//对输入的高电平脉宽采样计数
else high <= 18'd0;
always@(posedge clk)
if(!switch) low <= low + 1;//同时对输入的低电平脉宽采样计数
else low <= 18'd0;
always@(posedge clk)
//对高电平脉宽采样计数若大于12,则输出1
if(high == 12) switchtemp <= 1'b1;
//对低电平脉宽计数若大于7,则输出0
else if (low == 7) switchtemp <= 1'b0;
always@(posedge switchtemp or negedge rst)
if(0 == rst)
begin
i <= 0; y <= 7'hFF;
end
else
begin
if(switchtemp == 1'b1)
i <= i+1;
case(i)//译码程序
4'b0000: y <= 7'hC0;//0
4'b0001: y <= 7'hF9;//1
4'b0010: y <= 7'hA4;//2
4'b0011: y <= 7'hB0;//3
4'b0100: y <= 7'h99;//4
4'b0101: y <= 7'h92;//5
4'b0110: y <= 7'h82;//6
4'b0111: y <= 7'hF8;//7
4'b1000: y <= 7'h80;//8
4'b1001: y <= 7'h90;//9
default: y <= 7'hFF;
endcase
if(i == 10) i <= 0;
end
endmodule
3(选做)设计一个上升沿检测电路,统计并显示输入信号中上升沿的个数,用DE2板上1个按键(key)模拟输入信号(注:与第一个类似)
module key_countSeg(rst, key, y);
input rst;
input key;
output [6:0] y;
reg [6:0] y;
reg [3:0] i;
reg [9:0] keytemp;
always@(posedge key or negedge rst)
if(0 == rst)
begin
i <= 0; keytemp <= 0; y <= 7'hFF;
end
else
begin
keytemp <= {key, keytemp [8:0]};
if(keytemp == 10'b1000000000)
i <= i+1;
case(i)//译码程序
4'b0000: y <= 7'hC0;//0
4'b0001: y <= 7'hF9;//1
4'b0010: y <= 7'hA4;//2
4'b0011: y <= 7'hB0;//3
4'b0100: y <= 7'h99;//4
4'b0101: y <= 7'h92;//5
4'b0110: y <= 7'h82;//6
4'b0111: y <= 7'hF8;//7
4'b1000: y <= 7'h80;//8
4'b1001: y <= 7'h90;//9
default: y <= 7'hFF;
endcase
if(i == 10) i <= 0;
end
endmodule