| 
 | 
下面我们来进行建模和仿真下板。 
新建一个工程文件夹。 
 
设计代码如下:  
/*  
模块名称:ledrun  
模块功能:四个灯做流水,每一个灯亮一秒钟  
编写时间:2016-08-12  
作者:至芯科技----奋斗的小孩  
邮箱:zxopenhxs@126.com  
*/ 
module ledrun (clk, rst_n, led);  
        input clk;  
        input rst_n; //低电平有效  
        output reg [3:0] led; 
 
        parameter T1s = 50_000_000;//参数设置  
        reg [25:0] count;//计数器(需要计数50M个周期)  
        reg [1:0] state;//四个状态  
        localparam         s0 = 2'b00,//定义四个状态机参数  
                                s1 = 2'b01,  
                                s2 = 2'b10,  
                                s3 = 2'b11;  
        always @ (posedge clk or negedge rst_n)  
                begin  
                        if (!rst_n)  
                                begin  
                                        state <= s0;  
                                        count <= 26'd0;  
                                        led <= 4'b0000;  
                                end  
                        else  
                                begin//状态机的关键字case()•••endcase 
                                        case(state) //按照设计的状态转移图进行描述  
                                        s0        :         begin  
                                                                if (count < T1s - 1)  
                                                                        begin  
                                                                                led <= 4'b0111;  
                                                                                state <= s0;  
                                                                                count <= count + 1;  
                                                                        end  
                                                                else  
                                                                        begin  
                                                                                count <= 0;  
                                                                                state <= s1;  
                                                                        end  
                                                        end  
                                        s1         :         begin  
                                                                if (count < T1s - 1)  
                                                                        begin  
                                                                                led <= 4'b1011;  
                                                                                state <= s1;  
                                                                                count <= count + 1; 
                                                                        end  
                                                                else  
                                                                        begin  
                                                                                count <= 0;  
                                                                                state <= s2;  
                                                                        end  
                                                        end  
                                        s2        :         begin  
                                                                if (count < T1s - 1)  
                                                                        begin  
                                                                                led <= 4'b1101;  
                                                                                state <= s2;  
                                                                                count <= count + 1;  
                                                                        end  
                                                                else  
                                                                        begin  
                                                                                count <= 0;  
                                                                                state <= s3;  
                                                                        end  
                                                        end 
                                        s3         :         begin  
                                                                if (count < T1s - 1)  
                                                                        begin  
                                                                                led <= 4'b1110;  
                                                                                state <= s3;  
                                                                                count <= count + 1;  
                                                                        end  
                                                                else  
                                                                        begin  
                                                                                count <= 0;  
                                                                                state <= s0;  
                                                                        end  
                                                        end  
                                        default        :         state <= s0;  
                                        endcase  
                                end  
                end 
endmodule 
 |   
 
 
 
 |