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

来源:互联网 发布:淘宝产品推广方案 编辑:程序博客网 时间:2024/06/06 07:33

有限状态机时序电路之连续检测四个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  endtask 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;endcaseendtaskfunction [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;endcaseendfunctionendmodule 


1 0
原创粉丝点击