sheafh

心得:非阻塞赋值的实际案例应用

0
阅读(1380)

根据一个自己写的案例来实际说明非阻塞赋值的用法。

/*******************************************************************/

//本实验完成的功能为,从0开始计数到60,再从60减数到40,再反复从40到60
module zy(clk,bc2,bc1,shu_decode);
 input  clk;
 output bc2,bc1;
 output [6:0]shu_decode;

//从seg[7]到seg[0],分别代表从gfedcba,高位7为g,低位为a  阴极数码管,低电平点通
 

parameter  s0 = 7'b1000000,          

                 s1 = 7'b1111001, 
                 s2 = 7'b0100100,
                 s3 = 7'b0110000,
                 s4 = 7'b0011001,
                 s5 = 7'b0010010,
                 s6 = 7'b0000010,
                 s7 = 7'b1111000,
                 s8 = 7'b0000000,
                 s9 = 7'b0011000,
                 sa = 7'b0001000,
                 sb = 7'b0000011, 
                 sc = 7'b1000110,
                 sd = 7'b0100001, 
                 se = 7'b0000110, 
                 sf = 7'b0001110;
 
 reg bc2,bc1;
 reg [6:0] shu_decode;
 reg [21:0] led_reg;

 always@(posedge clk)
  led_reg<=led_reg+1'b1;
  
 reg[7:0] DATA;
 reg[3:0] DISP;
 
 reg con="1"'b1;
 
 always@(posedge clk)   //从40-60计数功能
  begin
      if(con)
    begin  
     if(&led_reg[21:0])
       DATA<=DATA+1'b1;
    // 这里,当DATA[7:4]==4'b0110时才可以触发条件,而在数码管上显示的时候,这个时候是59,而显示59的时候,程序其实已经走到了60了

    if(DATA[7:4]==4'b0110)   
       begin
        DATA[7:4]<=4'b0110;
        DATA[3:0]<=4'b0000;
        con<=1'b0;
       end
     if(DATA[3:0]==4'b1010)
       begin
        DATA[3:0]<=4'b0000;
        DATA[7:4]<=DATA[7:4]+1'b1;
       end
       
    end
    
   else if(!con)
    begin
     if(&led_reg[21:0])
       DATA<=DATA-1'b1;    
     if(DATA[3:0]==4'b1111)  //非阻塞赋值语句要多考虑一步
       begin
        DATA[3:0]<=4'b1001;
       end
     if(DATA[7:4]==4'b0011)
       begin
           DATA[7:4]<=4'b0100;
        DATA[3:0]<=4'b0000;
        con<=1'b1;
       end
    end
    
  end
  
 always @ (led_reg[15])
  begin
   if(&led_reg[15])
    begin
     bc1<=1'b0;
     bc2<=1'b1;
     DISP<=DATA[7:4];
     case(DISP)
      4'b0000:shu_decode<=s0;
      4'b0001:shu_decode<=s1;
      4'b0010:shu_decode<=s2;
      4'b0011:shu_decode<=s3;
      4'b0100:shu_decode<=s4;
      4'b0101:shu_decode<=s5;
      4'b0110:shu_decode<=s6;
      4'b0111:shu_decode<=s7;
      4'b1000:shu_decode<=s8;
      4'b1001:shu_decode<=s9;
     endcase
    end
   else
    begin
     bc1<=1'b1;
     bc2<=1'b0;
     DISP<=DATA[3:0];
     case(DISP)
      4'b0000:shu_decode<=s0;
      4'b0001:shu_decode<=s1;
      4'b0010:shu_decode<=s2;
      4'b0011:shu_decode<=s3;
      4'b0100:shu_decode<=s4;
      4'b0101:shu_decode<=s5;
      4'b0110:shu_decode<=s6;
      4'b0111:shu_decode<=s7;
      4'b1000:shu_decode<=s8;
      4'b1001:shu_decode<=s9;
     endcase
    end
  end
endmodule
/*******************************************************************/

 

在这个案例里面,最典型的一个地方就是,要理解到非阻塞赋值的一个核心关键点在于非阻塞的赋值显示出来的是前一时刻的信息,而程序内走的才是当下时刻的信息。