I'm very new to verilog and I am stuck on a project I am writing to get better at it. I have a button and an LED on my board, and I want the button to increment a counter which makes the led flash faster. This seems to work in theory but I cannot get it to work in practice. The switch won't make the led flash faster, it seems to do some strange things.
My current code, please inform me of any issues with what I'm doing even if it does not cause my issue here as I am trying to learn the langauge and constructs.
`timescale 1ns / 1ps
module LedFlash(CLK100MHZ, led0, sw0, btn0);
input CLK100MHZ;
output reg led0;
input sw0;
input btn0;
reg [25:0] clockTick = 0;
reg [1:0]currentlyLighting = 0;
reg [3:0] speedFactor = 0;
reg [1:0]oldButton = 0;
reg [1:0]buttonValue = 0;
always @(posedge CLK100MHZ)
begin
led0 <= 0;
buttonValue <= 0;
if(oldButton != btn0 && btn0 == 1)
buttonValue <= 1;
oldButton <= btn0;
if(clockTick == 0)
currentlyLighting <= !currentlyLighting;
if(currentlyLighting)
led0 <= 1;
if(buttonValue) begin
speedFactor <= speedFactor + 1;
end
clockTick <= clockTick + speedFactor;
end
endmodule
Here is a gif of a couple presses of the button. Only the times when speedfactor increments is when I actually press the button.
I would provide more presses, but it just gets more strange from there, clearly I am doing something wrong. After that initial press of the button, it strays away from what I expect. Sometimes it turns off, sometimes stays solid, slows down, speeds up.....
**Also, if I replace this line :
clockTick <= clockTick + speedFactor;
with
clockTick <= clockTick + 10;
for example, it will flash fast like I would expect it to. I'm guessing I am doing something wrong with the bit addition, although the probe seems to tell me otherwise.
**
Fixed code :
`timescale 1ns / 1ps
module LedFlash(CLK100MHZ, led0, sw0, btn0);
input CLK100MHZ;
output reg led0;
input sw0;
input btn0;
reg [25:0] clockTick = 0;
reg currentlyLighting = 0;
reg [3:0] speedFactor = 0;
reg oldButton = 0;
reg buttonValue = 0;
always @(posedge CLK100MHZ)
begin
led0 <= 0;
buttonValue <= 0;
if(oldButton != btn0 && btn0 == 1)
buttonValue <= 1;
oldButton <= btn0;
currentlyLighting <= clockTick[25];
if(currentlyLighting && sw0)
led0 <= 1;
if(buttonValue) begin
speedFactor <= speedFactor + 1;
end
clockTick <= clockTick + speedFactor;
end
endmodule
