bestwyysx 发表于 2009-12-8 15:09:27

新手请教一个PWM的程序。

module shiyan01 (iclk,rst,oclk);
input iclk;
input rst;
output oclk;
reg counter;
reg off;
reg pwmwidth;
reg pwmperiod;
reg counter1;
reg clk;
always @ (posedge iclk or negedge rst)
begin
if(!rst)
begin
   counter<=0;
   pwmperiod<=16'b1000000000000000;
end
else
begin
   if(counter>=pwmperiod-1)
    counter<=0;
   else
    counter<=counter+1;
end
end
always @ (posedge iclk or negedge rst)
begin
if(!rst)
pwmwidth<=16'b0000000100000000;
else
begin
   if(counter>=pwmwidth)
    off<=6'b111111;
   else
    off<=6'b000000;
end
end
assign oclk=off;
endmodule
为什么我写的这个程序的输出一直是0,没有形成波形呢?

bestwyysx 发表于 2009-12-8 15:25:39

有人能指点一下吗?

ngzhang 发表于 2009-12-8 18:27:48

reset的高低是否有问题?

bestwyysx 发表于 2009-12-8 18:42:27

没有吧?下降沿复位啊

ngzhang 发表于 2009-12-8 19:00:12

我拿你的代码(verilog应当称之为代码,而不是所谓程序以示区别),跑了仿真。能出来波形。
testbench如下:

module shiyan01_tb;

        // Inputs
        reg iclk;
        reg rst;

        // Outputs
        wire oclk;

        // Instantiate the Unit Under Test (UUT)
        shiyan01 uut (
                .iclk(iclk),
                .rst(rst),
                .oclk(oclk)
        );

        initial begin
                // Initialize Inputs
                iclk = 0;
                rst = 1;

                // Wait 100 ns for global reset to finish
                #100;
                rst = 0;
                #100;
                rst = 1;
      
                // Add stimulus here

        end
      
               
                always #10 iclk = ~iclk;
endmodule

bestwyysx 发表于 2009-12-8 19:08:32

我也用MODSIM做过仿真的,输出一直是0,而且我下到板子上也是0,没变化

bestwyysx 发表于 2009-12-8 19:11:29

`timescale 1ns/100ps
module shiyan01_tb;

wireoclk   ;
reg    rst   ;
reg    iclk   ;

initial
begin
      iclk=0;
      rst=1;
      #20 rst=0;
      #120 rst=1;
      #10000 $stop;
end

always #50 iclk=~iclk;

shiyan01
   DUT(
       .oclk (oclk ) ,
      .rst (rst ) ,
      .iclk (iclk ) );

endmodule
这是我的TB

ngzhang 发表于 2009-12-8 19:12:51

你仿真的时间太短了,加长100万倍看看。

bestwyysx 发表于 2009-12-8 19:18:38

仿真时间加长了也没用啊

ngzhang 发表于 2009-12-8 19:53:46

。。。先用我写的tb跑1秒看看。。。

http://cache.amobbs.com/bbs_upload782111/files_23/ourdev_512073.jpg
(原文件名:pwm.jpg)

bestwyysx 发表于 2009-12-8 20:24:13

恩,谢谢你,确实是仿真时间太短了

weidadejang 发表于 2017-12-27 13:42:05

没看懂你这个!我最近也在搞CPLD的PWM 还没有摸到门!
页: [1]
查看完整版本: 新手请教一个PWM的程序。