#题目要求:利用开发板的四个按键来控制四颗LED灯,以实现不同的显示效果。具体要求为:
1.无按键按下,LED全灭
2.按下key[0],从右向左流水灯
3.按下key[1],从左向右流水灯
4.按下key[2],LED灯闪烁
5.按下key[4],LED全亮
根据以下代码得到的结果是:无按键按下时LED不亮。单独按下任一按键LED都无反应。只有同时按下key[0],key[1]和key[2]时led交替闪烁。刚接触开发板,真不知道问题出在哪里
`timescale 1ns / 1ps
module key_led(
input sys_clk,
input sys_rst_n,
input [3:0]key,
output [3:0]led
);
reg [23:0] cnt;
reg [3:0] led_middle;
assign led = led_middle;
always@(posedge sys_clk or negedge sys_rst_n)begin
if(!sys_rst_n)
cnt <= 24'd0;
else if(cnt < 24'd999_9999)
cnt <= cnt + 24'd1;
else
cnt <= 24'd0;
end
always@(*)begin
case(key)
4'b0000 : led_middle = 4'b0000;//led all off
4'b0001 : begin //key[0]flow led right to lift
if(cnt < 24'd249_9999 )
led_middle = 4'b0001;
else if(cnt < 24'd499_9999)
led_middle = 4'b0010;
else if(cnt < 24'd749_9999)
led_middle = 4'b0100;
else
led_middle = 4'b1000;
end
4'b0010 : begin //key[1]flow led left to right
if(cnt <24'd249_9999 )
led_middle = 4'b1000;
else if(cnt < 24'd499_9999)
led_middle = 4'b0100;
else if(cnt < 24'd749_9999)
led_middle = 4'b0010;
else
led_middle = 4'b0001;
end
4'b0100 : begin //key[2]led twinkle
if(cnt <24'd499_9999 )
led_middle = 4'b1111;
else
led_middle = 4'b0000;
end
4'b1000 : led_middle = 4'b1111;//key[3]led all on
default led_middle = 4'b0000;
endcase
end
endmodule