效果:
QQ视频20231220151806
light:
`timescale 1ns / 1ps
module light(
input syck,
input rest_n,
output [7:0] dx,
output reg [3:0] wx
//input [13:0] number
);
reg [31:0] number ;
wire [31:0] numb;
reg [27:0] cnt2;
parameter delay2=125_000_000; //1s
//1s计时器
always@(posedge syck,negedge rest_n)begin
if(!rest_n)
cnt2<=0;
else if(cnt2==delay2-1)
cnt2<=0;
else
cnt2<=cnt2+1;
end
//led计数器
always@(posedge syck or negedge rest_n)begin
number <= 5940;
if(!rest_n)
number=10;
else if(cnt2==delay2-1)
number<=number+1;
else if (number == 5960)
number <= 0;
else
number<=number;
endreg [3:0] num;
assign numb = (cnt2==delay2-1)?number+1:number;
//状态编码
parameter idle = 5'b00001,
s1 = 5'b00010,
s2 = 5'b00100,
s3 = 5'b01000,
s4 = 5'b10000;
//内部声明
reg [4:0] cur_state,
next_state;
parameter delay1 = 125000;
reg [31:0] cnt1;
//1ms计时器
always@(posedge syck,negedge rest_n)begin
if(!rest_n)
cnt1<=0;
else if(cnt1==delay1-1)
cnt1<=0;
else
cnt1<=cnt1+1;
end
//二段式状态机 描述总状态跳转,现态,次态
always@(posedge syck or negedge rest_n)begin
if(!rest_n)
cur_state<=idle;
else
cur_state<=next_state;
end
always@(posedge syck or negedge rest_n)begin
if(!rest_n)
begin
wx<=4'b1111;
num<=8;
next_state<=idle;
end
else case(cur_state)
idle:
next_state<=s1;
s1:
if(cnt1==delay1-1)
begin
next_state<=s2;
wx<=4'b1110;
num<=numb%10;
end
else
next_state<=s1;
s2:
if(cnt1==delay1-1)
begin
next_state<=s3;
wx<=4'b1101;
num<=numb/10%10;
end
else
next_state<=s2;
s3:
if(cnt1==delay1-1)
begin
next_state<=s4;
wx<=4'b1011;
num<=numb/100%10;
end
else
next_state=s3;
s4:
if(cnt1==delay1-1)
begin
next_state<=s1;
wx<=4'b0111;
num<=numb/1000%10;
end
else
next_state=s4;
default:
next_state=idle;
endcase
end
yima yima_u(
. syck (syck) ,
. rest_n (rest_n) ,
. seg (dx) ,
. num (num)
);
endmodule
译码:
`timescale 1ns / 1ps
//
// Company:
// Engineer:
//
// Create Date: 2023/12/15 15:31:29
// Design Name:
// Module Name: yima
// Project Name:
// Target Devices:
// Tool Versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//
module yima(
input syck,
input rest_n,
output reg [7:0] seg,
input [3:0] num
);
always@(*)begin
case(num)
0:seg<=8'b0011_1111;
1:seg<=8'b0000_0110;
2:seg<=8'b0101_1011;
3:seg<=8'b0100_1111;
4:seg<=8'b0110_0110;
5:seg<=8'b0110_1101;
6:seg<=8'b0111_1101;
7:seg<=8'b0000_0111;
8:seg<=8'b0111_1111;
9:seg<=8'b0110_1111;
default:seg<=8'b1111_1111;
endcase
end
endmodule
连接配置:
https://img-blog.csdnimg.cn/direct/f052a7e4ed714588b70c5bef644d76a8.png