Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
11 years ago

I have problem to get counter to PWM

I'm trying to assign _t3 value from _t1, before I reset _t1 to 0. And I use _t3 value to run next pwm to out

Here is the code.

module convert_Ref(input clk, input in, output reg out);

reg [15: 0] _t1, _t2, _t3, _i1;

reg _tA, _tB;

initial begin

_t1 = 0;

_t2 = 0;

_t3 = 0;

_tA = 0;

_tB = 0;

_i1 = 0;

end

always @(posedge clk) begin

if (in) begin

if (_tA) begin

_t3 <= _t1;

_t1 <= 0;

_t2 <= 0;

_tB <= 1;

end

_tA <= 0;

_t1 <= _t1 + 1;

end

else begin

if (!_tA) begin

_tB <= 0;

end

_tA <= 1;

_t2 <= _t2 + 1;

end

if (_tB) begin

_i1 <= _i1 + 1;

if (_i1 < _t3)

out <= 1;

else

out <= 0;

if (_i1 == (_t3 + 100))

_i1 <= 0;

end

end

1 Reply

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Rule number 1 of coding: Make it readable :). Please use meaningful signal names. '_t1' doesn't tell me anything.

    A tip when posting code: wrap the code tags around any text you post. e.g.

    module convert_Ref(input clk, input in, output reg out);
    reg  _t1, _t2, _t3, _i1;
    reg _tA, _tB;
    

    Then you won't end up with unwanted smilies B) littered about your code and it will maintain any (very helpful) indentation you may have included in your original code...

    As for the actual problem with the code - I'm really not sure what you're trying to achieve. You mention PWM in the title but I really can't see what you're trying to do.

    For starters: counter '_t2' increments under certain conditions but nothing is dependent on it. So, what is it there for?

    Perhaps a description of what it is you're trying to do would be the place to start...

    Regards,

    Alex