DE2-115 60Sec Clock
DE2-115 60秒鐘時鐘
//Filename:digi_60sec.v
module digi_60sec (
input CLOCK_50,
input [17:15] SW,
input [3:0] KEY,
output [6:0] HEX0,
output [6:0] HEX1
);
wire clk_1;
wire [3:0] w_sq0;
wire [2:0] w_sq1;
// 1Hz clock
divn # (.WIDTH(26), .N(50000000))
u0 (
.clk(CLOCK_50), .rst_n(KEY[0]), .o_clk(clk_1)
);
clock u1 (
.clk(clk_1),
.en(SW[17]), // input enable
.clr(SW[16]), // input clear
.load(SW[15]), // input load
.sd0(4'h0), // input second digit 0
.sd1(3'h0), // input second digit 1
.sq0(w_sq0), // output second digit 0
.sq1(w_sq1) // output second digit 1
);
// sec. dig0 to seg7
seg7_lut u2 ( .i_dig(w_sq0), .o_seg(HEX0));
// sec. dig1 to seg7
seg7_lut u3 ( .i_dig({1'b0, w_sq1}), .o_seg(HEX1));
endmodule
=============================================
/*
Filename : clock.v
*/
module clock (
input clk,
input en,
input clr,
input load,
input [3:0] sd0,
input [2:0] sd1,
output [3:0] sq0,
output [2:0] sq1,
output co
);
wire w_clr;
wire [3:0] w_md0;
wire [2:0] w_md1;
reg [3:0] w_hd0;
reg [2:0] w_hd1;
wire w_sco; // second carry
counter60 sec (
.clk(clk),
.load(load),
.clr(w_clr),
.en(en),
.d0(sd0),
.d1(sd1),
.q0(sq0),
.q1(sq1),
.co(w_sco)
);
assign w_clr = clr | co;
assign co = w_sco;
endmodule
Filename : divn.v
*/
module divn (
input clk,
input rst_n,
output o_clk
);
parameter WIDTH = 3;
parameter N = 6;
reg [WIDTH-1:0] cnt_p;
reg [WIDTH-1:0] cnt_n;
reg clk_p;
reg clk_n;
assign o_clk = (N == 1) ? clk : (N[0]) ? (clk_p | clk_n) : (clk_p);
always@(posedge clk or negedge rst_n) begin
if (!rst_n)
cnt_p <= 0;
else if (cnt_p == (N-1))
cnt_p <= 0;
else
cnt_p <= cnt_p + 1;
end
always@(posedge clk or negedge rst_n) begin
if (!rst_n)
clk_p <= 1;
else if (cnt_p < (N>>1))
clk_p = 1;
else
clk_p = 0;
end
always@(negedge clk or negedge rst_n) begin
if (!rst_n)
cnt_n <= 0;
else if (cnt_n == (N-1))
cnt_n <= 0;
else
cnt_n <= cnt_n + 1;
end
always@(negedge clk or negedge rst_n) begin
if (!rst_n)
clk_n <= 1;
else if (cnt_n < (N>>1))
clk_n = 1;
else
clk_n = 0;
end
endmodule
=============================================
/*
Filename : counter60.v
*/
module counter60 (
input clk,
input clr,
input load,
input en,
input [3:0] d0,
input [3:0] d1,
output reg [3:0] q0,
output reg [2:0] q1,
output co
);
assign co = q1[2] & q1[0] & q0[3] & q0[0]; // 101 1001 = 59
always@(posedge clk) begin
if (clr) begin
q0 <= 0;
q1 <= 0;
end
else if (load) begin
q0 <= d0;
q1 <= d1;
end
else if (en) begin
if (q0 == 9) begin
q0 <= 0;
if (q1 == 5)
q1 <= 0;
else
q1 <= q1 + 1;
end
else
q0 <= q0 + 1;
end
else begin
q0 <= q0;
q1 <= q1;
end
end
endmodule
=============================================
/*
Filename : seg7_lut.V
*/
module seg7_lut (
input [3:0] i_dig,
output reg [6:0] o_seg
);
always@(i_dig) begin
case(i_dig)
4'h1: o_seg = 7'b111_1001; // ---t----
4'h2: o_seg = 7'b010_0100; // | |
4'h3: o_seg = 7'b011_0000; // lt rt
4'h4: o_seg = 7'b001_1001; // | |
4'h5: o_seg = 7'b001_0010; // ---m----
4'h6: o_seg = 7'b000_0010; // | |
4'h7: o_seg = 7'b111_1000; // lb rb
4'h8: o_seg = 7'b000_0000; // | |
4'h9: o_seg = 7'b001_1000; // ---b----
4'ha: o_seg = 7'b000_1000;
4'hb: o_seg = 7'b000_0011;
4'hc: o_seg = 7'b100_0110;
4'hd: o_seg = 7'b010_0001;
4'he: o_seg = 7'b000_0110;
4'hf: o_seg = 7'b000_1110;
4'h0: o_seg = 7'b100_0000;
endcase
end
endmodule
沒有留言:
張貼留言