这个是交通灯的按键和led,如下这一组

`timescale 1ns/1ps
module Top(
input clk, // 27MHz系统时钟
input rst_n, // 低电平复位
input key, // 按键输入
output reg led // LED输出
);
// 15ms消抖计数值: 27,000,000 * 0.015 = 405,000
parameter CNT_15MS = 20'd405_000;
reg [19:0] cnt;
reg [1:0] key_reg;
wire key_stable;
// 异步信号同步化,防止亚稳态
always @(posedge clk or negedge rst_n) begin
if(!rst_n)
key_reg <= 2'b11;
else
key_reg <= {key_reg[0], key};
end
// 消抖计数器逻辑
always @(posedge clk or negedge rst_n) begin
if(!rst_n)
cnt <= 20'd0;
else if(key_reg[1] == 1'b1) // 按键未按下(高电平)时清零
cnt <= 20'd0;
else if(cnt < CNT_15MS) // 按键按下(低电平)开始计数
cnt <= cnt + 1'b1;
end
// 产生按键触发脉冲(计数到15ms那一刻产生一个周期高电平)
assign key_stable = (cnt == CNT_15MS - 1'b1) ? 1'b1 : 1'b0;
// LED状态翻转逻辑
always @(posedge clk or negedge rst_n) begin
if(!rst_n)
led <= 1'b1; // 初始熄灭(低点亮模式)
else if(key_stable)
led <= ~led; // 状态翻转
end
endmodule
define_attribute {p:clk} {PAP_IO_DIRECTION} {INPUT}
define_attribute {p:clk} {PAP_IO_LOC} {D18}
define_attribute {p:clk} {PAP_IO_VCCIO} {3.3}
define_attribute {p:clk} {PAP_IO_STANDARD} {LVCMOS33}
define_attribute {p:clk} {PAP_IO_NONE} {TRUE}
define_attribute {p:rst_n} {PAP_IO_DIRECTION} {INPUT}
define_attribute {p:rst_n} {PAP_IO_LOC} {C22}
define_attribute {p:rst_n} {PAP_IO_VCCIO} {3.3}
define_attribute {p:rst_n} {PAP_IO_STANDARD} {LVCMOS33}
define_attribute {p:rst_n} {PAP_IO_NONE} {TRUE}
define_attribute {p:key} {PAP_IO_DIRECTION} {INPUT}
define_attribute {p:key} {PAP_IO_LOC} {U21}
define_attribute {p:key} {PAP_IO_VCCIO} {3.3}
define_attribute {p:key} {PAP_IO_STANDARD} {LVCMOS33}
define_attribute {p:key} {PAP_IO_NONE} {TRUE}
define_attribute {p:led} {PAP_IO_DIRECTION} {OUTPUT}
define_attribute {p:led} {PAP_IO_LOC} {U16}
define_attribute {p:led} {PAP_IO_VCCIO} {3.3}
define_attribute {p:led} {PAP_IO_STANDARD} {LVCMOS33}
define_attribute {p:led} {PAP_IO_DRIVE} {8}
define_attribute {p:led} {PAP_IO_SLEW} {SLOW}
define_attribute {p:led} {PAP_IO_NONE} {TRUE}


没有回复内容