目錄
概述
設(shè)計(jì)功能
數(shù)字秒表設(shè)計(jì)的目的
模塊仿真
設(shè)計(jì)代碼
概述
該設(shè)計(jì)是用于體育比賽的數(shù)字秒表,基于FPGA在Quartus II 9.0sp2軟件下應(yīng)用VHDL語(yǔ)言編寫(xiě)程序,采用ALTRA公司CycloneII系列的EP2C8Q208芯片進(jìn)行了計(jì)算機(jī)仿真,并給出了相應(yīng)的仿真結(jié)果。本設(shè)計(jì)有效的克服了傳統(tǒng)的數(shù)字秒表的缺點(diǎn)采用EDA技術(shù)采取自上而下的設(shè)計(jì)思路。繪制出了具體的邏輯電路,最后又通過(guò)硬件上對(duì)其進(jìn)行調(diào)試和驗(yàn)證。該電路能夠?qū)崿F(xiàn)很好的計(jì)時(shí)功能,計(jì)時(shí)精度高,最長(zhǎng)計(jì)時(shí)時(shí)間可達(dá)一個(gè)小時(shí)。?
設(shè)計(jì)功能
1、完成一個(gè)帶有時(shí)分秒顯示的數(shù)字秒表;?
2、12、24小時(shí)可以調(diào)節(jié);
3、能作秒表計(jì)時(shí);
4、能夠倒計(jì)時(shí)顯示;
數(shù)字秒表設(shè)計(jì)的目的
本次設(shè)計(jì)的目的就是在掌握EDA實(shí)驗(yàn)開(kāi)發(fā)系統(tǒng)的初步使用基礎(chǔ)上,了解EDA技術(shù),對(duì)計(jì)算機(jī)系統(tǒng)中時(shí)鐘控制系統(tǒng)進(jìn)一步了解,掌握狀態(tài)機(jī)工作原理,同時(shí)了解計(jì)算機(jī)時(shí)鐘脈沖是怎么產(chǎn)生和工作的。(此處省略)
模塊仿真
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-754133.html
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-754133.html
設(shè)計(jì)代碼
(代碼較長(zhǎng),完整工程免費(fèi)分享,需要獲取可關(guān)注:FPGA苑)
module time_clock(
clk,
reset_n,
hour_select_key,
second_counter_key,
second_countdown_key,
pause_key,
duan,
wei
);
input clk; //clk:50MHZ時(shí)鐘輸入;
input reset_n; //復(fù)位信號(hào)輸入,低電平有效;
input hour_select_key; //12、24小時(shí)可以調(diào)節(jié)按鍵,當(dāng)為‘1’時(shí)為24,‘0’時(shí)為12小時(shí);
input second_counter_key; //當(dāng)該按鍵為‘1’時(shí)為秒表計(jì)時(shí)功能,‘0’時(shí)為正常功能;
input second_countdown_key; //當(dāng)該按鍵為‘1’時(shí)為倒計(jì)時(shí)功能,‘0’時(shí)為正常功能;
input pause_key; //暫停功能按鍵,進(jìn)行秒表計(jì)時(shí)和倒計(jì)時(shí)時(shí)可以通過(guò)該按鍵進(jìn)行暫停,‘1’暫停,‘0’繼續(xù)
output [7:0] duan; //duan:數(shù)碼管段碼;
output [7:0] wei; //wei:數(shù)碼管位碼;
reg [7:0] duan; //duan:數(shù)碼管段碼;
reg [7:0] wei; //wei:數(shù)碼管位碼;
reg [24:0] count; //1HZ時(shí)鐘計(jì)數(shù)器
reg [13:0] count2; //掃描時(shí)鐘計(jì)數(shù)器
reg clk_1hz; //1HZ時(shí)鐘信號(hào)
reg [3:0] miao_ge; //秒個(gè)位數(shù)BCD碼
reg [2:0] miao_shi; //秒十位BCD二進(jìn)制碼
reg [3:0] fen_ge; //分鐘個(gè)位數(shù)
reg [2:0] fen_shi; //分鐘十位數(shù)
reg [1:0] shi_ge; //時(shí)鐘個(gè)位數(shù)
reg [1:0] shi_shi; //時(shí)鐘十位數(shù)
reg [1:0] shi_select_ge; //時(shí)鐘選擇個(gè)位數(shù),用于調(diào)節(jié)時(shí)制
reg [1:0] shi_select_shi; //時(shí)鐘選擇十位數(shù),用于調(diào)節(jié)時(shí)制
reg clk_scan; //數(shù)碼管掃描時(shí)鐘
reg [2:0] select; //用于掃描時(shí)選擇顯示位碼
//****************************************************************************************************
// 模塊名稱:秒時(shí)鐘分頻模塊
// 功能描述:
//****************************************************************************************************
always @(posedge clk or negedge reset_n)//1HZ時(shí)鐘進(jìn)程
begin
if(reset_n == 1'b0)
begin
count <= 25'd0;
clk_1hz <= 1'b0;
end
else
if(count == 25'd25000000)
begin
clk_1hz <= ~clk_1hz;
count <= 25'd0;
end
else
count <= count + 1'b1;
end
reg [17:0] counter_count;
reg clk_100hz;
always @(posedge clk or negedge reset_n)//100HZ時(shí)鐘進(jìn)程
begin
if(reset_n == 1'b0)
begin
counter_count <= 18'd0;
clk_100hz <= 1'b0;
end
else
if(~pause_key == 1'b0)
if(counter_count == 18'd250000)
begin
clk_100hz <= ~clk_100hz;
counter_count <= 18'd0;
end
else
counter_count <= counter_count + 1'b1;
else
begin
clk_100hz <= 1'b0;
counter_count <= 18'd0;
end
end
//****************************************************************************************************
// 模塊名稱:計(jì)時(shí)調(diào)節(jié)模塊
// 功能描述:
//****************************************************************************************************
always @(posedge clk or negedge reset_n)//1HZ時(shí)鐘進(jìn)程
begin
if(reset_n == 1'b0)
begin
shi_select_ge <= 2'd3;
shi_select_shi <= 2'd2;
end
else
if(hour_select_key == 1'b1)//12、24小時(shí)可以調(diào)節(jié)按鍵,當(dāng)為‘1’時(shí)為24,‘0’時(shí)為12小時(shí)
begin
shi_select_ge <= 2'd3;
shi_select_shi <= 2'd2;
end
else
begin
shi_select_ge <= 2'd1;
shi_select_shi <= 2'd1;
end
end
//****************************************************************************************************
// 模塊名稱:秒計(jì)時(shí)模塊
// 功能描述:
//****************************************************************************************************
always @(posedge clk_1hz or negedge reset_n)//秒分時(shí)各位累加功能進(jìn)程
begin
if(reset_n == 1'b0)
begin
miao_ge <= 4'd9;
miao_shi <= 3'd5;
fen_ge <= 4'd9;
fen_shi <= 3'd5;
shi_ge <= 2'd3;
shi_shi <= 2'd2;
end
else
if(miao_ge ==4'd9)
begin
miao_ge <= 4'd0;
if(miao_shi == 3'd5)
begin
miao_shi <= 3'd0;
if(fen_ge == 4'd9)
begin
fen_ge <= 4'd0;
if(fen_shi == 3'd5)
begin
fen_shi <= 3'd0;
if(shi_ge == shi_select_ge)//24小時(shí)制對(duì)應(yīng)3,12小時(shí)制則對(duì)應(yīng)1
begin
shi_ge <= 2'd0;
if(shi_shi == shi_select_shi)//24小時(shí)制對(duì)應(yīng)2,12小時(shí)制則對(duì)應(yīng)1
shi_shi <= 2'd0;
else
shi_shi <= shi_shi + 1'b1;
end
else
shi_ge <= shi_ge + 1'b1;
end
else
fen_shi <= fen_shi + 1'b1;
end
else
fen_ge <= fen_ge + 1'b1;
end
else
miao_shi <= miao_shi + 1'b1;
end
else
miao_ge <= miao_ge + 1'b1;
end
//****************************************************************************************************
// 模塊名稱:數(shù)碼管位選擇時(shí)鐘產(chǎn)生模塊
// 功能描述:
//****************************************************************************************************
always @(posedge clk or negedge reset_n)//數(shù)碼管掃描時(shí)鐘產(chǎn)生進(jìn)程
begin
if(reset_n == 1'b0)
begin
count2 <= 14'd0;
clk_scan <= 1'b0;
end
else
if(count2 == 14'd10000)
begin
count2 <= 14'd0;
clk_scan <= ~clk_scan;
end
else
count2 <= count2 + 1'b1;
end
//****************************************************************************************************
// 模塊名稱:數(shù)碼管位選擇產(chǎn)生信號(hào)模塊
// 功能描述:
//****************************************************************************************************
always @(posedge clk_scan or negedge reset_n)
begin
if(reset_n == 1'b0)
select <= 3'b000;
else
select <= select + 1'b1;
end
//****************************************************************************************************
// 模塊名稱:秒表計(jì)時(shí)
// 功能描述:秒表計(jì)時(shí)
//****************************************************************************************************
reg [3:0] counter_haomiao_ge; //秒表計(jì)時(shí),毫秒個(gè)位數(shù)BCD碼
reg [3:0] counter_haomiao_shi; //秒表計(jì)時(shí),毫秒十位數(shù)BCD碼
reg [3:0] counter_miao_ge; //秒表計(jì)時(shí),秒個(gè)位數(shù)BCD碼
reg [2:0] counter_miao_shi; //秒表計(jì)時(shí),秒十位BCD二進(jìn)制碼
reg [3:0] counter_fen_ge; //秒表計(jì)時(shí),分鐘個(gè)位數(shù)
reg [2:0] counter_fen_shi; //秒表計(jì)時(shí),分鐘十位數(shù)
reg [1:0] counter_shi_ge; //秒表計(jì)時(shí),時(shí)鐘個(gè)位數(shù)
reg [1:0] counter_shi_shi; //秒表計(jì)時(shí),時(shí)鐘十位數(shù)
always @(posedge clk_100hz or negedge reset_n)
begin
if(reset_n == 1'b0)
begin
counter_haomiao_ge <= 4'd0;
counter_haomiao_shi <= 4'd0;
counter_miao_ge <= 4'd0;
counter_miao_shi <= 3'd0;
counter_fen_ge <= 4'd0;
counter_fen_shi <= 3'd0;
counter_shi_ge <= 2'd0;
counter_shi_shi <= 2'd0;
end
else
if(~second_counter_key == 1'b1)
if(counter_haomiao_ge == 4'd9)
begin
counter_haomiao_ge <= 4'd0;
if(counter_haomiao_shi == 4'd9)
begin
counter_haomiao_shi <= 4'd0;
if(counter_miao_ge ==4'd9)
begin
counter_miao_ge <= 4'd0;
if(counter_miao_shi == 3'd5)
begin
counter_miao_shi <= 3'd0;
if(counter_fen_ge == 4'd9)
begin
counter_fen_ge <= 4'd0;
if(counter_fen_shi == 3'd5)
begin
counter_fen_shi <= 3'd0;
if(counter_shi_ge == 2'd3)//24小時(shí)制對(duì)應(yīng)3,12小時(shí)制則對(duì)應(yīng)1
begin
counter_shi_ge <= 2'd0;
if(counter_shi_shi == 2'd2)//24小時(shí)制對(duì)應(yīng)2,12小時(shí)制則對(duì)應(yīng)1
counter_shi_shi <= 2'd0;
else
counter_shi_shi <= counter_shi_shi + 1'b1;
end
else
counter_shi_ge <= counter_shi_ge + 1'b1;
end
else
counter_fen_shi <= counter_fen_shi + 1'b1;
end
else
counter_fen_ge <= counter_fen_ge + 1'b1;
end
else
counter_miao_shi <= counter_miao_shi + 1'b1;
end
else
counter_miao_ge <= counter_miao_ge + 1'b1;
end
else
counter_haomiao_shi <= counter_haomiao_shi + 1'b1;
end
else
counter_haomiao_ge <= counter_haomiao_ge + 1'b1;
else
begin
counter_haomiao_ge <= 4'd0;
counter_haomiao_shi <= 4'd0;
counter_miao_ge <= 4'd0;
counter_miao_shi <= 3'd0;
counter_fen_ge <= 4'd0;
counter_fen_shi <= 3'd0;
counter_shi_ge <= 2'd0;
counter_shi_shi <= 2'd0;
end
end
//****************************************************************************************************
// 模塊名稱:倒計(jì)時(shí)模塊
// 功能描述:
//****************************************************************************************************
reg [3:0] countdown_haomiao_ge; //倒計(jì)時(shí),毫秒個(gè)位數(shù)BCD碼
reg [3:0] countdown_haomiao_shi; //倒計(jì)時(shí),毫秒十位數(shù)BCD碼
reg [3:0] countdown_miao_ge; //倒計(jì)時(shí),秒個(gè)位數(shù)BCD碼
reg [2:0] countdown_miao_shi; //倒計(jì)時(shí),秒十位BCD二進(jìn)制碼
always @(posedge clk_100hz or negedge reset_n)//秒分時(shí)各位累加功能進(jìn)程
begin
if(reset_n == 1'b0)
begin
countdown_haomiao_ge <= 4'd9;
countdown_haomiao_shi <= 4'd9;
countdown_miao_ge <= 4'd9;
countdown_miao_shi <= 3'd5;
end
else
if(~second_countdown_key == 1'b1)
if(countdown_haomiao_ge == 4'd0)
begin
countdown_haomiao_ge <= 4'd9;
if(countdown_haomiao_shi == 4'd0)
begin
countdown_haomiao_shi <= 4'd9;
if(countdown_miao_ge ==4'd0)
begin
countdown_miao_ge <= 4'd9;
if(countdown_miao_shi == 3'd0)
begin
countdown_miao_shi <= 3'd5;
end
else
countdown_miao_shi <= countdown_miao_shi - 1'b1;
end
else
countdown_miao_ge <= countdown_miao_ge - 1'b1;
end
else
countdown_haomiao_shi <= countdown_haomiao_shi - 1'b1;
end
else
countdown_haomiao_ge <= countdown_haomiao_ge - 1'b1;
else
begin
countdown_haomiao_ge <= 4'd9;
countdown_haomiao_shi <= 4'd9;
countdown_miao_ge <= 4'd9;
countdown_miao_shi <= 3'd5;
end
end
//****************************************************************************************************
// 模塊名稱:功能選擇模塊
// 功能描述:
//****************************************************************************************************
reg [3:0] reg_haomiao_ge; //毫秒個(gè)位數(shù)BCD碼
reg [3:0] reg_haomiao_shi; //毫秒十位數(shù)BCD碼
reg [3:0] reg_miao_ge; //秒個(gè)位數(shù)BCD碼
reg [2:0] reg_miao_shi; //秒十位BCD二進(jìn)制碼
reg [3:0] reg_fen_ge; //分鐘個(gè)位數(shù)
reg [2:0] reg_fen_shi; //分鐘十位數(shù)
reg [3:0] reg_shi_ge; //時(shí)鐘個(gè)位數(shù)
reg [1:0] reg_shi_shi; //時(shí)鐘十位數(shù)
always @(posedge clk or negedge reset_n)
begin
if(reset_n == 1'b0)
begin
reg_haomiao_ge <= 4'd9;
reg_haomiao_shi <= 4'd9;
reg_miao_ge <= 4'd9;
reg_miao_shi <= 3'd5;
reg_fen_ge <= 4'd9;
reg_fen_shi <= 3'd5;
reg_shi_ge <= 4'd3;
reg_shi_shi <= 2'd2;
end
else
begin
case({~second_counter_key,~second_countdown_key})
2'b10://秒表功能
begin
reg_haomiao_ge <= counter_haomiao_ge;
reg_haomiao_shi <= counter_haomiao_shi;
reg_miao_ge <= counter_miao_ge;
reg_miao_shi <= counter_miao_shi;
reg_fen_ge <= counter_fen_ge;
reg_fen_shi <= counter_fen_shi;
reg_shi_ge <= counter_shi_ge;
reg_shi_shi <= counter_shi_shi;
end
2'b01://倒計(jì)時(shí)功能
begin
reg_haomiao_ge <= countdown_haomiao_ge;
reg_haomiao_shi <= countdown_haomiao_shi;
reg_miao_ge <= countdown_miao_ge;
reg_miao_shi <= countdown_miao_shi;
reg_fen_ge <= 4'hf; //不進(jìn)行顯示
reg_fen_shi <= 3'b111; //不進(jìn)行顯示
reg_shi_ge <= 4'b1111; //不進(jìn)行顯示
reg_shi_shi <= 2'b11; //不進(jìn)行顯示
end
default://正常功能,顯示時(shí)分秒
begin
reg_haomiao_ge <= 4'hf; //不進(jìn)行顯示
reg_haomiao_shi <= 4'hf; //不進(jìn)行顯示
reg_miao_ge <= miao_ge;
reg_miao_shi <= miao_shi;
reg_fen_ge <= fen_ge;
reg_fen_shi <= fen_shi;
reg_shi_ge <= shi_ge;
reg_shi_shi <= shi_shi;
end
endcase
end
end
//****************************************************************************************************
// 模塊名稱:譯碼電路
// 功能描述:
//****************************************************************************************************
always @(posedge clk)//敏感信號(hào)列表reg_haomiao_ge or miao_ge or miao_shi or fen_ge or fen_shi or shi_ge or shi_shi or select
begin
if(select == 3'd0)
begin
wei <= 8'b11111110;//毫秒個(gè)位數(shù)顯示
case(reg_haomiao_ge)
4'b0000:duan <= 8'b1100_0000;//0
4'b0001:duan <= 8'b1111_1001;//1
4'b0010:duan <= 8'b1010_0100;//2
4'b0011:duan <= 8'b1011_0000;//3
4'b0100:duan <= 8'b1001_1001;//4
4'b0101:duan <= 8'b1001_0010;//5
4'b0110:duan <= 8'b1000_0010;//6
4'b0111:duan <= 8'b1111_1000;//7
4'b1000:duan <= 8'b1000_0000;//8
4'b1001:duan <= 8'b1001_0000;//9
default:duan <= 8'hff;
endcase
end
else if(select == 3'd1)
begin
wei <= 8'b11111101;//毫秒十位數(shù)顯示
case(reg_haomiao_shi)
4'b0000:duan <= 8'b1100_0000;//0
4'b0001:duan <= 8'b1111_1001;//1
4'b0010:duan <= 8'b1010_0100;//2
4'b0011:duan <= 8'b1011_0000;//3
4'b0100:duan <= 8'b1001_1001;//4
4'b0101:duan <= 8'b1001_0010;//5
4'b0110:duan <= 8'b1000_0010;//6
4'b0111:duan <= 8'b1111_1000;//7
4'b1000:duan <= 8'b1000_0000;//8
4'b1001:duan <= 8'b1001_0000;//9
default:duan <= 8'hff;
endcase
end
else if(select == 3'd2)
begin
wei <= 8'b11111011;//秒個(gè)位數(shù)顯示
case(reg_miao_ge)
4'b0000:duan <= 8'b1100_0000;//0
4'b0001:duan <= 8'b1111_1001;//1
4'b0010:duan <= 8'b1010_0100;//2
4'b0011:duan <= 8'b1011_0000;//3
4'b0100:duan <= 8'b1001_1001;//4
4'b0101:duan <= 8'b1001_0010;//5
4'b0110:duan <= 8'b1000_0010;//6
4'b0111:duan <= 8'b1111_1000;//7
4'b1000:duan <= 8'b1000_0000;//8
4'b1001:duan <= 8'b1001_0000;//9
default:duan <= 8'hff;
endcase
end
else if(select == 3'd3)
begin
wei <= 8'b11110111;//秒十位數(shù)顯示
case(reg_miao_shi)
3'b000:duan <= 8'b1100_0000;
3'b001:duan <= 8'b1111_1001;
3'b010:duan <= 8'b1010_0100;
3'b011:duan <= 8'b1011_0000;
3'b100:duan <= 8'b1001_1001;
3'b101:duan <= 8'b1001_0010;
3'b110:duan <= 8'b1000_0010;
default:duan <= 8'hff;
endcase
end
else if(select == 3'd4)
begin
wei <= 8'b11101111;//分鐘個(gè)位數(shù)顯示
case(reg_fen_ge)
4'b0000:duan <= 8'b1100_0000;
4'b0001:duan <= 8'b1111_1001;
4'b0010:duan <= 8'b1010_0100;
4'b0011:duan <= 8'b1011_0000;
4'b0100:duan <= 8'b1001_1001;
4'b0101:duan <= 8'b1001_0010;
4'b0110:duan <= 8'b1000_0010;
4'b0111:duan <= 8'b1111_1000;
4'b1000:duan <= 8'b1000_0000;
4'b1001:duan <= 8'b1001_0000;
default:duan <= 8'hff;
endcase
end
else if(select == 3'd5)
begin
wei <= 8'b11011111;//分鐘十位數(shù)顯示
case(reg_fen_shi)
3'b000:duan <= 8'b1100_0000;
3'b001:duan <= 8'b1111_1001;
3'b010:duan <= 8'b1010_0100;
3'b011:duan <= 8'b1011_0000;
3'b100:duan <= 8'b1001_1001;
3'b101:duan <= 8'b1001_0010;
3'b110:duan <= 8'b1000_0010;
default:duan <= 8'hff;
endcase
end
else if(select == 3'd6)
begin
wei <= 8'b10111111;//時(shí)鐘個(gè)位數(shù)顯示
case(reg_shi_ge)
4'b0000:duan <= 8'b1100_0000;
4'b0001:duan <= 8'b1111_1001;
4'b0010:duan <= 8'b1010_0100;
4'b0011:duan <= 8'b1011_0000;
default:duan <= 8'hff;
endcase
end
else
begin
wei <= 8'b01111111;//時(shí)鐘十位數(shù)顯示
case(reg_shi_shi)
3'b000:duan <= 8'b1100_0000;
3'b001:duan <= 8'b1111_1001;
3'b010:duan <= 8'b1010_0100;
default:duan <= 8'hff;
endcase
end
end
endmodule
到了這里,關(guān)于基于FPGA的數(shù)字秒表設(shè)計(jì)(完整工程)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!