Pony279 发表于 2012-5-31 10:46:08

LED 实验求助,帮忙看个怪异的问题

本帖最后由 Pony279 于 2012-5-31 10:47 编辑

大家帮忙看看这段代码,现在是没问题的(顶层是 light_water),3个LED像个计数器一样,每隔 1s 加 1,

但是现在的问题是,当我把下面的被注释的代码恢复以后(也就是加上复位功能),

LED显示就不正常了(rst_n脚已经被我接到高电平了),只有 LED 和 LED 会亮,现象相当于一个 2 位的计数器。。。,LED对应的引脚没反应module clk_div(
        inputclk_in,
        output p);                        // output pulse
       
        parameter DIV =50_000_000;        // freq(clk_in) / DIV
        parameter BIT_WIDTH = 26;        // the bit-width of the counter
        reg cnt;

        always@ (posedge clk_in) begin
                        if(cnt == DIV-1)
                                cnt <= 0;
                        else
                                cnt <= cnt+1'b1;
        end
        assign p = (cnt == DIV-1)?1'b1:1'b0;
endmodule

module led_display(
        input clk
        ,output led_data
//        ,input rst_n
);
        reg led_data_r;
        always@(posedge clk
//                ,negedge rst_n
                )begin
//                if(!rst_n)
//                        led_data_r <= 0;
//                else
                        led_data_r <= led_data_r + 1'b1;
        end
        assign led_data = (~led_data_r);
endmodule

module light_water(
        input clk_in,
        output led_data,
        output led
//        ,input rst_n
);

        wire led_clk;
        clk_div clk_div_inst(
                .clk_in (clk_in),
                .p (led_clk)
        );
       
        led_display led_display_inst(
                .clk (led_clk)
                , .led_data (led_data)               
//                , .rst_n (rst_n)
        );
       
        assign led = led_data;

endmodule

Pony279 发表于 2012-5-31 11:12:42

大家帮忙看看啊,

我昨晚调这个调到 2 点多。。。

玩了 一年多的 C/C++ 都没这么蛋疼过。。。感觉真有点耻辱啊。。。{:mad:}

wye11083 发表于 2012-5-31 12:58:37

你不会写个TestBench啊?仿真不就知道怎么回事了?

Pony279 发表于 2012-5-31 13:11:18

wye11083 发表于 2012-5-31 12:58 static/image/common/back.gif
你不会写个TestBench啊?仿真不就知道怎么回事了?

刚入门。。。不会的。。。

Pony279 发表于 2012-5-31 18:28:56

受不了,然后装了 quartus II 11.1 试了下,两种版本的代码都没问题了。。。

之前用的是 8.1 的。。。难道是软件的 BUG 吗。。。

superluan 发表于 2012-5-31 19:17:00

你在8.1版本下,看下综合和实现当中,有没有警告或者错误。
表面看,真的看不出什么问题来。

Pony279 发表于 2012-5-31 19:22:18

superluan 发表于 2012-5-31 19:17 static/image/common/back.gif
你在8.1版本下,看下综合和实现当中,有没有警告或者错误。
表面看,真的看不出什么问题来。 ...

已经被我卸载掉了。。。
刚开始学,真不想看那些警告,错误倒是没有,

我之前是试过软件仿真过没问题的,(不过用的是比较简单的图形化的方法的,所以要把分频系数调低了才能很快仿真出来)

我做这个是用来偷懒的,数电的课程设计要做这个,看同学画电路图挺蛋疼的,因为我基本天天都敲代码,所以看着代码觉得特有亲切感 : )

Pony279 发表于 2012-6-5 00:19:05

现在又出现问题了,,,实在是诡异啊。。。难道没有人遇到过和我一样的问题么。。。那我作为一个新人真的是奇葩了{:mad:}

Pony279 发表于 2012-6-5 15:32:58

唉,好无助啊。。。问题似乎是解决了,可是还是不明白为什么。。。

问题代码似乎是 /*---*/ 包围的地方,有三种方案,看注释,不知没有有人能帮我解释一下?module ClockDivision(
          inputclk
        , input        rst_n_d                                /* 异步清零 */
        , input div_factor
        , output out_pulse                        /* 输出时钟脉冲 */
);

        parameter CLK_DIV_BIT_WIDTH = 32;        // the bit-width of the counter

        wire counter_rst_n;       
        wire count;       
        Counter #(CLK_DIV_BIT_WIDTH) ClockDivision_Counter(
                  .clk (clk)
                , .rst_n_d (counter_rst_n)
                , .count (count)
        );
       
        reg out_pulse_r;                /* 输出脉冲寄存器 */
       
        always@ (posedge clk) begin
                        /* 当上升沿之前计数到 div_factor-1'b1,就可以输出脉冲了 */
                        out_pulse_r <= ( (count+1'b1) == div_factor)?1'b1:1'b0 ;
        end
       
        /* 当计数到设定的值的时候,使能异步复位,直接清零 */
        assign counter_rst_n = (!rst_n_d)?1'b0:( (count == div_factor)?1'b0:1'b1 );
       
        /*---------------------------------------------------------------*/
        /* 为什么这个会有问题呢,我想在时钟下降沿清零啊!!!为什么呢?为什么呢? */
        //assign out_pulse = clk?out_pulse_r:1'b0;
       
        /* 如果分频系数为 1,直接输出时钟,这种方式是可行的 */
        wire sel = div_factor==1?1'b1:1'b0;
        assign out_pulse = sel?clk:out_pulse_r;
       
        /* 这种方式在分频系数为 1 的时候输出是不会跳的 */
        //assign out_pulse = out_pulse_r;
                /*---------------------------------------------------------------*/
       
endmodule
页: [1]
查看完整版本: LED 实验求助,帮忙看个怪异的问题