Created
January 25, 2018 05:40
-
-
Save glennklockwood/d2dde3d9c58cc5fda173f502ba6cf19a to your computer and use it in GitHub Desktop.
Verilog Howto
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
always @(posedge clk) | |
cnt = cnt + 1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
output wire led; | |
input wire clk; | |
reg [25:0] cnt; | |
reg led_state; | |
always @(posedge clk) begin | |
cnt = cnt + 1; | |
if (cnt > 50000000) begin | |
// incrementing led_state causes it to alternate between 1 and 0 | |
led_state = led_state + 1; | |
// reset counter so that we count back up to exactly one second | |
cnt = 0; | |
end | |
end | |
assign led = led_state; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
output wire led; // declare a wire that we'll use for output | |
input wire clk; // our 50 MHz clock actually needs to be wired in as well | |
reg [25:0] cnt; | |
reg led_state; // our 1-bit register to store the state of our LED | |
always @(posedge clk) begin | |
cnt = cnt + 1; | |
if (cnt > 50000000) | |
led_state = 0; | |
else | |
led_state = 1; | |
end | |
// connect one end of the led output wire to our led_state register | |
assign led = led_state; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Note: [25:0] means a 26-bit register whose bits are labeled from 0 to 25, | |
// and the most significant bit (#25) comes first (big endian) | |
reg [25:0] cnt; | |
always @(posedge clk) begin // "begin" is Verilog's equivalent of C's "{" | |
cnt = cnt + 1; | |
if (cnt > 50000000) | |
// turn off the LED | |
else | |
// turn on the LED | |
end // "end" is Verilog's equivalent of C's "}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void loop() { | |
digitalWrite(LED_PIN, HIGH); // turn LED on | |
delay(1000); // wait for 1000 milliseconds | |
digitalWrite(LED_PIN, LOW); // turn LED off | |
delay(1000); // wait for 1000 milliseconds | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void loop() { | |
digitalWrite(LED_PIN, HIGH); // turn LED on | |
digitalWrite(LED_PIN, LOW); // turn LED off | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
always @(posedge clk) // whenever `clk` changes from a 0 to a 1, | |
cnt = cnt + 1; // increment the register `cnt` by one | |
assign led_pin = cnt; // independently, also connect the `led` wire to the `cnt` register |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment