---
+ Post New Thread
Results 1 to 20 of 20
  1. #1
    Newbie level 3
    Points: 779, Level: 6

    Join Date
    Sep 2008
    Posts
    4
    Helped
    0 / 0
    Points
    779
    Level
    6

    Verilog code for frequency divider (50 Mhz to 1 kHz)

    what would the verilog code be to change a 50MHz clock to 1kHz with a reset input.

  2. #2
    Full Member level 5
    Points: 3,046, Level: 12
    lordsathish's Avatar
    Join Date
    Feb 2006
    Location
    Asia
    Posts
    247
    Helped
    31 / 31
    Points
    3,046
    Level
    12

    verilog code frequency divider

    module fredivider(clk,rst,clk_out);
    input clk,rst;
    output clk_out;
    reg counter[15];

    always @(posedege clk or negedge rst)
    begin
    if(!rst)
    counter<=16'd0;
    else
    if(counter==16'd5000)
    counter<=16'd0;
    else
    counter<=counter+1;
    end

    assign out_clk<=counter[15;

    end module







    I think this code should work...
    it might have syntax errors though...



  3. #3
    Full Member level 5
    Points: 3,287, Level: 13

    Join Date
    Dec 2004
    Location
    Munich, Germany
    Posts
    243
    Helped
    46 / 46
    Points
    3,287
    Level
    13

    Re: frequency divider

    a small modification to lordsatish code :

    use assign out_clk = (counter == 16'd5000);

    instead of assign out_clk<=counter[15;

    and declare counter as reg[15:0] counter;



  4. #4
    Full Member level 5
    Points: 3,046, Level: 12
    lordsathish's Avatar
    Join Date
    Feb 2006
    Location
    Asia
    Posts
    247
    Helped
    31 / 31
    Points
    3,046
    Level
    12

    Re: frequency divider

    The duty cycle in the previous code would be 50%
    and this modification will give you 0.02%...
    correct me if i am wrong...!



    •   Alt11th September 2008, 13:11

      advertising

        
       

  5. #5
    Full Member level 5
    Points: 3,287, Level: 13

    Join Date
    Dec 2004
    Location
    Munich, Germany
    Posts
    243
    Helped
    46 / 46
    Points
    3,287
    Level
    13

    frequency divider

    Thats true lordsatish !!!

    You would get only 0.02% duty cycle with my assignment statement

    But u can get 50% duty cycle with assign out_clk = (counter <= 16'd25000);



  6. #6
    Newbie level 3
    Points: 779, Level: 6

    Join Date
    Sep 2008
    Posts
    4
    Helped
    0 / 0
    Points
    779
    Level
    6

    frequency divider

    would

    if(counter >= 5000)

    work? would that be 1kHz?



  7. #7
    Full Member level 5
    Points: 3,046, Level: 12
    lordsathish's Avatar
    Join Date
    Feb 2006
    Location
    Asia
    Posts
    247
    Helped
    31 / 31
    Points
    3,046
    Level
    12

    Re: frequency divider

    This is a Divide by 5000 counter...
    So there is no point in the count value being greater than 5000...
    However there will be clk_out when count value is equal to 5000...
    So it will work...



  8. #8
    Full Member level 4
    Points: 3,339, Level: 13
    Black Jack's Avatar
    Join Date
    Dec 2003
    Location
    UKRAINE
    Posts
    237
    Helped
    14 / 14
    Points
    3,339
    Level
    13

    Re: frequency divider

    I think that correct value 4999!
    From 0 to 5000 you have 5001 pulses



    •   Alt15th September 2008, 07:54

      advertising

        
       

  9. #9
    Member level 1
    Points: 1,552, Level: 9

    Join Date
    Dec 2005
    Posts
    41
    Helped
    7 / 7
    Points
    1,552
    Level
    9

    Re: frequency divider

    Hope this modified code will give u a 1khz output clock for an input of 50Mhz clk with 50% duty cycle, correct am if am wrong


    module fredivider(clk,rst,clk_out);
    input clk,rst;
    output reg clk_out;
    reg [15:0] counter;

    always @(posedege clk or negedge rst)
    begin
    if(!rst)
    begin
    counter<=16'd0;
    clk_out <= 1'b0;
    end
    else
    if(counter==16'd25000)
    begin
    counter<=16'd0;
    clk_out <= ~clk_out;
    end
    else
    begin
    counter<=counter+1;
    end
    end

    endmodule


    1 members found this post helpful.

    •   Alt17th September 2008, 11:52

      advertising

        
       

  10. #10
    Newbie level 5
    Points: 774, Level: 6

    Join Date
    Oct 2008
    Posts
    9
    Helped
    0 / 0
    Points
    774
    Level
    6

    frequency divider

    Hello, i'm trying to divide 100 MHz clock by 11 and 22 to get 9.091 MHz and 4.545 MHz clock outputs.
    I'm using XC9536XL. I thought of using FDD (Double Edge Triggered Flip Flop), but it's not available to XC9536. How can i do it ? Thank's



  11. #11
    Newbie level 3
    Points: 274, Level: 3

    Join Date
    Nov 2012
    Posts
    4
    Helped
    0 / 0
    Points
    274
    Level
    3

    Re: frequency divider

    Hi,

    I tried to change this code to get a larger duty cycle.


    module coinsorter(clk,rst,top_motor);
    input clk,rst;
    output reg top_motor;
    reg [15:0] counter;
    always @(posedge clk or posedge rst)
    begin
    if(rst)
    begin
    counter<=16'd0;
    top_motor <= 1'b0;
    end
    else
    if(counter==16'd12500)
    begin
    top_motor <= 1'b1;
    counter<=counter+1;
    end
    else
    if(counter==16'd50000)
    begin
    counter <= 16'd0;
    top_motor <= 1'b0;
    end
    else
    begin
    counter<=counter+1;
    end
    end
    endmodule


    The simulation shows that it's working but when I actually apply it to the RC servo motor it doesn't work.

    Also I am trying to control this with an external switch.
    I don't want the motor to run unless the switch is on, but it keeps giving me errors whenever I try to compile (The code I posted doesn't include the switch bit, obviously)



  12. #12
    Advanced Member level 5
    Points: 13,636, Level: 28
    mrflibble's Avatar
    Join Date
    Apr 2010
    Posts
    2,468
    Helped
    618 / 614
    Points
    13,636
    Level
    28

    Re: Verilog code for frequency divider (50 Mhz to 1 kHz)

    Maybe because your pwm frequency and/or duty cycle values are wrong?

    http://www.mitchr.me/SS/batteriesReq...ory/index.html



  13. #13
    Newbie level 3
    Points: 274, Level: 3

    Join Date
    Nov 2012
    Posts
    4
    Helped
    0 / 0
    Points
    274
    Level
    3

    Re: Verilog code for frequency divider (50 Mhz to 1 kHz)

    Quote Originally Posted by mrflibble View Post
    Maybe because your pwm frequency and/or duty cycle values are wrong?

    l
    Using resistors the motor now turns 360 degrees (not just a maximum of 180). I need a frequency of 1KHz to rotate it clockwise and 2KHz for anti-clockwise.
    So I think the frequency is right because the motor does rotate with a duty cycle of 50% at 1KHz and 2KHz
    Only when I try to change the on time does it not work.



  14. #14
    Advanced Member level 5
    Points: 13,636, Level: 28
    mrflibble's Avatar
    Join Date
    Apr 2010
    Posts
    2,468
    Helped
    618 / 614
    Points
    13,636
    Level
    28

    Re: Verilog code for frequency divider (50 Mhz to 1 kHz)

    Any other handy bits of information? The information in your last post is a bit hard to guess based on your first post. :P That, and we don't even know what clock frequency you use for that coinsorter module...

    Also ...

    The simulation shows that it's working but when I actually apply it to the RC servo motor it doesn't work.
    Kindly post testbench code as well. That always helps.



  15. #15
    FvM
    FvM is offline
    Super Moderator
    Points: 171,292, Level: 98
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    27,733
    Helped
    8749 / 8749
    Points
    171,292
    Level
    98

    Re: Verilog code for frequency divider (50 Mhz to 1 kHz)

    Using resistors the motor now turns 360 degrees (not just a maximum of 180). I need a frequency of 1KHz to rotate it clockwise and 2KHz for anti-clockwise.
    Doesn't seem to refer to a standard RC servo.



  16. #16
    Advanced Member level 5
    Points: 13,636, Level: 28
    mrflibble's Avatar
    Join Date
    Apr 2010
    Posts
    2,468
    Helped
    618 / 614
    Points
    13,636
    Level
    28

    Re: Verilog code for frequency divider (50 Mhz to 1 kHz)

    Wasn't going to be the first to bring that up explicitely. Thought I'd just be the first to bring that up implicitly by including that link he probably didn't read.

    Case in point being: 50 Hz PWM frequency, pulse width on the order of 1-2 ms and all that. So that 1 kHz / 2 kHz scheme looked a bit curious....

    So a bit more information on the design probably wouldn't hurt.



  17. #17
    Newbie level 6
    Points: 105, Level: 1

    Join Date
    May 2013
    Posts
    14
    Helped
    0 / 0
    Points
    105
    Level
    1

    Re: verilog code frequency divider

    Hey i cant get 50 per duty cycle.. how would i in this code??



  18. #18
    vka
    vka is offline
    Newbie level 2
    Points: 220, Level: 2

    Join Date
    Jun 2012
    Posts
    2
    Helped
    0 / 0
    Points
    220
    Level
    2

    Re: verilog code frequency divider

    Quote Originally Posted by Tajwar View Post
    Hey i cant get 50 per duty cycle.. how would i in this code??

    pl try my code ..this will give you 50 % duty cycle

    http://technicalone.blogspot.in/2013...k-divider.html



  19. #19
    Advanced Member level 5
    Points: 13,636, Level: 28
    mrflibble's Avatar
    Join Date
    Apr 2010
    Posts
    2,468
    Helped
    618 / 614
    Points
    13,636
    Level
    28

    Re: Verilog code for frequency divider (50 Mhz to 1 kHz)

    Maybe in another 5 years this thread will have accumulated enough 1-poster fragments to start making some sense. :)



  20. #20
    Newbie level 3
    Points: 30, Level: 1

    Join Date
    Jul 2013
    Posts
    3
    Helped
    0 / 0
    Points
    30
    Level
    1

    Re: Verilog code for frequency divider (50 Mhz to 1 kHz)

    Dear All,

    I am using clock divider where you will see verilog code in followinx text:
    But I need to control the frequency using external register.

    Could you please let me know how I should do it.
    Thank you very much for your help in advance.

    module clock_divider(my_clk,clk_div,timer_divider);
    input my_clk;
    input reg [31:0] timer_divider;

    reg [31:0] count; // clock divider counter

    output reg clk_div; // clock divider output

    always @ (posedge my_clk)
    begin

    // if(count == 32'b(timer_divider)) // timer_divider should assign to register where I can control it
    if(count ==32'b1111101000)
    count <= 32'b0000; // reset to 0
    else
    count <= count+1; // increment counter
    clk_div <= (count == 32'b0000);
    end



+ Post New Thread
Please login