2012年10月16日 星期二

Varying an LED intensity


源自http://www.fpga4fun.com/Opto2.html

Varying an LED intensity

Turning an LED on and off
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 [32:0] cnt;
always @(posedge clk) cnt<=cnt+1;

assign LED = cnt[22];    // blink the LED at a few Hz (change the bit index to change the blinking rate)
endmodule
Making an LED half-lit
One solution would be doubling the value of the resistor used in series with the LED.
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
Fine-tuning the LED intensity
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[4];
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[23] ? 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[4];
endmodule
This is the same example that was presented in part 0.


>>> NEXT: 7-segments LED displays >>>

沒有留言:

張貼留言

ESP32 微控制器 採集的環境數據,經由 MQTT 協定 轉換並寫入工業標準的 Modbus 暫存器 中

  一個典型的 IoT(物聯網)與工業自動化系統整合(SCADA/機台連線) 的架構圖。它透過 Node-RED 作為核心資料網關,將前端 ESP32 微控制器 採集的環境數據,經由 MQTT 協定 轉換並寫入工業標準的 Modbus 暫存器 中。 1. 各模組功能拆解...