您好,欢迎访问三七文档
BCD码模块modulebcdma(out1,out0);input[3:0]out0;output[6:0]out1;reg[6:0]out1;always@(out0)begincase(out0)4'd0:out1=7'b1111110;4'd1:out1=7'b0110000;4'd2:out1=7'b1101101;4'd3:out1=7'b1111001;4'd4:out1=7'b0110011;4'd5:out1=7'b1011011;4'd6:out1=7'b1011111;4'd7:out1=7'b1110000;4'd8:out1=7'b1111111;4'd9:out1=7'b1111011;default:out1=7'bx;endcaseendendmodule分频模块(由于我们选择的时钟频率是1khz所以要把它分成1hz的)modulefenpin(reset,clk,clkout,qout);inputreset,clk;outputclkout,qout;regclkout;reg[4:0]qout;always@(posedgeclk)beginif(!reset)qout=0;elseif(qout19)qout=qout+1;elseqout=0;endalways@(posedgeclk)beginif(!reset)clkout=0;elseif(qout==19)clkout=1;elseclkout=0;endendmodule20分频modulefenpina(reset,clkout,clkouta,qouta);inputreset,clkout;outputclkouta,qouta;regclkouta;reg[5:0]qouta;always@(posedgeclkout)beginif(!reset)qouta=0;elseif(qouta49)qouta=qouta+1;elseqouta=0;endalways@(posedgeclkout)beginif(!reset)clkouta=0;elseif(qouta==49)clkouta=1;elseclkouta=0;endendmodule50分频控制部分采用的是状态机modulezhuangtai(clk,EN,out);inputEN,clk;output[2:0]out;reg[2:0]out;reg[1:0]state,next_state;parameterstate0=2'b00,state1=2'b01,state2=2'b11,state3=3'b10;always@(posedgeclk)beginif(EN)state=next_state;elsestate=state0;endalways@(posedgeclk)begincase(state)state0:beginif(EN)next_state=state1;elsenext_state=state0;endstate1:beginnext_state=state2;endstate2:beginif(EN)next_state=state3;elsenext_state=state0;endstate3:beginif(EN)next_state=state0;elsenext_state=state3;enddefault:next_state=state0;endcaseendalways@(posedgeclk)begincase(state)state0:out=3'b111;state1:out=3'b011;state2:out=3'b101;state3:out=3'b001;default:out=3'b111;endcaseendendmodule译码模块moduleyima(out,ACOUNT,BCOUNT,out0);input[7:0]ACOUNT,BCOUNT;input[2:0]out;output[3:0]out0;reg[3:0]out0;always@(out)begincase(out)3'b111:out0=ACOUNT[7:4];3'b011:out0=ACOUNT[3:0];3'b101:out0=BCOUNT[7:4];3'b001:out0=BCOUNT[3:0];endcaseendendmodule主程序modulezhuanzhou(clkouta,EN,LAMPA,LAMPB,ACOUNT,BCOUNT);inputclkouta,EN;output[3:0]LAMPA,LAMPB;output[7:0]ACOUNT,BCOUNT;regtempa,tempb;reg[2:0]counta,countb;reg[3:0]LAMPA,LAMPB;reg[7:0]ared,ayellow,agreen,aleft,bred,byellow,bgreen,bleft;reg[7:0]numa,numb;always@(EN)if(EN)beginared=8'd55;ayellow=8'd5;agreen=8'd40;aleft=8'd15;bred=8'd65;byellow=8'd5;bleft=8'd15;bgreen=8'd30;endassignACOUNT=numa;assignBCOUNT=numb;always@(posedgeclkouta)beginif(EN)beginif(!tempa)begintempa=1;case(counta)0:beginnuma=agreen;LAMPA=2;counta=1;end1:beginnuma=ayellow;LAMPA=4;counta=2;end2:beginnuma=aleft;LAMPA=1;counta=3;end3:beginnuma=ayellow;LAMPA=4;counta=4;end4:beginnuma=ared;LAMPA=8;counta=0;enddefault:LAMPA=8;endcaseendelsebeginif(numa1)if(numa[3:0]==0)beginnuma[3:0]=4'b1001;numa[7:4]=numa[7:4]-1;endelsenuma[3:0]=numa[3:0]-1;if(numa==1)tempa=0;endendelsebeginLAMPA=4'b1000;counta=0;tempa=0;endendalways@(posedgeclkouta)beginif(EN)beginif(!tempb)begintempb=1;case(countb)0:beginnumb=bred;LAMPB=8;countb=1;end1:beginnumb=bgreen;LAMPB=2;countb=2;end2:beginnumb=byellow;LAMPB=4;countb=3;end3:beginnumb=bleft;LAMPB=1;countb=4;end4:beginnumb=byellow;LAMPB=4;countb=0;enddefault:LAMPB=8;endcaseendelsebeginif(numb1)if(!numb[3:0])beginnumb[3:0]=9;numb[7:4]=numb[7:4]-1;endelsenumb[3:0]=numb[3:0]-1;if(numb==1)tempb=0;endendelsebeginLAMPB=4'b1000;tempb=0;countb=0;endendendmodule
本文标题:FPGA交通灯程序
链接地址:https://www.777doc.com/doc-6333577 .html