【作业要求】
1、练习:数码管时钟由两个按键分别控制两种不同的模式,按键一控制简易数码管秒表模式,按键二控制数码管闹钟模式,设置闹钟。根据题意画出数据模块的状态转化图和整个实验的模块框图?
2、练习:数码管时钟由两个按键分别控制两种不同的模式,按键一控制简易数码管秒表模式,按键二控制数码管闹钟模式,设置闹钟。根据题意写出数据模块的详细代码?
【我的做法】
module top_clock(
input sysclk, // 系统主时钟 50MHz
input rst_n, // 全局复位 低电平有效
input key_1, // 按键1:秒表启停
input key_2, // 按键2:闹钟开关
output [7:0] seg, // 数码管段选 dp-g-f-e-d-c-b-a
output [3:0] sel // 数码管位选 4位共阴
);
//==================== 内部连线声明 ====================
// 按键消抖输出
wire flag_1;
wire flag_2;
// 时钟分频输出
wire sec_pulse;
wire scan_pulse;
wire flash_pulse;
// 计时器输出
wire [7:0] min;
wire [7:0] sec;
// 闹钟模块输出
wire alarm_en;
wire flash_G;
//==================== 1. 按键消抖模块 ====================
key_filter key_filter_u(
.sysclk (sysclk),
.rst_n (rst_n),
.key_1 (key_1),
.key_2 (key_2),
.flag_1 (flag_1),
.flag_2 (flag_2)
);
//==================== 2. 时钟分频模块 ====================
clk_div clk_div_u(
.sysclk (sysclk),
.rst_n (rst_n),
.sec_pulse (sec_pulse),
.scan_pulse (scan_pulse),
.flash_pulse(flash_pulse)
);
//==================== 3. 分秒计时器模块 ====================
time_counter time_counter_u(
.sysclk (sysclk),
.rst_n (rst_n),
.sec_pulse (sec_pulse),
.flag_1 (flag_1),
.min (min),
.sec (sec)
);
//==================== 4. 闹钟控制模块 ====================
alarm alarm_u(
.sysclk (sysclk),
.rst_n (rst_n),
.flag_2 (flag_2),
.flash_pulse(flash_pulse),
.alarm_en (alarm_en),
.flash_G (flash_G)
);
//==================== 5. 数码管驱动模块 ====================
SMG_dirve SMG_dirve_u(
.sysclk (sysclk),
.rst_n (rst_n),
.scan_pulse (scan_pulse),
.min (min),
.sec (sec),
.flash_G (flash_G),
.alarm_en (alarm_en),
.seg (seg),
.sel (sel)
);
endmodule
module key_filter(
input sysclk ,
input rst_n ,
input key_1 ,
input key_2 ,
output reg flag_1 ,//按键一消抖完成信号
output reg flag_2 //按键二消抖完成信号
);
parameter delay = 16'd50_000 ;//按键1MS的消抖时间
reg [15:0] cnt_1 , cnt_2 ;
//按键一消抖
always @(posedge sysclk or negedge rst_n)
begin
if(!rst_n)
begin
cnt_1 <=16'd0;
end
else if(key_1 == 1'b0)
begin //按键按下
if(cnt_1 == delay - 1) //计时器开始工作
cnt_1 <=cnt_1;
else
cnt_1 <=cnt_1 + 16'd1;
end
else
begin
cnt_1 <=16'd0; //按键松开
end
end
//按键2消抖
always @(posedge sysclk or negedge rst_n)
begin
if(!rst_n)
begin
cnt_2 <=16'd0;
end
else if(key_2 == 1'b0)
begin
if(cnt_2 == delay - 1)
cnt_2 <=cnt_2 ;
else
cnt_2 <=cnt_2 + 16'd1 ;
end
else
begin
cnt_2 <=16'd0 ;
end
end
//给消抖脉冲赋值
always @(posedge sysclk or negedge rst_n)
begin
if(!rst_n)
begin
flag_1 <= 1'b0;
flag_2 <= 1'b0;
end
else
begin
flag_1 <=(cnt_1 == delay - 2)?1'b1:1'b0;
flag_2 <=(cnt_2 == delay - 2)?1'b1:1'b0;
end
end
endmodule
module clk_div(
input sysclk ,//系统时钟
input rst_n ,//系统复位
output reg sec_pulse ,//1秒信号
output reg scan_pulse ,//扫描信号
output reg flash_pulse //闪烁信号
);
reg [25:0] cnt_1hz ; //1秒节拍
reg [15:0] cnt_1khz ; //1khz扫描节拍
reg [24:0] cnt_2hz ; //2hz闪烁方波
//1秒计时器
always @(posedge sysclk or negedge rst_n)
begin
if(!rst_n)
begin
cnt_1hz <= 26'd0 ;//计数器清零
sec_pulse <= 1'b0 ;//使能信号不工作
end
else if(cnt_1hz == 26'd49_999_999)
begin
cnt_1hz <= 26'd0 ;//计数器清零
sec_pulse <= 1'b1 ;//使能信号输出高电平
end
else
begin
cnt_1hz <= cnt_1hz + 26'd1 ;
sec_pulse <= 1'b0 ;
end
end
//1khz扫描节拍
always @(posedge sysclk or negedge rst_n)
begin
if(!rst_n)
begin
cnt_1khz <= 16'd0 ;//计数器清零
scan_pulse <= 1'b0 ;//使能信号不工作
end
else if(cnt_1khz == 16'd49_999)
begin
cnt_1khz <= 16'd0 ;//计数器清零
scan_pulse <= 1'b1 ;//使能信号输出高电平
end
else
begin
cnt_1khz <= cnt_1khz + 16'd1 ;
scan_pulse <= 1'b0 ;
end
end
// 2HZ闪烁方波
always @(posedge sysclk or negedge rst_n)
begin
if(!rst_n)
begin
cnt_2hz <= 25'd0 ;
flash_pulse <= 1'b0 ;
end
else if(cnt_2hz == 25'd12_499_999)
begin //高、低电平各持续 0.25s,完整方波周期 0.5s,对应频率 2Hz
cnt_2hz <= 25'd0 ;
flash_pulse <= ~flash_pulse ;
end
else
begin
cnt_2hz <= cnt_2hz + 25'd1 ;//闪烁时间只有一个周期20NS
end
end
endmodule
module time_counter(
input sysclk,
input rst_n ,
input sec_pulse, //1秒信号
input flag_1 , //新增秒表启停使能
output reg [7:0] min, /*分位,高4位[7:4]为十位,低4位[3:0]为个位*/
output reg [7:0] sec /*秒位,高4位[7:4]为十位,低4位[3:0]为个位*/
);
reg time_en;//秒表开关信号reg
reg sec_pulse_d; // 打一拍寄存器,缓存上一个时钟周期的sec_pulse状态
wire sec_pulse_pos = sec_pulse && !sec_pulse_d; // 上升沿检测组合逻辑
wire min_add = (sec == 8'h59) && sec_pulse_pos && time_en;
/*分位运行条件,当秒位高低4位分别显示59时并且一秒信号来临,才进行跳转*/
//触发条件改成sec_pulse_pos原来是触发条件改成sec_pulse
// 作用:生成延迟1个系统时钟的信号sec_pulse_d,与原信号组合实现上升沿检测,规避亚稳态
always @(posedge sysclk or negedge rst_n)
begin
if(!rst_n)
begin
sec_pulse_d <= 1'b0;
end
else
begin
sec_pulse_d <= sec_pulse;
end
end
//-------秒计数-------
always @(posedge sysclk or negedge rst_n)
begin
if(!rst_n)
begin
sec <= 8'h00;//复位时,秒清零
end
else if(sec_pulse_pos && time_en)
begin //触发条件改成sec_pulse_pos
if(sec[3:0] >= 4'd9)
begin /*判断低4位是否为9*//*原来是:==,现在改成:>=之前有溢出*/
sec[3:0] <= 4'd0;
if(sec[7:4] >= 4'd5)
begin /*判断高4位是否为5*/
sec[7:4] <= 4'd0;
end
else
begin
sec[7:4] <= sec[7:4] + 4'd1;
end
end
else
begin
sec[3:0] <= sec[3:0] + 4'd1;
end
end
end
//------分计数------
always @(posedge sysclk or negedge rst_n)
begin
if(!rst_n)
begin
min <= 8'h00;//复位时,分清零
end
else if(min_add)
begin
if(min[3:0] >= 4'd9)
begin
min[3:0] <= 4'd0;
if(min[7:4] >= 4'd5)
begin
min[7:4] <= 4'd0;
end
else
begin
min[7:4] <= min[7:4] + 4'd1;
end
end
else
begin
min[3:0] <= min[3:0] + 4'd1;
end
end
end
//秒表启停功能
always @(posedge sysclk or negedge rst_n)
begin
if(!rst_n)
begin
time_en <= 1'b0;
end
else if(flag_1)
begin
time_en <= ~time_en;
end
end
endmodule
module alarm(
input sysclk,
input rst_n ,
input flag_2, //按键二消抖完成信号
input flash_pulse,/*2hz闪烁信号*/
output reg alarm_en, /*闪烁使能*/
output flash_G /*按键按下数码管中间段闪烁信号*/
);
//------收到信号翻转开关
always @(posedge sysclk or negedge rst_n)
begin
if(!rst_n)
begin
alarm_en <= 1'b0;
end
else if(flag_2)
begin
alarm_en <= ~alarm_en;
end
end
//二选一多路器
assign flash_G = alarm_en ? flash_pulse:1'b0;
endmodule
module SMG_dirve(
input sysclk ,
input rst_n ,
input scan_pulse ,//1khz扫描
input [7:0] min ,//分位
input [7:0] sec ,//秒位
input flash_G ,//按键按下数码管中间段闪烁信号
input alarm_en ,//闹钟闪烁使能
output reg [7:0] seg ,//数码管段选
output reg [3:0] sel //数码管位选
);
//使用计数器描述现态和次态
reg [1:0] scan_cnt;
always @(posedge sysclk or negedge rst_n)
begin
if(!rst_n)
begin
scan_cnt <= 2'd0;
end
else if(scan_pulse)
begin
scan_cnt <= scan_cnt + 1'b1;
end
end
//------位选信号
always @(*)
begin
case(scan_cnt)
2'd0: sel = 4'b0111;// 最左侧:分十位
2'd1: sel = 4'b1011;// 左数第二位:分个位
2'd2: sel = 4'b1101;// 左数第三位:秒十位
2'd3: sel = 4'b1110;// 最右侧:秒个位
default: sel = 4'b1111;
endcase
end
//------段选信号
reg [3:0] num;//显示数字
always @(*)
begin
case(scan_cnt)
2'd0: num = min[7:4];// 最左侧:分十位
2'd1: num = min[3:0];// 左数第二位:分个位
2'd2: num = sec[7:4]; // 左数第三位:秒十位
2'd3: num = sec[3:0]; // 最右侧:秒个位
default: num = 4'b0000;
endcase
end
//-------数码管译码(dp g f e d c b a)
reg [7:0] num_1;
always @(*)
begin
case(num)
4'd0: num_1 = 8'b00111111;
4'd1: num_1 = 8'b00000110;
4'd2: num_1 = 8'b01011011;
4'd3: num_1 = 8'b01001111;
4'd4: num_1 = 8'b01100110;
4'd5: num_1 = 8'b01101101;
4'd6: num_1 = 8'b01111101;
4'd7: num_1 = 8'b00000111;
4'd8: num_1 = 8'b01111111;
4'd9: num_1 = 8'b01101111;
default:num_1 = 8'b00000000;
endcase
end
//------闹钟信号为G段数码管闪烁
always @(*)
begin
if(alarm_en)
begin
seg = {1'b0 , flash_G , 6'b000000};
end
else
begin
seg = num_1;
end
end
endmodule【结果/时序图/实验现象(可视频)】

评论 0