抢答器verilog版

  • 格式:pdf
  • 大小:305.30 KB
  • 文档页数:10

抢答器-verilog

一.实验要求

设计一个可容纳四组参赛的数字式抢答器,每组设一个按钮供抢答使用。抢答器具有第一信

号鉴别和锁存功能,使除第一抢答者外的按钮不起作用;设置一个主持人“复位”按钮,主持

人复位后,开始抢答,第一信号鉴别锁存电路得到信号后,用指示灯显示抢答组别,扬声器发出2—3s的音响。 设置犯规电路,对提前抢答和超时答题(例如3min)的组别鸣笛示警,并由组别显示电

路显示出犯规组别。 设置一个计分电路,每组开始预置10分,由主持人记分,答对一次加1分,答错一

次减1分。 二.实验方框图

三.答题规则(本实验均采用脉冲按键模式)

四.本次实验的verilog源程序 module answer(clk,k1,k2,k3,k4,o1,o2,o3,o4,out1,out2,start,reset1, save); //抢答信号鉴别,保持模

块 input clk,k1,k2,k3,k4,start,reset1;//k1~k2是选手按键

output o1,o2,o3,o4,out1,out2;//o1~o2是抢答信号显示灯

output[2:0] save; //用于保存选手的抢答信号

reg o1,o2,o3,o4,out1,out2,ok;

reg[7:0] counter;

reg[2:0] save;

integer i;

always@(posedge clk) 按reset1键复位清零

主持人念下一题

有选手按抢答键

主持人已按下start键 答对加分;答错,提前抢答,超时减分

NO 扬声器报警

YES

选手答题,扬声器响几下

未超时 NO

YES

答对 NO YES if(reset1)//当reset1为1时复位清零

begin

{o1,o2,o3,o4,out1,out2,ok}<=7'b0000000; counter<=0;

i=0;

end

else begin

//

//

if(start)

begin ok<=1;

save<=0;

end if(!ok)//reset1和ok为0时,有人按键表示犯规

begin //一旦有人按键,他的输出信号将屏蔽其他人的按键

if(k1&&!o2&&!o3&&!o4)

begin

o1<=1;

save<=1; end

if(k2&&!o1&&!o3&&!o4)

begin

o2<=1;

save<=2; end

if(k3&&!o1&&!o2&&!o4)

begin

o3<=1;

save<=3; end

if(k4&&!o1&&!o2&&!o3)

begin

o4<=1; save<=4;

end if(o1 || o2 || o3 || o4)//提前按键扬声器响

out1<=1;

end else//reset1为0,ok为1时,有人按键为正常抢答

begin

// if(k1 || k2 || k3 || k4) begin //一旦有人按键,他的输出信号将屏蔽其他人的按键

if(k1&&!o2&&!o3&&!o4)

begin

o1<=1; save<=1;

end

if(k2&&!o1&&!o3&&!o4)

begin

o2<=1; save<=2;

end

if(k3&&!o1&&!o2&&!o4)

begin

o3<=1; save<=3;

end

if(k4&&!o1&&!o2&&!o3)

begin

o4<=1; save<=4;

end

end

if(o1 || o2 || o3 || o4)

begin i=i+1;

if(i<2) out2<=1;//有人抢到题,响几下铃。由i控制响铃次数

else

out2<=0; if(counter<60)//一旦有抢答信号,计数器开始记时

begin

counter<=(counter+1);

out1<=out1; end

else out1<=1;//超时报警

end

// end

//

// end

endmodule

module

count(clk,add,min,count1l,count1h,count2l,count2h,count3l,count3h,count4l,count4h,save); input clk,o1,o2,o3,o4,add,min;//add,min分别为加分,减分键

input [2:0] save;

output[3:0] count1l,count1h,count2l,count2h,count3l,count3h,count4l,count4h; reg[3:0] count1l,count1h,count2l,count2h,count3l,count3h,count4l,count4h;//1~4号选手的高位,

低位记分输出

always@(posedge clk)

begin

//

// if(add && min)//开始前置10分

{count1l,count1h,count2l,count2h,count3l,count3h,count4l,count4h}=32'h01010101;

else

begin // if(add)//add和各输出信号配合,给选手加分

begin

if(save==1)

begin if(count1l==9)

begin

count1l=0;

count1h=count1h+1;

end else

count1l=count1l+1;

end

else if(save==2) begin

if(count2l==9)

begin

count2l=0;

count2h=count2h+1; end else

count2l=count2l+1;

end

else if(save==3)

begin

if(count3l==9)

begin count3l=0;

count3h=count3h+1;

end

else

count3l=count3l+1; end

else if(save==4)

begin

if(count4l==9) begin

count4l=0;

count4h=count4h+1;

end

else count4l=count4l+1;

end

end

if(min)//min和各输出信号配合,给选手减分

begin

if(save==1)

begin

if(count1l==0) begin

count1l=9;

count1h=count1h-1;

end

else count1l=count1l-1;

end

else if(save==2)

begin if(count2l==0) begin

count2l=9;

count2h=count2h-1; end

else

count2l=count2l-1;

end

else if(save==3)

begin

if(count3l==0)

begin

count3l=9; count3h=count3h-1;

end

else

count3l=count3l-1;

end

else if(save==4)

begin

if(count4l==0)

begin count4l=9;

count4h=count4h-1;

end

else

count4l=count4l-1; end

end

//

end //

//

end

endmodule

module sound(speaker,out1,out2,clk1);//扬声器模块

input out1,out2,clk1;//out1,out2分别为犯规,正常抢答的扬声器驱动信号

output speaker;

assign speaker=(out1&&clk1) || (out2&&clk1); endmodule

module pld(clk,clk1,speaker,reset1,k1,k2,k3,k4,o1,o2,o3,o4,start,add,min,count1l,count1h,count2l,count2h,count3l,count3h,count4l,count4h);//总模块,将上面各模块组合在一起形成抢答器功能

input clk,clk1,k1,k2,k3,k4,start,add,min,reset1;

output o1,o2,o3,o4,speaker;

output[3:0] count1l,count1h,count2l,count2h,count3l,count3h,count4l,count4h; wire out1,out2,o1,o2,o3,o4;

wire [2:0] save;

answer pld1(clk,k1,k2,k3,k4,o1,o2,o3,o4,out1,out2,start,reset1,ok,save);

count pld2(clk,o1,o2,o3,o4,add,min,count1l,count1h,count2l,count2h,count3l,count3h,count4l,count4h,s

ave);

sound pld3(speaker,out1,out2,clk1);

endmodule

五。实验波形 1. 下图是答题正确的情况。 从图中可见一旦有一位选手先按抢答键(图中为K4),则其他选手的按键无效。当K4

按键后响铃会响一会儿,提醒有人抢到题了。答题正确后,会加一分。(图中K4的分从10加到11)。

答错题的情况与此相似,只是加分变成减分。