標(biāo)題: 基于FPGA的搶答器 包含源碼、演示、文檔 [打印本頁(yè)]
作者: cc12138 時(shí)間: 2020-1-2 21:03
標(biāo)題: 基于FPGA的搶答器 包含源碼、演示、文檔
設(shè)計(jì)要求:
根據(jù)所學(xué)知識(shí)和試驗(yàn)板的資源,使用Verilog HDL語(yǔ)言設(shè)計(jì)一個(gè)四路搶答器。
搶答器可供四組搶答,且有鑒別第一信號(hào)和鎖存功能,主持人按下復(fù)位鍵后開(kāi)始搶答,并在數(shù)碼管顯示10秒的倒計(jì)時(shí),當(dāng)有人搶答時(shí),數(shù)碼管計(jì)時(shí)消失,對(duì)應(yīng)選手的LED燈將亮起,且蜂鳴器發(fā)出響聲,此時(shí)電路自鎖,其他選手搶答無(wú)效。
如果10秒內(nèi)無(wú)人搶答,相應(yīng)的LED燈亮起,表示此輪搶答作廢。
設(shè)計(jì)代碼及說(shuō)明:
qiangda模塊:該模塊具備鑒別第一信號(hào)和鎖存的功能。精確的鑒別出第一信號(hào)之后將輸入端封鎖,使其他組的搶答無(wú)效。設(shè)置4個(gè)輸入端num1,num2,num3,num4(4個(gè)獨(dú)立按鍵),并設(shè)置pd8做主持人復(fù)位鍵rst。當(dāng)rst=1,搶答開(kāi)始,當(dāng)rst=0時(shí),搶答電路復(fù)位。
module qiangda(num1, num2, num3, num4, clk, rst_n, led1, led2, led3, led4,sec_h, beep);
input num1, num2, num3, num4, clk, rst_n, sec_h;
output led1, led2, led3, led4, beep;
reg startflag=1, beepflag=0;
reg led1, led2, led3, led4, beep;
always@(posedge clk)
begin
//-------------復(fù)位操作,當(dāng)復(fù)位鍵置0時(shí),led1-4都不亮,蜂鳴器不叫--------
if(rst_n==0)
begin
startflag<=1;
led1=0;
led2=0;
led3=0;
led4=0;
beep<=0;
end
//-------------搶答操作,共4種情況--------------------
if(startflag==1&&!(sec_h==0))
begin
if(num1==0)
begin
startflag<=0;
led1=1;
beepflag<=1;
end
else if(num2==0)
begin
startflag<=0;
led2=1;
beepflag<=1;
end
else if(num3==0)
begin
startflag<=0;
led3=1;
beepflag<=1;
end
else if(num4==0)
begin
startflag<=0;
led4=1;
beepflag<=1;
end
end
if(beepflag==1)
begin
beep<=1;
beepflag<=0;
end
end
Endmodule
time_counter模塊:該模塊實(shí)現(xiàn)了10秒倒計(jì)時(shí)的計(jì)數(shù)功能,通過(guò)1000ms進(jìn)位1秒,倒數(shù)10秒后可通過(guò)主持人的復(fù)位鍵重新復(fù)位計(jì)時(shí)。
module time_counter( rst_n, clk, sec_h, sec_l, display_flag, led8);
parameter CLK_CYCLE = 20;
parameter T0 = 1000000;
parameter T0_VAL = T0/CLK_CYCLE;
input clk;
input rst_n;
output reg led8;
output reg[2:0] sec_h; //數(shù)碼管的十位
output reg[3:0] sec_l; //數(shù)碼管的個(gè)位
output display_flag; //數(shù)碼管動(dòng)態(tài)掃描標(biāo)志位
//-----------------------------------1ms延時(shí)------
reg[15:0] cnt;
always@(posedge clk or negedge rst_n)
begin
if(rst_n == 1'b0)
cnt <= (0);
else if(cnt < T0_VAL)
cnt <= cnt + 1'b1;
else
cnt <= (0);
end
assign delay_1ms = (cnt == T0_VAL); //1ms延時(shí)完成標(biāo)志位
assign display_flag = delay_1ms; //數(shù)碼管動(dòng)態(tài)掃描標(biāo)志位
//------------------------------------1s延時(shí)------
reg[9:0] mse;
always@(posedge clk or negedge rst_n)
begin
if(rst_n == 1'b0)
mse <= (0);
else
begin
if(delay_1ms == 1'b1)
begin
if(mse < 10'd1000)
mse <= mse + 10'd1;
else
mse <= (0);
end
end
end
wire sec_l_flag = ((mse == 10'd999) && (delay_1ms == 1'b1));//1s延時(shí)完成標(biāo)志位
//------------------------------------秒個(gè)位數(shù)計(jì)數(shù)------
always@(posedge clk or negedge rst_n) //時(shí)鐘和復(fù)位
begin
if(rst_n == 1'b0) //復(fù)位后數(shù)碼管低位等于0
sec_l <= 0;
else
begin
if(sec_l_flag ==1'b1) //1s延時(shí)完成標(biāo)志位等于1
begin
if(sec_h!=3'd0 && sec_l==0) //高位不等于0并且低位等于0
sec_l<=9; //讓低位等于9
else if(sec_l >4'd0) //低位大于0
sec_l <= sec_l - 4'd1; //低位進(jìn)行減一操作
else
sec_l <= 0; //低位等于0
end
end
end
wire sec_h_flag = ((sec_l == 4'd0) && (sec_l_flag == 1'b1)); //sec_h_flag是秒個(gè)位數(shù)進(jìn)位標(biāo)志位
//------------------------------------秒十位數(shù)計(jì)數(shù)------
always@(posedge clk or negedge rst_n) //時(shí)鐘和復(fù)位
begin
if(rst_n == 1'b0) //復(fù)位后,如果高位等于1,led8不亮
begin
sec_h <= 1;
led8=0;
end
else
begin
if(sec_h_flag == 1'b1) //如果秒個(gè)位數(shù)進(jìn)位標(biāo)志位等于1
begin
if(sec_h > 0 && sec_l==0) //高位大于0且低位等于0
sec_h <= sec_h - 3'd1; //高位減1
else
begin
sec_h <= 3'd0; //高位等于0
led8=1; //led8燈亮
end
end
end
end
endmodule
display模塊:該模塊包含了數(shù)碼管的編碼函數(shù),數(shù)碼管動(dòng)態(tài)顯示的計(jì)數(shù)器,數(shù)碼管的編碼輸出,并將模塊2的計(jì)數(shù)結(jié)果動(dòng)態(tài)顯示在數(shù)碼管上。
module display(rst_n, clk, sec_h, sec_l, led1, led2, led3, led4, display_flag, seg, sel);
input rst_n;
input clk;
input led1, led2, led3, led4;
input[2:0] sec_h;
input[3:0] sec_l;
input[3:0] display_flag;
output reg[7:0] seg;
output reg[3:0] sel;
function [7:0] seg_data;
input[3:0] din;
input dp;
begin
case(din)
4'd0 : seg_data = {7'b1111110,dp};
4'd1 : seg_data = {7'b0110000,dp};
4'd2 : seg_data = {7'b1101101,dp};
4'd3 : seg_data = {7'b1111001,dp};
4'd4 : seg_data = {7'b0110011,dp};
4'd5 : seg_data = {7'b1011011,dp};
4'd6 : seg_data = {7'b1011111,dp};
4'd7 : seg_data = {7'b1110000,dp};
4'd8 : seg_data = {7'b1111111,dp};
4'd9 : seg_data = {7'b1111011,dp};
4'd10 : seg_data = {7'b0000000,dp};
endcase
end
endfunction
//-------------------------------------數(shù)碼管動(dòng)態(tài)顯示的計(jì)數(shù)器
reg[1:0] cnt;
always @(posedge clk or negedge rst_n)
begin
if(rst_n == 1'b0)
cnt <= (0);
else if(display_flag == 1'b1)
cnt <= cnt + 1'b1;
else
cnt <= cnt;
end
//-------------------------------------編碼輸出
always @(posedge clk or negedge rst_n)
begin
if(rst_n == 1'b0)
begin
seg <=7'b0000000; //執(zhí)行復(fù)位按鍵后,清0
sel <=4'b0000;
end
else
begin
case(cnt)
2'b00 :
begin
if(led1==1||led2==1||led3==1||led4==1)
begin
seg<=7'b0000000;
sel<=4'b0000;
end
else
seg <= seg_data(sec_l,1'b0);
sel <= 4'b1000;
end
2'b01 :
begin
if(led1==1||led2==1||led3==1||led4==1)
begin
seg<=7'b0000000;
sel<=4'b0000;
end
else
seg <= seg_data({1'b0,sec_h},1'b0);
sel <= 4'b0100;
end
endcase
end
end
endmodule
main模塊:調(diào)用其他模塊實(shí)現(xiàn)整體功能
module main(num1, num2, num3, num4, led1, led2, led3, led4, led8, rst_n, clk,Pd0, Pd1, seg, sel,beep);
//參數(shù)定義
parameter CLK_CYCLE = 20;
parameter T0 = 1000_000;
//端口定義
input rst_n;
input clk;
input Pd0;
input Pd1;
input num1,num2,num3,num4;
output led1,led2,led3,led4,led8;
output[7:0] seg;
output[3:0] sel;
output beep;
// 變量定義
wire[2:0] sec_h;
wire[3:0] sec_l;
wire display_flag;
//----------------模塊例化--------------------
//例化qiangda模塊
qiangda u_qiangda (
.rst_n ( rst_n ),
.clk ( clk ),
.led1 ( led1 ),
.led2 ( led2 ),
.led3 ( led3 ),
.led4 ( led4 ),
.sec_h ( sec_h ),
.num1 ( num1 ),
.num2 ( num2 ),
.num3 ( num3 ),
.num4 ( num4 ),
.beep ( beep ));
//例化time_counter模塊
time_counter #(
.CLK_CYCLE ( CLK_CYCLE ),
.T0 ( T0 ))
u_time_counter (
.rst_n ( rst_n ),
.clk ( clk ),
.sec_h ( sec_h ),
.sec_l ( sec_l ),
.display_flag ( display_flag ),
.led8 ( led8 ));
//例化display模塊
display u_display(
.rst_n ( rst_n ),
.clk ( clk ),
.sec_h ( sec_h ),
.sec_l ( sec_l ),
.display_flag ( display_flag ),
.seg ( seg ),
.led1 ( led1 ),
.led2 ( led2 ),
.led3 ( led3 ),
.led4 ( led4 ),
.sel ( sel ));
endmodule
硬件效果:
將設(shè)計(jì)好的代碼下載到試驗(yàn)板里,實(shí)現(xiàn)了相應(yīng)的功能,效果如圖
(1)搶答開(kāi)始,倒計(jì)時(shí)進(jìn)行中
圖片3.png (381.03 KB, 下載次數(shù): 53)
下載附件
2020-1-2 21:01 上傳
(2)搶答時(shí)間到仍然沒(méi)有人搶答,裁判燈亮
圖片2.png (374.39 KB, 下載次數(shù): 58)
下載附件
2020-1-2 21:01 上傳
(3)三號(hào)選手搶答成功,倒計(jì)時(shí)停止,對(duì)應(yīng)的led燈亮
圖片1.png (377.16 KB, 下載次數(shù): 58)
下載附件
2020-1-2 21:00 上傳
完整的Word格式文檔51黑下載地址:
文檔.doc
(613.27 KB, 下載次數(shù): 45)
2020-1-2 23:49 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
歡迎光臨 (http://www.raoushi.com/bbs/) |
Powered by Discuz! X3.1 |