有限状态机时序电路设计之检测连续四个0或1的个数

有限状态机时序电路之连续检测四个0或1的个数

设计一个4连续0或者4个连续1的序列检测FSM,定义一个长序列,在七段管上分别显示检测的4个连续04个连续1的个数。显示连续0和连续1的个数在七段管上的显示,分别用函数和任务实现。

 比如:100000110011111000011111.。。。。

 检测连0个数: 000011111111111111222222。。。。 
 1个数: 000000000000011111111122.。。


module show(clk,clr,x,hex1,hex2);
input clk,clr,x; 
output reg[6:0] hex1,hex2; 
reg[3:0] state;
reg[3:0] count1=0,count2=0;
parameter 	S0=4'b0000,S1=4'b0001,S2=4'b0010,S3=4'b0011,S4=4'b0100,S5=4'b0101,S6=4'b0110,S7=4'b0111,S8=4'b1000;//定义八个好状态
always @(posedge clk or posedge clr)
begin     
         if(clr) state=S0;        //异步复位,s0为起始状态
	      else 
	        case(state)//状态转移过程
	           S0:begin 	
                  if(x) begin state<=S5; taskMatch(count1,hex1);hex2<=funMatch(count2); end
		          else begin state<=S1; taskMatch(count1,hex1);hex2<=funMatch(count2); end
                  end
	           S1:begin 	
                  if(x) begin state <=S5;taskMatch(count1,hex1);hex2<=funMatch(count2); end
			      else begin state <=S2; taskMatch(count1,hex1);hex2<=funMatch(count2); end
                  end
	           S2:begin	
                  if(x) begin state <=S5; taskMatch(count1,hex1);hex2<=funMatch(count2); end
			      else begin state <=S3; taskMatch(count1,hex1);hex2<=funMatch(count2); end
                  end
	           S3:begin  
                  if(x) begin state <=S5; taskMatch(count1,hex1);hex2 <=funMatch(count2); end
			      else begin state<=S4; count1 <= count1+1;taskMatch(count1,hex1);hex2<=funMatch(count2); end
                  end
               S4:begin  
                  if(x) begin state <=S5; taskMatch(count1,hex1);hex2<=funMatch(count2); end
			      else begin state<=S1; ; taskMatch(count1,hex1);hex2<=funMatch(count2); end
                  end
               S5:begin  
                  if(x) begin state <=S6; taskMatch(count1,hex1);hex2<=funMatch(count2); end
			      else begin state<=S1; taskMatch(count1,hex1);hex2<=funMatch(count2); end
                  end
               S6:begin  
                  if(x) begin state <=S7; taskMatch(count1,hex1);hex2<=funMatch(count2); end
			      else begin state<=S1; taskMatch(count1,hex1);hex2<=funMatch(count2); end
                  end
               S7:begin  
                  if(x) begin state <=S8; count2 <= count2+1;taskMatch(count1,hex1);hex2<=funMatch(count2); end
			      else begin state<=S1; taskMatch(count1,hex1);hex2<=funMatch(count2); end
                  end
               S8:begin  
                  if(x) begin state <=S5;taskMatch(count1,hex1);hex2<=funMatch(count2); end
			      else begin state<=S1; taskMatch(count1,hex1);hex2<=funMatch(count2); end
                  end
	           default:begin  
	              state<=S0;taskMatch(count1,hex1);hex2<=funMatch(count2);
	              end   
	         endcase  
end



task taskMatch;//七段管显示数字的任务
input [3:0] a;
output [6:0] b;
case(a)
    4'H0:b<=7'b1000000;   
    4'H1:b<=7'b1111001;  
    4'H2:b<=7'b0100100; 
    4'H3:b<=7'b0110000;   
    4'H4:b<=7'b0011001;  
    4'H5:b<=7'b0010010;  
    4'H6:b<=7'b0000010; 
    4'H7:b<=7'b1111000;  
    4'H8:b<=7'b0000000;
    4'H9:b<=7'b0011000;  
    4'HA:b<=7'b0001000;   
    4'Hb:b<=7'b0000011;  
    4'Hc:b<=7'b1000110;  
    4'Hd:b<=7'b0100001;   
    4'He:b<=7'b0000110;  
    4'Hf:b<=7'b0001110;
    default:b<=7'b1111111;
endcase
endtask

function [6:0] funMatch;//七段管显示数字的函数
input [3:0] a;
case(a)
    4'H0:funMatch=7'b1000000;   
    4'H1:funMatch=7'b1111001;  
    4'H2:funMatch=7'b0100100; 
    4'H3:funMatch=7'b0110000;   
    4'H4:funMatch=7'b0011001;  
    4'H5:funMatch=7'b0010010;  
    4'H6:funMatch=7'b0000010; 
    4'H7:funMatch=7'b1111000;  
    4'H8:funMatch=7'b0000000;
    4'H9:funMatch=7'b0011000;  
    4'HA:funMatch=7'b0001000;   
    4'Hb:funMatch=7'b0000011;  
    4'Hc:funMatch=7'b1000110;  
    4'Hd:funMatch=7'b0100001;   
    4'He:funMatch=7'b0000110;  
    4'Hf:funMatch=7'b0001110;
    default:funMatch=7'b1111111;
endcase
endfunction
endmodule 


一、实验目的: 1、深入了解与掌握同步时序逻辑电路的设计过程; 2、了解74LS74、74LS08、74LS32及74LS04芯片的功能; 3、能够根据电路图连接好实物图,并实现其功能。学会设计过程中的检验与完善。 二、实验内容描述: 题目:“1 1 1”序列检测器。 原始条件:使用D触发器( 74 LS 74 )、“与”门 ( 74 LS 08 )、“或”门( 74 LS 32 )、非门 ( 74 LS 04 ),设计“1 1 1”序列检测器。 集成电路引脚图: D触发器( 74 LS 74 ) “与”门 ( 74 LS 08 ) “或........ 三、实验设计过程: 第1步,画出原始状态图和状态表。 根据任务书要求,设计的序列检测器有一个外部输入x和一个外部输出Z。输入和输出的逻辑关系为:当外部输入x第一个为“1”,外部输出Z为“0”;当外部输入x第二个为“1”,外部输出Z为“0”;当外部输入x第三个为“1”,外部输出Z才为“1”。假定有一个外部输入x序列以及外部输出Z为: 输入x: 0 1 0 1 1 1 0 1 1 1 1 0 1 输出Z: 0 0 0 0 0 1 0 0 0 1 1 0 0 要判别序列检测器是否连续接收了“111”,电路必须用不同的状态记载外部输入x的值。假设电路的初始状态为A,x输入第一个“1”,检测器状态由A装换到B,用状态B记载检测器接受了111序列的第一个“1”,这时外部输出Z=0;x输入第二个“1”,检测器状态由B装换到C,用状态C记载检测器接受了111序列的第二个“1”,外部输出Z=0;x输入第三个“1”,检测器状态由C装换到D,外部输出Z=1。然后再根据外部输入及其他情况时的状态转移,写出相应的输出。以上分析了序列检测器工作,由此可画出图7-1所示的原始状态图。根据原始状态图可列出原始状态表,如表7-2所示。 现态 次态/输出 x = 0 x = 1 A A / 0 B / 0 B A / 0 C / 0 C A / 0 D / 1 D A / 0 D / 1 (表 7-2 原始状态表) (图
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值