Here's how to make an LED blink (on and off).
module LEDblink(clk, LED); input clk; // clock typically from 10MHz to 50MHz output LED; // create a binary counter reg [31:0] cnt; always @(posedge clk) cnt <= cnt+1; assign LED = cnt; // blink the LED at a few Hz (using the 23th bit of the counter, use a different bit to modify the blinking rate) endmodule
Another solution is to drive the LED FPGA output half of the time. If that's done fast enough, your eye will see the LED half-lit. Anything faster than 100Hz is too fast for the human eye to detect - it averages the light intensity it receives.
module LEDhalflit(clk, LED); input clk; // clk should be at least 200Hz. // Anything above is fine (most FPGA boards have adequate clocks, running at a few 10's of MHz) output LED; reg toggle; always @(posedge clk) toggle <= ~toggle; // toggles at half the clk frequency (at least 100Hz) assign LED = toggle; endmodule
For more LED intensity control, a PWM is an ideal solution.
Here's an example that uses a 4-bits control to select between 16 intensity levels out of an LED.
module LED_PWM(clk, PWM_input, LED); input clk; input [3:0] PWM_input; // 16 intensity levels output LED; reg [4:0] PWM; always @(posedge clk) PWM <= PWM[3:0]+PWM_input; assign LED = PWM; endmodule
By continuously changing the LED intensity, the LED appears to "glow".
module LEDglow(clk, LED); input clk; output LED; reg [23:0] cnt; always @(posedge clk) cnt<=cnt+1; wire [3:0] PWM_input = cnt ? cnt[22:19] : ~cnt[22:19]; // ramp the PWM input up and down reg [4:0] PWM; always @(posedge clk) PWM <= PWM[3:0]+PWM_input; assign LED = PWM; endmodule
This is the same example that was presented in part 0.