VerilogHDL电梯程序文件.docx
《VerilogHDL电梯程序文件.docx》由会员分享,可在线阅读,更多相关《VerilogHDL电梯程序文件.docx(14页珍藏版)》请在冰点文库上搜索。
VerilogHDL电梯程序文件
这个程序不需要你再添加任何的程序,你只需要添加楼层数就可以成功控制6层以上电梯,另外你必须得读懂每一块程序的作用,才能对这个程序有更好的应用
moduledtsj(clk,reset,up1,up2,up3,up4,up5,dn2,dn3,
dn4,dn5,dn6,d1,d2,d3,d4,d5,d6,overw,pro,led,hex,Buzzer1,lig);
inputclk,reset;//reset键使用key
inputoverw,pro,up1,up2,up3,up4,up5,dn2,dn3,dn4,dn5,dn6,d1,d2,d3,d4,d5,d6;//故障警报键
output[6:
0]hex,lig;//light和led用数码管
output[5:
0]led;
outputBuzzer1;
regBuzzer1,door;
reg[6:
0]hex,lig;
reg[2:
0]state,next_state,count;
reg[5:
0]d,up,dn,now_f,curr,led;//d是内部按键,up外部上升按键,dn是外部下降按键,
//now_f是当前楼层,curr是当前楼层的另外一个变量
reg[1:
0]ud_f;//上升下降标志位
reglight;
//reg[29:
0]counter;
//regclkout;
parameter
idle_state=3'b001,open_state=3'b010,close_state=3'b011,
up_state=3'b100,down_state=3'b101,sleep_state=3'b110,
alarm_state=3'b111,f1=6'b000001,f2=6'b000010,f3=6'b000100,f4=6'b001000
f5=6'b010000,f6=6'b100000,up_f=2'b01,dn_f=2'b10,idle=2'b00;
//jiuzhongzhuangtaiheshangshengzhuangtai'shangshengzhuangtaihexiajiazhuangtai
initial
begin
state<=idle_state;
end
always@(posedgeclkorposedgereset)
if(reset)
state<=idle_state;
else
state<=next_state;
always@(stateorupordnordornow_forcountorud_f)
case(state)
open_state:
begin
if(count<5)
next_state=open_state;
else
next_state=close_state;
end
sleep_state:
begin
if((up||dn||d||ud_f)==0)
begin
next_state=close_state;
end
else
next_state=idle_state;
end
idle_state:
//初始状态
begin
if((up||dn||d||ud_f)==0)
next_state=sleep_state;
if(!
overw||pro==1)
next_state=alarm_state;
elseif(d>0)
begin
if((d&now_f)>0)
next_state=open_state;
elseif(d>now_f)
next_state=up_state;
else
next_state=down_state;
end
elseif((up&now_f)||(dn&now_f))
next_state=open_state;
elseif((up>now_f)||(dn>now_f))
next_state=up_state;
elseif(up||dn)
next_state=down_state;
else
next_state=idle_state;
end
up_state:
begin
if((up||dn||d||ud_f)==0)
next_state=sleep_state;
if(!
overw||pro==1)
next_state=alarm_state;
elseif((d&now_f)||(up&now_f))
next_state=open_state;
elseif((d>now_f)||(up>now_f))
next_state=up_state;
elseif((dnext_state=down_state;
elseif(dn>0)
begin
if(dn>now_f)
next_state=up_state;
elseif((dn&now_f)||(now_fnext_state=open_state;
elseif((dn&now_f)&&(now_f==f6))
next_state=open_state;
else
next_state=down_state;
end
else
next_state=idle_state;
end
down_state:
begin
if((up||dn||d||ud_f)==0)
next_state=sleep_state;
elseif(!
overw||pro==1)
next_state=alarm_state;
elseif((d&now_f)||(dn&now_f))
next_state=open_state;
elseif(((d=6'b000001))||((dn=6'b000001)))
next_state=down_state;
elseif((d>now_f)||(dn>now_f))
next_state=up_state;
elseif(up>0)
begin
if(upnext_state=down_state;
elseif((up&now_f)&&(now_f>f1))
next_state=down_state;
elseif((up&now_f)&&(now_f==f1))
next_state=open_state;
else
next_state=up_state;
end
else
next_state=idle_state;
end
close_state:
begin
if((up||dn||d||ud_f)==0)
next_state=sleep_state;
elseif(!
overw||pro==1)
next_state=alarm_state;
elseif(ud_f==up_f)
begin
if((d&now_f)||(up&now_f))
next_state=open_state;
elseif((d>now_f)||(up>now_f))
next_state=up_state;
elseif(d||up)
next_state=down_state;
elseif(dn>0)
begin
if(dn>now_f)
next_state=up_state;
elseif((dn&now_f)>0)
next_state=open_state;
else
next_state=down_state;
end
else
next_state=idle_state;
end
elseif(ud_f==dn_f)
begin
if((d&now_f)||(dn&now_f))
next_state=open_state;
elseif(((d=6'b000000))||((dn=6'b000000)))
next_state=down_state;
elseif(d||dn)
next_state=up_state;
elseif(up>0)
begin
if(upnext_state=down_state;
elseif((up&now_f)>0)
next_state=open_state;
else
next_state=up_state;
end
else
next_state=idle_state;
end
else
begin
if(d>0)
begin
if((d&now_f)>0)
next_state=open_state;
elseif(d>now_f)
next_state=up_state;
else
next_state=down_state;
end
elseif((up&now_f)&&(dn&now_f))
next_state=open_state;
elseif((up>now_f)&&(dn>now_f))
next_state=up_state;
elseif(up||dn)
next_state=down_state;
else
next_state=idle_state;
end
end
alarm_state:
begin
if(!
overw||pro==1)
begin
//Buzzer1<=1'b1;
next_state=open_state;
end
else
begin
//Buzzer1<=1'b0;
if(d>0)
begin
if((d&now_f)>0)
next_state=open_state;
elseif(d>now_f)
next_state<=up_state;
else
next_state=down_state;
end
elseif((up&now_f)||(dn&now_f))
next_state=open_state;
elseif((up>now_f)||(dn>now_f))
next_state=up_state;
elseif(up||dn)
next_state=down_state;
else
next_state=idle_state;
end
end
default:
next_state<=idle_state;
endcase
always@(up1orup2orup3orup4orup5)
up={1'b0,up5,up4,up3,up2,up1};
always@(dn2ordn3ordn4ordn5ordn6)
dn={dn6,dn5,dn4,dn3,dn2,1'b0};
always@(d1ord2ord3ord4ord5ord6)
d={d6,d5,d4,d3,d2,d1};
always@(posedgeclkorposedgereset)
begin
if(reset)
count<=0;
elseif((next_state==open_state)&&(count<5))
count<=count+1;
else
count<=0;
end
always@(posedgeclkorposedgereset)
//always@(resetornext_state)
if(reset)
begin
now_f<=f1;
ud_f<=idle;
led<=6'b000001;
light<=1'b0;
end
else
begin
now_f<=now_f;
curr=now_f;
case(next_state)
idle_state:
begin
now_f<=now_f;
ud_f<=idle;
led<=led;
curr<=now_f;
Buzzer1<=1'b0;
end
up_state:
begin
now_f<=now_f<<1;
ud_f<=up_f;
led<=led+1;
curr<=now_f;
end
down_state:
begin
now_f<=now_f>>1;
ud_f<=dn_f;
led<=led+1;
curr<=now_f;
end
open_state:
begin
now_f<=now_f;
ud_f<=ud_f;
led<=led;
door<=1'b1;
light<=door;
curr<=now_f;
end
close_state:
begin
now_f<=now_f;
ud_f<=ud_f;
led<=led;
curr<=now_f;
door<=1'b0;
light<=door;
end
alarm_state:
begin
now_f<=now_f;
ud_f<=ud_f;
led<=led;
Buzzer1<=1'b1;
curr<=now_f;
door<=1'b1;
light<=door;
end
sleep_state:
begin
now_f<=now_f;
ud_f<=ud_f;
led<=6'b000000;
curr<=now_f;
end
default:
begin
now_f<=f1;
ud_f<=idle;
led<=led;
end
endcase
end
always@(curr)
begin
case(curr)
6'b000001:
hex=7'b1111001;//数码管共阴共阳
6'b000010:
hex=7'b1000100;
6'b000100:
hex=7'b0110000;
6'b001000:
hex=7'b0011001;
6'b010000:
hex=7'b1101101;
6'b100000:
hex=7'b1111101;
default:
hex=7'b0000000;
endcase
end
always@(light)
begin
case(light)
1'b0:
lig=7'b1000000;//1'b1:
lig=7'b1000000;
1'b1:
lig=7'b1111001;
default:
light=7'b0000000;
endcase
end
//分频程序
/*always@(posedgeclk)
begin
if(counter[29:
0]==24999999)
begin//每计到4个(0~3)上升沿,输出信号翻转一次
counter<=0;
clkout<=~clkout;
end
else
counter[29:
0]<=counter[29:
0]+1'b1;
end*/
endmodule