//---------------------按键动作检测---------------------------//
reg key_r;
reg key_rr;
always@(posedge fpga_clkor negedge rst_n)
if(!rst_n)
begin
key_r<=1'b0;
key_rr<=1'b0;
end
else
begin
key_r<=key; //输入信号延迟1拍
key_rr<=key_r; //输入信号延迟2拍
end
reg [19:0]cnt;
reg flag;
always@(posedge fpga_clk or negedge rst_n)
if(!rst_n)
flag<=1'd1;
else if((key_r==1'd0)&&(key_rr==1'd1)) //下降沿检测
flag<=1'd0;
else if((key_r==1'd1)&&(key_rr==1'd0)) //上升沿检测
flag<=1'd1;
always@(posedge fpga_clk or negedge rst_n)
if(!rst_n)
cnt<=20'd0;
else if((cnt<20'd1000_000)&&(flag==1'b0)) //约40ms
cnt<=cnt+1'd1;
else if(flag==1'b1)
cnt<=20'd0;
else
cnt<=20'd1000001;
reg [2:0] led_r;
always@(posedge fpga_clk or negedge rst_n) //LED 状态切换
if(!rst_n)
led_r<=3'b110;
else if(cnt==20'd1000_000)
led_r<={led_r[1],led_r[0],led_r[2]}; //循环左移,三色LED颜色循环切换