特权的程序不知道为什么,大毛病没有,总留下小毛病......故意留下的?
如果按照原来的程序,640ms检测一下按键,20ms判断按键的翻转,实在是.....
`timescale 1ns / 1ps
// Module Name : johnson
// Project Name : johnson
module johnson(
clk,rst_n,
key1,key2,key3,
led0,led1,led2,led3
);
input clk; //主时钟,25MHz
input rst_n; //低电平复位
input key1,key2,key3; // 按键接口
output led0,led1,led2,led3; // LED等接口
//------------------------------------
//---------------------------------------------------------------------------
reg[2:0] key_rst;
always @(posedge clk or negedge rst_n)
if (!rst_n) key_rst <= 3'b111;
else key_rst <= {key3,key2,key1};
reg[2:0] key_rst_r; //每个时钟周期的上升沿将low_sw信号锁存到low_sw_r中
always @ ( posedge clk or negedge rst_n )
if (!rst_n) key_rst_r <= 3'b111;
else key_rst_r <= key_rst;
//当寄存器key_rst由1变为0时,led_an的值变为高,维持一个时钟周期
wire[2:0] key_an = key_rst_r & ( ~key_rst);
//---------------------------------------------------------------------------
reg[19:0] cnt; //计数寄存器
always @ (posedge clk or negedge rst_n)
if (!rst_n) cnt <= 20'd0; //异步复位
else if(key_an) cnt <=20'd0;
else cnt <= cnt + 1'b1;
reg[2:0] low_sw;
always @(posedge clk or negedge rst_n)
if (!rst_n) low_sw <= 3'b111;
else if (cnt == 20'hfffff) //满20ms,将按键值锁存到寄存器low_sw中 cnt == 20'hfffff
low_sw <= {key3,key2,key1};
//---------------------------------------------------------------------------
reg [2:0] low_sw_r; //每个时钟周期的上升沿将low_sw信号锁存到low_sw_r中
always @ ( posedge clk or negedge rst_n )
if (!rst_n) low_sw_r <= 3'b111;
else low_sw_r <= low_sw;
//当寄存器low_sw由1变为0时,led_ctrl的值变为高,维持一个时钟周期
wire[2:0] led_ctrl = low_sw_r[2:0] & ( ~low_sw[2:0]);
//assign key_change = key_value_r & (~key_value); //check key_value negedge per clk
//------------------------------------
reg stop_start,left_right; //流水灯控制位
always @ (posedge clk or negedge rst_n)
if(!rst_n) begin
stop_start <= 1;
left_right <= 1;
end
else
if(led_ctrl[2]) stop_start = ~ stop_start; //开始结束控制位
else if(led_ctrl[1]) left_right <= 1; //流水灯方向控制
else if(led_ctrl[0]) left_right <= 0; //流水灯方向控制
//-------------------------------------
reg[23:0] delay;
always @ (posedge clk or negedge rst_n)
if (!rst_n) delay <= 24'd0;
else delay <= delay +1'b1;
reg[3:0] led_value_r; // LED值寄存器
always @ (posedge clk or negedge rst_n)
if(!rst_n) led_value_r <= 4'b1110;
else if(delay == 24'h3fffff && stop_start) //流水灯控制
case (left_right) //方向控制
1: led_value_r <= {led_value_r[2:0],led_value_r[3]}; //右移
0: led_value_r <= {led_value_r[0],led_value_r[3:1]}; //左移
default: ;
endcase
assign {led3,led2,led1,led0} = led_value_r;
endmodule
文章评论(0条评论)
登录后参与讨论