新手一枚,刚开始学FPGA,从学长那里拿来了一块开发板,可是他把例程全丢了,幸亏还给我留了一张原理图,哎哎哎。 这寒假就这么匆匆的来了,时间这么多,不能浪费啊,开始自学,废话不多说,上主题 。
用的是按键驱动LED,在网上找了几个程序,在此特地感谢特权同学给我提供了很多帮助,可是我还是觉得下面的这个防抖程序更简单。
`define ud #1
module ANTI_SHAKE(
//input ports
SYSCLK,
RST_B,
KEY_B,
//output ports
LED_B
);
//****************************************
//input and output declaration
//****************************************
input SYSCLK;
input RST_B;
input [3:0] KEY_B;
output [3:0] LED_B;
//****************************************
//reg and wire declaration
//****************************************
wire SYSCLK;
wire RST_B;
wire [3:0] KEY_B;
reg [3:0] LED_B;
//****************************************
//reg and wire declaration in the module
//****************************************
reg [3:0] LED_B_N;
reg [19:0] TIME_CNT;
wire [19:0] TIME_CNT_N;
reg [3:0] KEY_B_REG;
wire [3:0] KEY_B_REG_N;
wire [3:0] PRESS;
//counter
always @ (posedge SYSCLK or negedge RST_B)
begin
if(!RST_B)
TIME_CNT <= `ud 20'h0;
else
TIME_CNT <= `ud TIME_CNT_N;
end
assign TIME_CNT_N = TIME_CNT+20'h1;
always @ (posedge SYSCLK or negedge RST_B)
begin
if(!RST_B)
KEY_B_REG <= `ud 4'hf;
else
KEY_B_REG <= `ud KEY_B_REG_N;
end
assign KEY_B_REG_N = (TIME_CNT==20'hfffff)? KEY_B : KEY_B_REG;
assign PRESS = (KEY_B_REG)&(~KEY_B_REG_N); //pulse edge detected method
//****************************************
//Logic about ANTI_SHAKE,LED control
//****************************************
always @ (posedge SYSCLK or negedge RST_B)
begin
if(!RST_B)
LED_B <= `ud 4'hf;
else
LED_B <= `ud LED_B_N;
end
always @ (*)
begin
case (PRESS)
4'b0001: LED_B_N = {LED_B[3:1],(~LED_B[0])};
4'b0010: LED_B_N = {LED_B[3:2],(~LED_B[1]),LED_B[0]};
4'b0100: LED_B_N = {LED_B[3],(~LED_B[2]),LED_B[1:0]};
4'b1000: LED_B_N = {(~LED_B[3]),LED_B[2:0]};
default:LED_B_N = LED_B;
endcase
end
endmodule
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>