Mark Baseggio
Junior Member level 2
- Joined
- Nov 2, 2013
- Messages
- 22
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 249
module clock_div(
input clk,
input rst,
output mpu_clk
);
parameter period = 50;
parameter halfperiod = 25;
reg mpu_clk;
wire rst,clk;
reg [5:0] countvalue;
always @(posedge clk) begin
if(rst) begin
countvalue <= 5'b0;
mpu_clk <= 1'b0;
end
else begin
if(countvalue == period - 1'b1) begin
countvalue <= 1'b0;
mpu_clk <= 1'b0;
end
else countvalue <= countvalue + 1'b1;
if(countvalue == halfperiod) mpu_clk <= 1'b1;
end
end
endmodule
NET "mpu_clk" LOC = P1;
Code Verilog - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 always @(posedge clk) begin // the lack of rst in this sensitivity list implies that it's a synchronous reset if(rst) begin countvalue <= 5'b0; mpu_clk <= 1'b0; end else begin if(countvalue == halfperiod - 1'b1) begin countvalue <= 1'b0; mpu_clk <= ~mpu_clk; // toggle mpu clock signal end else countvalue <= countvalue + 1'b1; end end
Oh and one last thing. Eventually I will also be needing a 20Mhz clock to communicate with a SPI SRAM that I will be adding. Would it be advisable to generate both signals in the same module?
Line 16: Redeclaration of ansi port mpu_clk is not allowed
Line 17: Redeclaration of ansi port rst is not allowed
FF/Latch <mpu_clock_divider/countvalue_5> has a constant value of 0 in block <mojo_top>. This FF/Latch will be trimmed during the optimization process.
Thanks for your quick and helpful reply! I just noticed that I am getting a few warnings related to this module when I generate the programming file:
Should I be declaring them as reg and wire up in the module section?Code:Line 16: Redeclaration of ansi port mpu_clk is not allowed Line 17: Redeclaration of ansi port rst is not allowed
One other warning I don't understand:
Code:FF/Latch <mpu_clock_divider/countvalue_5> has a constant value of 0 in block <mojo_top>. This FF/Latch will be trimmed during the optimization process.
it looks like this is optimizing away the counter that I use, but inexplicably the routine still works. So it must not be. I am scratching my head.
Also, I noticed that you added the comment "// the lack of rst in this sensitivity list implies that it's a synchronous reset" Can you please explain that one to me a bit more, I'm not 100% sure what you mean. I assume that because I am not passing the rst value to sensitivity that rst won't ever be true so that code block will never work.. but somehow I think I am wrong.
always @(posedge clk) begin
if(rst) begin
countvalue <= 5'b0;
mpu_clk <= 1'b0;
end
else begin
if(countvalue == PERIOD - 1'b1) begin
countvalue <= 1'b0;
mpu_clk <= ~mpu_clk; // toggle mpu clock signal
end
if (!clk_switch && mpu_clk) begin
//clock stopes high if the clk_switch is low.
end
else countvalue <= countvalue + 1'b1;
end
end
Right now I'm just trying to figure out if I can use the same module for different buttons...
Code Verilog - [expand] 1 countvalue <= 1'b0; // you probably mean 5'b00000 here or 5'd0. But right now you are using a 1-bit wide value...
module debounce(
input clk,
input PB, // "PB" is the glitchy, asynchronous to clk, active low push-button signal
// from which we make three outputs, all synchronous to the clock
output reg PB_state, // 1 as long as the push-button is active (down)
output PB_down, // 1 for one clock cycle when the push-button goes down (i.e. just pushed)
output PB_up // 1 for one clock cycle when the push-button goes up (i.e. just released)
);
// First use two flip-flops to synchronize the PB signal the "clk" clock domain
reg PB_sync_0; always @(posedge clk) PB_sync_0 <= ~PB; // invert PB to make PB_sync_0 active high
reg PB_sync_1; always @(posedge clk) PB_sync_1 <= PB_sync_0;
// Next declare a 16-bits counter
reg [15:0] PB_cnt;
// When the push-button is pushed or released, we increment the counter
// The counter has to be maxed out before we decide that the push-button state has changed
wire PB_idle = (PB_state==PB_sync_1);
wire PB_cnt_max = &PB_cnt; // true when all bits of PB_cnt are 1's
always @(posedge clk)
if(PB_idle)
PB_cnt <= 0; // nothing's going on
else
begin
PB_cnt <= PB_cnt + 16'd1; // something's going on, increment the counter
if(PB_cnt_max) PB_state <= ~PB_state; // if the counter is maxed out, PB changed!
end
assign PB_down = ~PB_idle & PB_cnt_max & ~PB_state;
assign PB_up = ~PB_idle & PB_cnt_max & PB_state;
endmodule
module mpu_clock_div(
input clk,
input rst,
input clk_en,
input step_press,
output reg mpu_clk
);
localparam PERIOD = 24; // 1Mhz with 50% duty cycle.
reg [4:0] countvalue = 5'b0;
always @(posedge clk) begin
if(rst) begin
countvalue <= 5'b0;
mpu_clk <= 1'b0;
end
else begin
if(countvalue == PERIOD) begin
countvalue <= 5'b0; // reset counter
mpu_clk <= ~mpu_clk; // toggle mpu clock signal
end
if (clk_en && mpu_clk) begin
//clock stops high when clk_en is true.
end
else countvalue <= countvalue + 5'b1;
end
end
endmodule
Is there a better approach than feeding the debounce routine a separate clock to slow down the shift register?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?