一、目标
1、显示时钟;数码管显示分钟和秒;使用开发板上LED显示小时和分钟的十分位
二、实现方法
1、基于秒表工程的分频以及LED的驱动;注意LED是低电平点亮。
2、基于数码管动态扫描的方式进行数码管驱动。
3、开发板上左边一排LED,对应的管脚号114,116,118,120,122,124,128,130,对应8位bit-0-7;右边一排LED对应的管脚为:113,115,117,119,121,123,126,129.对应8位bit-0-7;并且都是使用8421BCD码表示十进制的小时和分钟。
三、代码
主要看代码:
1、主要显示部分
/*
数码管时钟显示实验
4个数码管
*/
`define UD #1 //延时1个时间单位
module watch(
input sys_clk,//系统时钟
input sys_rst_n,
output [3:0]dig,//位选
output reg[7:0]smg,//数码管段码
output reg[7:0]Hour_led,//用于显示小时的led控制管脚
output reg[7:0]Minute_led//用于显示分钟的led控制管脚
);
wire clk_1hz; //变量
wire clk_1ms;
div_clk u_div_clk
(
.sys_clk(sys_clk),
.clk_1hz(clk_1hz),
.clk_1ms(clk_1ms)
);
reg [4:0]second_counter_ge = 5;//秒个位计时
reg [4:0]second_counter_shi = 3;//秒十位计时
reg [4:0]minute_counter = 6;//分计时个位
reg [4:0]minute_counter_shi = 5;//分计时十位
reg [4:0]hour_counter = 8;
reg [4:0]num = 0;//要显示的数字
always @(posedge clk_1hz)
begin
if(second_counter_ge == 4'd9) //秒个位加到9进位(使用除法,比较耗费资源)
begin
second_counter_ge <= `UD 4'd0;
second_counter_shi <= `UD second_counter_shi + 1'b1;
if(second_counter_shi == 4'd5) //秒十位加到5,并且个位到9,分钟加1
begin
second_counter_shi <= `UD 4'd0;
minute_counter <= `UD minute_counter + 1'b1;
if(minute_counter == 4'd9)
begin
// second_counter_shi <= 0;
minute_counter <= 0;
// second_counter_ge <= 0;
minute_counter_shi <= `UD minute_counter_shi + 1'b1;
if(minute_counter_shi == 4'd5)
begin
hour_counter <= `UD hour_counter + 1'b1;
minute_counter_shi <= 0;
if(hour_counter == 4'd11)
hour_counter <= 0;
end
end
end
end
else
second_counter_ge <= `UD second_counter_ge + 1'b1;
end
parameter smg_one = 4'b1110;
parameter smg_two = 4'b1101;
parameter smg_three = 4'b1011;
parameter smg_four = 4'b0111;
reg [3:0]sel ;
reg [3:0]disp_sel;
//使用状态机进行数码管扫描
always @(posedge clk_1ms or negedge sys_rst_n)
begin
if(!sys_rst_n)
begin
sel <= smg_one;
disp_sel = 4'b1110;
end
else
begin
case(sel)
smg_one:
begin
disp_sel <= 4'b1110;sel <= smg_two;num <= second_counter_ge;
end
smg_two:
begin
disp_sel <= 4'b1101;sel <= smg_three;num <= second_counter_shi;
end
smg_three:
begin
disp_sel <= 4'b1011;sel <= smg_four;num <= 4'd10;
end
smg_four:
begin
disp_sel <= 4'b0111;sel <= smg_one;num <= minute_counter;
end
default :sel <= smg_one;
endcase
end
end
//共阳极
assign dig = disp_sel; //显示一位
//0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80,0x90
//显示部分 共阳极,为0有效
always @(*)
begin
case(num)
4'd0:smg = 8'hc0;//数字0
4'd1:smg = 8'hf9;//数字1
4'd2:smg = 8'ha4;//数字2
4'd3:smg = 8'hb0;//数字3
4'd4:smg = 8'h99;//数字4
4'd5:smg = 8'h92;//数字5
4'd6:smg = 8'h82;//数字6
4'd7:smg = 8'hf8;//数字7
4'd8:smg = 8'h80;//数字8
4'd9:smg = 8'h90;//数字9
4'd10:smg = 8'hBF;//符号-
default:smg = 8'hc0;//数字0
endcase
end
//用于分钟LED显示
always @(*)
begin
case(minute_counter_shi)
4'd0:Minute_led = 8'hFF;//数字0
4'd1:Minute_led = 8'hFE;//数字1
4'd2:Minute_led = 8'hFD;//数字2
4'd3:Minute_led = 8'hFC;//数字3
4'd4:Minute_led = 8'hFB;//数字4
4'd5:Minute_led= 8'hFA;//数字5
default:Minute_led = 8'hFF;//数字0
endcase
end
//用于小时LED显示
always @(*)
begin
case(hour_counter)
4'd0:Hour_led = 8'hFF;//数字0
4'd1:Hour_led = 8'hFE;//数字1
4'd2:Hour_led = 8'hFD;//数字2
4'd3:Hour_led = 8'hFC;//数字3
4'd4:Hour_led = 8'hFB;//数字4
4'd5:Hour_led = 8'hFA;//数字5
4'd6:Hour_led = 8'hF9;//数字6
4'd7:Hour_led = 8'hF8;//数字7
4'd8:Hour_led = 8'hF7;//数字8
4'd9:Hour_led = 8'hF6;//数字9
4'd10:Hour_led = 8'hF5;//符号-
4'd11:Hour_led = 8'hF4;//符号-
default:Hour_led = 8'hFF;//数字0
endcase
end
endmodule
2、时钟分频部分
`define UD #1 //在引用已定义的宏名时,必须在宏名的前面加上符号“`”,表示该名字是一个经过宏定义的名字.
//宏定义不是Verilog HDL语句,不必在行末加分号。如果加了分号会连分号一起进行置换
//宏名和宏内容必须在同一行中进行声明。如果在宏内容中包含有注释行,注释行不会作为被置换的内容
module div_clk(
input sys_clk,//系统时钟 50Mhz
output clk_1hz,//1Hz时钟
output clk_1ms //1ms时钟
);
reg [25:0] time_cnt = 0;//用于保存分频计数值
reg [15:0] ms_cnt = 0; //用于保存分频计数值
reg flag = 0;//用于产生正负信号标志
reg ms_flag = 0;//用于产生正负信号标志
always @(posedge sys_clk)
begin
if(time_cnt == 26'h2faf080-1'b1)
time_cnt <= `UD 26'd0;
else
time_cnt <= `UD time_cnt + 1'b1;
end
always @(posedge sys_clk)
begin
if(ms_cnt == 16'hc350-1'b1)
ms_cnt <= `UD 16'd0;
else
ms_cnt <= `UD ms_cnt + 1'b1;
end
//分频,产生1Hz时钟
always @(posedge sys_clk)
begin
if(time_cnt == 26'h2faf080/2-1'b1)
flag <= `UD 1'b1;
else if(time_cnt == 26'h2faf080-1'b1)
flag <= `UD 1'b0;
end
//分频,产生1000Hz时钟
always @(posedge sys_clk)
begin
if(ms_cnt == 16'hc350/2-1'b1)
ms_flag <= `UD 1'b1;
else if(ms_cnt == 16'hc350-1'b1)
ms_flag <= `UD 1'b0;
end
assign clk_1ms = ms_flag;
assign clk_1hz = flag;
endmodule
四、实验现象
数码管显示6分-秒
左边LED显示 8、右边显示5。和程序中设定的初始值一样。
引用: littleshrimp 发表于 2021-12-17 16:24 感谢分享。
期待你下一篇精彩的测评
引用: littleshrimp 发表于 2021-12-17 19:39 正在弄,我是FPGA新手,写程序速度有些慢。
我也是小白一枚