温控箱主文件:
module box(t,heat,cool,clk,reset);//命名模块,列出端口列表
output heat,cool; //声明输出变量及其位宽:
input [5:0] t; //声明输入变量及其位宽
input clk,reset; //声明输入变量
reg heat,cool; //定义输出变量为寄存器类型
parameter [1:0] right =2'b00,//设定参数
too_cold=2'b10,
too_hot =2'b01;
reg [1:0] current_state; //定义中间变量,用于判定加热还是制冷//还是不操作
always@(posedge clk or negedge reset)//异步清零
if(reset==1'b0) //首先判断清零端是否要清零
current_state<=right; //等于通电后先初始化
else
case(current_state) //判断当天状态,并进入相应选项
right: begin //如果在不操作状态下
if (t[5:0]>26) //输入大于26度
current_state<=too_hot;//则过热状态给中间变量
else if(t[5:0]<24) //输入小于24度
current_state<=too_cold//则过冷状态给中间变量;
else
current_state<=right;//其他情况
end
too_cold:begin
if (t[5:0]>26)
current_state<=too_hot;
else if(t[5:0]==25)
current_state<=right;
else if(t[5:0]<24)
current_state<=too_cold;
else
current_state<=too_cold;
end
too_hot:begin
if (t[5:0]>26)
current_state<=too_hot;
else if(t[5:0]==25)
current_state<=right;
else if(t[5:0]<24)
current_state<=too_cold;
else
current_state<=too_hot;
end
default current_state<=right;
endcase
always@(current_state)//根据中间变量状态
case(current_state) //判断是该加热还是制冷还是不操作
right: begin
heat=1'b0;cool=1'b0;
end
too_hot: begin
heat=1'b0;cool=1'b1;
end
too_cold:begin
heat=1'b1;cool=1'b0;
end
endcase
endmodule
温控箱激励文件:
module box_tb;
wire HEAT,COOL;
reg [5:0] T=0;
reg CLK,RESET;
box BOX(.heat(HEAT),.cool(COOL),.t(T),.clk(CLK),.reset(RESET));
initial
begin
CLK=1;RESET=0;
#10 RESET=1;
#10 T[5:0]=6'b111111; //right to hot
#10 T[5:0]=6'b011001; //hot to rihgt
#10 T[5:0]=6'b000001; //right to cold
#10 T[5:0]=6'b011001; //cold to rihgt
#10 T[5:0]=6'b011001; //right to rihgt
#10 T[5:0]=6'b111111; //right to hot
#10 T[5:0]=6'b000001; //hot to cold
#10 T[5:0]=6'b111111; //cold to hot
#10 T[5:0]=6'b001111; //hot to hot
#10 T[5:0]=6'b000001; //hot to cold
#10 T[5:0]=6'b000011; //cold to cold
end
initial
forever #5 CLK=~CLK;
endmodule
评论0