Em muốn viết code để thực hiện mạch đếm thời gian hiển thị trên led 7 segment.Các anh chị vào xem giúp em xem em viết thế này có được không vì em không có bo mạch để thí nghiệm.Đây là code của em:
module timer (clk, rst, sw, led1, led2, led3, led4, led5, led6);
input clk, sw, rst, s1, s2;
output [6:0] led1, led2, led3, led4, led5, led6;
reg [6:0] led1, led2, led3, led4, led5, led6;
reg [3:0] bcd1, bcd2, bcd3, bcd4, bcd5, bcd6;
wire[3:0] bcd_next1, bcd_next2, bcd_next3, bcd_next4, bcd_next5, bcd_next6;
clock1Hz a(clk, clock);
always @(posedge clock)
if (rst == 1'b1)
begin
bcd1 <= 4'b0;
bcd2 <= 4'b0;
bcd3 <= 4'b0;
bcd4 <= 4'b0;
bcd5 <= 4'b0;
bcd6 <= 4'b0;
end
else if (sw == 1'b1)
begin
bcd1 <= bcd_next1;
end
else if ( bcd1 == 4'b1001)
begin
bcd2 <= bcd_next2;
end
else if ( bcd2 == 4'b0101)
begin
bcd3 <= bcd_next3;
end
else if ( bcd3 == 4'b1001)
begin
bcd4 <= bcd_next4;
end
else if ( bcd4 == 4'b0101)
begin
bcd5 <= bcd_next5;
end
else if ( bcd5 == 4'b1001)
begin
bcd6 <= bcd_next6;
end
else if ( bcd6 == 4'b0010&& bcd5==4'b0011)
begin
bcd1 <= 4'b0;
bcd2 <= 4'b0;
bcd3 <= 4'b0;
bcd4 <= 4'b0;
bcd5 <= 4'b0;
bcd6 <= 4'b0;
end
assign bcd_next1 = ( bcd1 == 4'b1001) ? 4'b0 : bcd1 + 4'b1;
assign bcd_next2 = ( bcd2 == 4'b0101) ? 4'b0 : bcd2 + 4'b1;
assign bcd_next3 = ( bcd3 == 4'b1001) ? 4'b0 : bcd3 + 4'b1;
assign bcd_next4 = ( bcd4 == 4'b0101) ? 4'b0 : bcd4 + 4'b1;
assign bcd_next5 = ( bcd5 == 4'b0011) ? 4'b0 : bcd5 + 4'b1;
assign bcd_next6 = ( bcd6 == 4'b0010) ? 4'b0 : bcd6 + 4'b1;
always @(posedge clock)
begin
case (bcd1)
4'b0000: led1 = 7'b0111111;
4'b0001: led1 = 7'b0000110;
4'b0010: led1 = 7'b1011011;
4'b0011: led1 = 7'b1001111;
4'b0100: led1 = 7'b1100110;
4'b0101: led1 = 7'b1101101;
4'b0110: led1 = 7'b1111101;
4'b0111: led1 = 7'b0000111;
4'b1000: led1 = 7'b1111111;
4'b1001: led1 = 7'b1100111;
endcase
case ( bcd2)
4'b0000: led2 = 7'b0111111;
4'b0001: led2 = 7'b0000110;
4'b0010: led2 = 7'b1011011;
4'b0011: led2 = 7'b1001111;
4'b0100: led2 = 7'b1100110;
4'b0101: led2 = 7'b1101101;
4'b0110: led2 = 7'b1111101;
4'b0111: led2 = 7'b0000111;
4'b1000: led2 = 7'b1111111;
4'b1001: led2 = 7'b1100111;
endcase
case ( bcd3)
4'b0000: led3 = 7'b0111111;
4'b0001: led3 = 7'b0000110;
4'b0010: led3 = 7'b1011011;
4'b0011: led3 = 7'b1001111;
4'b0100: led3 = 7'b1100110;
4'b0101: led3 = 7'b1101101;
4'b0110: led3 = 7'b1111101;
4'b0111: led3 = 7'b0000111;
4'b1000: led3 = 7'b1111111;
4'b1001: led3 = 7'b1100111;
endcase
case ( bcd4)
4'b0000: led4 = 7'b0111111;
4'b0001: led4 = 7'b0000110;
4'b0010: led4 = 7'b1011011;
4'b0011: led4 = 7'b1001111;
4'b0100: led4 = 7'b1100110;
4'b0101: led4 = 7'b1101101;
4'b0110: led4 = 7'b1111101;
4'b0111: led4 = 7'b0000111;
4'b1000: led4 = 7'b1111111;
4'b1001: led4 = 7'b1100111;
endcase
case ( bcd5)
4'b0000: led5 = 7'b0111111;
4'b0001: led5 = 7'b0000110;
4'b0010: led5 = 7'b1011011;
4'b0011: led5 = 7'b1001111;
4'b0100: led5 = 7'b1100110;
4'b0101: led5 = 7'b1101101;
4'b0110: led5 = 7'b1111101;
4'b0111: led5 = 7'b0000111;
4'b1000: led5 = 7'b1111111;
4'b1001: led5 = 7'b1100111;
endcase
case ( bcd6)
4'b0000: led6 = 7'b0111111;
4'b0001: led6 = 7'b0000110;
4'b0010: led6 = 7'b1011011;
4'b0011: led6 = 7'b1001111;
4'b0100: led6 = 7'b1100110;
4'b0101: led6 = 7'b1101101;
4'b0110: led6 = 7'b1111101;
4'b0111: led6 = 7'b0000111;
4'b1000: led6 = 7'b1111111;
4'b1001: led6 = 7'b1100111;
endcase
end
endmodule
// bo chia tan
module clock1Hz(Clk, clock1);
input Clk;
output reg clock1; // xung 1 Hz
reg [30:0]count ;
always @(posedge Clk)
begin
count = count + 1;
if (count==50000000)
begin clock1 = ~clock1;
count = 0;
end
end
endmodule
Nút rst của e dùng để reset,khi ấn rst thì 6 đèn led đều hiện thị số 0.Khi ấn nút Sw thì mạch bắt đầu đếm.
module timer (clk, rst, sw, led1, led2, led3, led4, led5, led6);
input clk, sw, rst, s1, s2;
output [6:0] led1, led2, led3, led4, led5, led6;
reg [6:0] led1, led2, led3, led4, led5, led6;
reg [3:0] bcd1, bcd2, bcd3, bcd4, bcd5, bcd6;
wire[3:0] bcd_next1, bcd_next2, bcd_next3, bcd_next4, bcd_next5, bcd_next6;
clock1Hz a(clk, clock);
always @(posedge clock)
if (rst == 1'b1)
begin
bcd1 <= 4'b0;
bcd2 <= 4'b0;
bcd3 <= 4'b0;
bcd4 <= 4'b0;
bcd5 <= 4'b0;
bcd6 <= 4'b0;
end
else if (sw == 1'b1)
begin
bcd1 <= bcd_next1;
end
else if ( bcd1 == 4'b1001)
begin
bcd2 <= bcd_next2;
end
else if ( bcd2 == 4'b0101)
begin
bcd3 <= bcd_next3;
end
else if ( bcd3 == 4'b1001)
begin
bcd4 <= bcd_next4;
end
else if ( bcd4 == 4'b0101)
begin
bcd5 <= bcd_next5;
end
else if ( bcd5 == 4'b1001)
begin
bcd6 <= bcd_next6;
end
else if ( bcd6 == 4'b0010&& bcd5==4'b0011)
begin
bcd1 <= 4'b0;
bcd2 <= 4'b0;
bcd3 <= 4'b0;
bcd4 <= 4'b0;
bcd5 <= 4'b0;
bcd6 <= 4'b0;
end
assign bcd_next1 = ( bcd1 == 4'b1001) ? 4'b0 : bcd1 + 4'b1;
assign bcd_next2 = ( bcd2 == 4'b0101) ? 4'b0 : bcd2 + 4'b1;
assign bcd_next3 = ( bcd3 == 4'b1001) ? 4'b0 : bcd3 + 4'b1;
assign bcd_next4 = ( bcd4 == 4'b0101) ? 4'b0 : bcd4 + 4'b1;
assign bcd_next5 = ( bcd5 == 4'b0011) ? 4'b0 : bcd5 + 4'b1;
assign bcd_next6 = ( bcd6 == 4'b0010) ? 4'b0 : bcd6 + 4'b1;
always @(posedge clock)
begin
case (bcd1)
4'b0000: led1 = 7'b0111111;
4'b0001: led1 = 7'b0000110;
4'b0010: led1 = 7'b1011011;
4'b0011: led1 = 7'b1001111;
4'b0100: led1 = 7'b1100110;
4'b0101: led1 = 7'b1101101;
4'b0110: led1 = 7'b1111101;
4'b0111: led1 = 7'b0000111;
4'b1000: led1 = 7'b1111111;
4'b1001: led1 = 7'b1100111;
endcase
case ( bcd2)
4'b0000: led2 = 7'b0111111;
4'b0001: led2 = 7'b0000110;
4'b0010: led2 = 7'b1011011;
4'b0011: led2 = 7'b1001111;
4'b0100: led2 = 7'b1100110;
4'b0101: led2 = 7'b1101101;
4'b0110: led2 = 7'b1111101;
4'b0111: led2 = 7'b0000111;
4'b1000: led2 = 7'b1111111;
4'b1001: led2 = 7'b1100111;
endcase
case ( bcd3)
4'b0000: led3 = 7'b0111111;
4'b0001: led3 = 7'b0000110;
4'b0010: led3 = 7'b1011011;
4'b0011: led3 = 7'b1001111;
4'b0100: led3 = 7'b1100110;
4'b0101: led3 = 7'b1101101;
4'b0110: led3 = 7'b1111101;
4'b0111: led3 = 7'b0000111;
4'b1000: led3 = 7'b1111111;
4'b1001: led3 = 7'b1100111;
endcase
case ( bcd4)
4'b0000: led4 = 7'b0111111;
4'b0001: led4 = 7'b0000110;
4'b0010: led4 = 7'b1011011;
4'b0011: led4 = 7'b1001111;
4'b0100: led4 = 7'b1100110;
4'b0101: led4 = 7'b1101101;
4'b0110: led4 = 7'b1111101;
4'b0111: led4 = 7'b0000111;
4'b1000: led4 = 7'b1111111;
4'b1001: led4 = 7'b1100111;
endcase
case ( bcd5)
4'b0000: led5 = 7'b0111111;
4'b0001: led5 = 7'b0000110;
4'b0010: led5 = 7'b1011011;
4'b0011: led5 = 7'b1001111;
4'b0100: led5 = 7'b1100110;
4'b0101: led5 = 7'b1101101;
4'b0110: led5 = 7'b1111101;
4'b0111: led5 = 7'b0000111;
4'b1000: led5 = 7'b1111111;
4'b1001: led5 = 7'b1100111;
endcase
case ( bcd6)
4'b0000: led6 = 7'b0111111;
4'b0001: led6 = 7'b0000110;
4'b0010: led6 = 7'b1011011;
4'b0011: led6 = 7'b1001111;
4'b0100: led6 = 7'b1100110;
4'b0101: led6 = 7'b1101101;
4'b0110: led6 = 7'b1111101;
4'b0111: led6 = 7'b0000111;
4'b1000: led6 = 7'b1111111;
4'b1001: led6 = 7'b1100111;
endcase
end
endmodule
// bo chia tan
module clock1Hz(Clk, clock1);
input Clk;
output reg clock1; // xung 1 Hz
reg [30:0]count ;
always @(posedge Clk)
begin
count = count + 1;
if (count==50000000)
begin clock1 = ~clock1;
count = 0;
end
end
endmodule
Nút rst của e dùng để reset,khi ấn rst thì 6 đèn led đều hiện thị số 0.Khi ấn nút Sw thì mạch bắt đầu đếm.
Comment