When the ‘light’ comes on, is it an LED?

A couple days ago I decided to try to blink an LED on the 2019 Hackaday Supercon badge. I tried and failed with many iterations of what I thought would work easily. Yesterday I decided to look up a blink example on the web to see how they were doing it. I could not get any of them to work. What was I doing wrong?

This morning as I was making coffee (pour-over) I started thinking about what I should try next. I have a counter that when it gets to 500000 it should turn on the LED. However, I already have the LED on. So, I decided to turn off the LED when the counter got up to 500000. I didn’t notice in the nesting that counter got reset to 0 (32’b0) in the wrong place and would add on and reset to zero right away. ::smack forehead::

always @(posedge clk) begin
counter <= counter + 1'b1;
if (counter > 500000) begin
ledc[5] = 1'b0;
end else begin
ledc[5] = 1'b1;
end
counter <= 32'b0;
end

Even when I took moved the reset of the counter to the proper place the LED would not turn off when counter reached 500000. UGH!, because of the else! OK time to change the if statement to if (counter[28]). Still the LED would not turn off.

Poured the first cup of coffee and finished pouring water over the grounds. Caffeine must be helping, change the code to

always @(posedge clk) begin
counter <= counter + 1'b1;
end


always @(posedge clk) begin

if (counter > 500000)begin
ledc[5] = 1'b1;
end else begin
ledc[5] = 1'b0;
end

That seemed to look OK, but it still didn’t turn off the LED until the the counter got above 500000. Was the clock speed so fast that it got to 500000 too quickly? How about resetting counter when it got to one million? That way the light should be off until 500001 and then back on. Right? … Nope! The LED was still not turning off. Whether I started with the LED off or on the counter would not make a change to the LED.

In a workshop code they assigned LEDs to come on with button presses. Something like press button one and turn on LED 2. When you released the button the LED turned off. They used assign ledc[2] = btn[1]; , and I was using assign ledc[5] = 1'b1; to initialize LED 5 to be on at start up.

I tried many different iterations of if (counter > 500000) and if counter[28] that didn’t work to turn off the LED. The ‘light’ came on above my head while thinking about how they were toggling the LED with a button. I decided to try it sort of like I thought they were doing.

assign ledc[5] = counter[21];

always @(posedge clk) begin
counter <= counter + 1'b1;
end

This worked just fine. The LED number 5 was blinking its fool head off.

A little bit more experimenting is going to help me figure out why trying to use the counter with an if statement would not work. I am starting to think that it is because of the assign command.

One thought on “When the ‘light’ comes on, is it an LED?”

Comments are closed.