2
\$\begingroup\$

I am trying to make a design that toggles a sound at a rate of 120 BPM (once every .5 seconds), and I am using a 50 MHz clock. Here's the tone module:

module tone(
input clk, 
input enable,
output reg speaker);

parameter clkdivider = 25000000/440/2;

reg [14:0] counter;
always @(posedge clk) if(counter==0) counter <= clkdivider-1; else counter <= counter-1;

always @(posedge clk) if(counter==0) speaker <= enable ? ~speaker : 0;
endmodule

and here's the toggling module:

module lockstep_main(input clk, output speaker);

reg enable;

tone(.clk(clk), .enable(enable), .speaker(speaker));

parameter beat = 50000000/2;
reg [14:0] counter;
always @(posedge clk) if(counter==0) counter <= beat-1; else counter <= counter-1;

always @(posedge clk) if(counter==0) enable <= ~enable;

endmodule

As it is, the design doesn't work properly, just causing the speaker to emit garbled tones. Also, if I try setting the enable register to 1 initially and not changing it, I still get no sound. What could be the issue here?

\$\endgroup\$
2
  • \$\begingroup\$ You have no reset configuration so the registers will start off full of garbage or 'X'. \$\endgroup\$
    – pjc50
    Commented Dec 13, 2014 at 21:46
  • \$\begingroup\$ I tried adding an initial set to 0, but that didn't work. \$\endgroup\$
    – aftrumpet
    Commented Dec 13, 2014 at 23:40

1 Answer 1

2
\$\begingroup\$

50000000/2 requires 25 bits, not 15 (log base 2 of 25000000 is 24.575, the ceil of that is 25). In the second module, counter gets set to 30783 instead of 25000000 so the 'BPM' is ~97456 instead of 120.

\$\endgroup\$
1
  • \$\begingroup\$ Thank you so much! I misread a powers of 2 table, causing me to think 15 bits was sufficient. \$\endgroup\$
    – aftrumpet
    Commented Dec 14, 2014 at 15:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.