还剩9页未读,继续阅读
本资源只提供10页预览,全部文档请下载后查看!喜欢就下载吧,查找使用更方便
文本内容:
源代码最初版本仅满足题目中的基本要求,用显示输出结果
1.LEDmodule testlinput[3:0]numl,input[3:0]num2,input elk,outcome,output[3:0]sum;reg[3:0]sum;always@posedge elkbeginifoutcomebeginsum=numl+num2;endendendmodule增加清零功能
2.module testlinput[3:0]numl,input[3:0]num2,input clk,outcome,clr,output[3:0]sum;reg[3:0]sum;always@posedge elkbeginifoutcomebeginsum=numl+num2;endelse ifclrsum=0;endendmodule使用数码管显示且溢出时会显示3,Emodule testlinput[3:0]numl,input[3:0]num2,input elk,outcome,clr,output reg[6:0]a_to_g,output reg[3:0]lead;reg[4:0]sum;reg[l:0]tmp;reg[32:0]cln;always@*begin casetmp0:a_to_g=7bllllll0;l a_to_g=7b0110000;default:a_to_g=7bl001111;endcaseendalways@posedge elkifoutcome sum=numl+num2;else ifclrsum=0;always@posedge elkcln=cln+l;always®*beginif!sum
[4]begin casecln[13:12]0:begin tmp=sum
[0];lead=41bl000;endl:begin tmp=sum[l];lead=4b0100;end2:begin tmp=sum
[2];lead=4b0010;end3:begin tmp=sum
[3];lead=41b0001;end endcaseendelse begincasecln[13:12]O:begin tmp=3;lead=4bl000;endl:begin tmp=3;lead=4b0100;end2:begin tmp=3;lead=4b0010;end3:begin tmp=3;lead=4b0001;end endcaseendendendmodule拓展功能使用微动开关输入数值,数码管动态显示数值与符号(用显示),在微动开
4.LED关弹起时有效,并添加开关防抖功能timescale Ins/Ipsllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll〃Company://Engineer:////Create Date:2019/09/2813:31:07//Design Name://Module Name:testl//Project Name://Target Devices://Tool Versions://Description:////Dependencies:////Revision://Revision
0.01-File Created//Additional Comments://llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll(module testl,〃按键力口,减input buttonl,button button_add,button_minus,0,1,等于号显示结果的按键,清零input elk,outcome,clr,//H^,output reg[6:0]a_to_g,〃控制四个数码管的亮暗与否output reg[3:0]lead〃控制显示符号output reg[7:0]lattice=8b00000000LED两个数字的和或差reg[4:0]sum=SbOOOOO;//reg[l:0]tmp;〃一个多位数的时钟,用于数码管显示reg[32:0]cln与数字reg[3:0]numl=4b0000,num2=41b0000;//^12〃用于判断加法还是减法reg sign;〃用于检测输入数字与数字的情reg[3:0]detectorl=4b0000,detecto2=4b0000;12况〃判断这几个按键reg outcome_state=O,button_add_state=O,button_minus_state=0;被按下的状态是否有效reg[Sl^JoutcomeJn.addJn.minusJn.buttonlJn.buttonOJn;wire outcomeJn_pos,add_in_pos,minusJn_pos buttonlJn_pos buttonOJn_pos;//ffiJ于判断当按键松开时输出为1〃循环变量integer tmpi;always@posedge elkbegin〃清零ifclrbeginsum=51b00000;outcome_state=0;button addstate=0;button_minus_state=0;end〃此部分为若加或减被按下且有效则更改开关状态,为后续显示做铺垫state LEDelse ifadd_in_posbeginsign=l;二button_add_state-button_add_state;endelse ifminus_in_posbeginsign=0;button_minus_state=-button_minus_state;endifoutcome_in_posoutcome_state=l;〃用于判断加法还是减法,调整的值其实应该是或ifsign sumsum differencesum=numl+num2;elseif-signsum=numl-num2;endalways@*begincasetmp二O a_to_g7bllllllO;l:a_to_g=7b0110000;default:a_to_g=7bl001111;endcaseend〃此部分用于输入数字与的值always®posedge elk12清零ifclr//begindetectorl=4b0000;detector2=4b0000;numl=4b0000;num2=41b0000;endelsebegin〃此循环可保证输入数字串的顺序正确性,不会因fortmpi=0;tmpi4;tmpi=tmpi+10,1语句一直运行而导致无法判别的顺序always0,1if-detectorl[tmpi]tmpi==0||detectorl[tmpi-l]beginifbuttonO_in_posbeginnuml[3-tmpi]=0;detectorl[tmpi]=l;endelse ifbuttonl_in_posbeginnuml[3-tmpi]=l;detectorl[tmpi]=l;endend,ifdetectorl==4bllllbutton_add_state||button_minus_statebeginfortmpi=0;tmpi4;tmpi=tmpi+lif!detector2[tmpi]tmpi==0||detector2[tmpi-l]ifbuttonO_in_posbeginnum2[3-tmpi]=0;detector2[tmpi]=l;endelse ifbuttonl_in_posbeginnum2[3-tmpi]=l;detector2[tmpi]=l;endendend〃此部分用于判断微动开关的按下是否有效,且有一定防抖功能always@posedge elkbeginoutcome_in[31:0]={outcome_in[30:0],outcome};addjn[31:0]={add_in[30:0],button_add};minusjn[31:0]={minus_in[30:0],button_minus;buttonl_in[31:0]={buttonljn[30:0],buttonl};button0_in[31:0]={button0Jn[30:0],button0};endassign outcome_in_pos=outcomejn
[31]-outcome_in
[30]-outcome_in
[0];assign addjn_pos=addjn
[31]-add_in
[30]-add_in
[0];assign minus_in_pos=minus_in
[31]-minusjn
[30]-minusjn
[0];assign buttonljn_pos=buttonl_in
[31]^buttonljn
[30]-buttonl_in
[0];assign button0Jn_pos=button0Jn
[31]-button0Jn
[30]-button0Jn
[0];〃一个随时钟而增加的值,便于控制数码管显示always@posedge elkcln=cln+l;〃控制显示符号的部分always@posedge elkifclrlattice=81b00000000;else ifbutton_add_state-outcome_statelattice=8bll000000;else ifbutton_minus_state-outcome_statelattice=8b00110000;else ifoutcome_statelattice=81b00001111;elselattice=8b00000000;/**/控制显示数码管always@posedge elk//ifclrtmp=0;elsebegin二二〃两个数字者已输入ifdetectorl4blllldetector2==4blllloutcome_state==l R有效且按下了等号输出结果begin〃溢出if!sum
[4]begincasecln[13:12]O:begin tmp=sum[O];lead=4bl000;endl:begin tmp=sum[l];lead=41b0100;end2:begin tmp=sum
[2];lead=41b0010;end3:begin tmp=sum
[3];lead=41b0001;endendcaseend〃非溢出elsebegincasecln[13:12]O:begin tmp=3;lead=4bl000;endl:begin tmp=3;lead=41b0100;end2:begin tmp=3;lead=4b0010;end3:begin tmp=3;lead=4b0001;endendcaseendend〃两个数字均未输入else ifdetectorl==4b0000casecln[13:12]O:begin tmp=O;lead=41bl000;endl:begin tmp=O;lead=4b0100;end2:begin tmp=O;lead=41b0010;end3:begin tmp=O;lead=41b0001;endendcase二二〃第一个数字已输入一位else ifdetectorl4b0001begin tmp=numl
[3];lead=41bl000;end二二〃第一个数字已输入两位else ifdetectorl4b0011casecln[13:12JO:begin tmp=numl
[2];lead=41bl000;endl:begin tmp=numl
[3];lead=41b0100;endendcase二〃第一个数字已输入三位else ifdetectorl=4b0111casecln[13:12]O:begin tmp=numl[l];lead=41bl000;endl:begin tmp=numl
[2];lead=41b0100;end2:begin tmp=numl
[3];lead=4b0010;endendcase〃第一个数字已输入完成且第二位数字未else ifdetectorl==4blllldetector2==4b0000输入casecln[13:12]O:begin tmp=numl[O];lead=4bl000;endl:begin tmp=numl[l];lead=41b0100;end2:begin tmp=numl
[2];lead=4b0010;end3:begin tmp=numl
[3];lead=4b0001;endendcase二二〃第二个数字输入位else ifdetector24b00011begin tmp=num2
[3];lead=4bl000;end〃第二个数字输入两位else ifdetector2==41b0011casecln[13:12]O:begin tmp=num2
[2];lead=41bl000;endl:begin tmp=num2
[3];lead=4b0100;endendcase〃第二个数字输入位else ifdetector2==4b01113casecln[13:12]O:begin tmp=num2[l];lead=4bl000;endl:begin tmp=num2
[2];lead=41b0100;end2:begin tmp=num2
[3];lead=4b0010;endendcase位且未按下等号else ifdetector2==4blllloutcome_state==0//M—4casecln[13:12]O:begin tmp=num2
[0];lead=4bl000;endl:begin tmp=num2[l];lead=41b0100;end2:begin tmp=num2
[2];lead=41b0010;end3:begin tmp=num2
[3];lead=41b0001;endendcaseendendmodule。
个人认证
优秀文档
获得点赞 0