verilog 实现的数字时钟
verilog 实现的数字时钟/*
本实验实现一个能显示小时,分钟,秒的数字时钟。
*/
module clock(clk,rst,dataout,en);
input clk,rst;
output dataout;
reg dataout;
output en;
reg en;
reg dataout_buf;
reg cnt;
reg cnt_scan;
reg dataout_code;
wire cal; //各级进位标志
assign cal=(dataout_buf==9)?1:0;
assign cal=(cal&&dataout_buf==5)?1:0;
assign cal=(cal&&dataout_buf==9)?1:0;
assign cal=(cal&&dataout_buf==5)?1:0;
assign cal=(cal&&dataout_buf==9)?1:0;
assign cal=(cal&&dataout_buf==2&&dataout_buf==1)?1:0;
always@(posedge clk or negedge rst)
begin
if(!rst) begin
cnt_scan<=0;
en<=8'b1111_1110;
end
else begin
cnt_scan<=cnt_scan+1;
if(cnt_scan==16'hffff) begin
en<=en;
en<=en;
end
end
end
always@(*)
begin
case(en)
8'b1111_1110:
dataout_code=dataout_buf;
8'b1111_1101:
dataout_code=dataout_buf;
8'b1111_1011:
dataout_code=dataout_buf;
8'b1111_0111:
dataout_code=dataout_buf;
8'b1110_1111:
dataout_code=dataout_buf;
8'b1101_1111:
dataout_code=dataout_buf;
8'b1011_1111:
dataout_code=dataout_buf;
8'b0111_1111:
dataout_code=dataout_buf;
default:
dataout_code=dataout_buf;
endcase
end
always@(posedge clk or negedge rst)
begin
if(!rst)
cnt<=0;
else if(cnt!=40000000)
cnt<=cnt+1;
else
cnt<=0;
end
always@(posedge clk or negedge rst)//实现计数和进位的功能
begin
if(!rst) begin
dataout_buf<=0;
dataout_buf<=0;
dataout_buf<=15;
dataout_buf<=0;
dataout_buf<=0;
dataout_buf<=15;
dataout_buf<=2;
dataout_buf<=1;
end
else begin
if(cnt==26'd40000000) begin
if(!cal)
dataout_buf<=dataout_buf+1;
else begin
dataout_buf<=0;
if(!cal)
dataout_buf<=dataout_buf+1;
else begin
dataout_buf<=0;
if(!cal)
dataout_buf<=dataout_buf+1;
else begin
dataout_buf<=0;
if(!cal)
dataout_buf<=dataout_buf+1;
else begin
dataout_buf<=0;
if(!cal)
dataout_buf<=dataout_buf+1;
else begin
dataout_buf<=0;
if(!cal)
dataout_buf<=dataout_buf+1;
else
dataout_buf<=0;
end
end
end
end
end
end
end
end
always@(dataout_code)
begin
case(dataout_code)
4'b0000:
dataout=8'b0000_0011;
4'b0001:
dataout=8'b1001_1111;
4'b0010:
dataout=8'b0010_0101;
4'b0011:
dataout=8'b0000_1101;
4'b0100:
dataout=8'b1001_1001;
4'b0101:
dataout=8'b0100_1001;
4'b0110:
dataout=8'b0100_0001;
4'b0111:
dataout=8'b0001_1111;
4'b1000:
dataout=8'b0000_0001;
4'b1001:
dataout=8'b0001_1001;
4'b1010:
dataout=8'b0001_0001;
4'b1011:
dataout=8'b1100_0001;
4'b1100:
dataout=8'b0110_0011;
4'b1101:
dataout=8'b1000_0101;
4'b1110:
dataout=8'b0110_0001;
4'b1111:
dataout=8'b1111_1110;
endcase
end
endmodule 数字时钟,不错
页:
[1]