Broadway
Broadway

Reputation: 3

Verilog: Using PWM to control the brightness of an LED

I am trying to make sense of this example code found here

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

To start off, it creates the 4 bit register PWM_input and 5 bit register PWM. It then goes on to set PWM equal to 3 bits of itself plus PWN_input at every clock cycle.

What exactly is happening here? Why are only 4 of the 5 bits of PWM being used and what is the value of PWM_input?

When actually controlling the LED, it is setting the LED to PWM[4]. Is that the same as PWM[4:0]? Or is it a standalone value?

Upvotes: 0

Views: 4164

Answers (1)

dave_59
dave_59

Reputation: 42788

PWM[4] is essentially the carry bit of a 4 bit adder/accumulator, and that bit is being used to turn on/off the LED. The value of PWM_input determines how rapidly the LED turns on and off. It might have been clearer if the example were written as

module LED_PWM( input clk,
                input [3:0] PWM_input, // 16 intensity levels
                output reg LED);

  reg [3:0] PWM; // internal accumulator

  always @(posedge clk) {LED,PWM} <= PWM + PWM_input;

endmodule

Upvotes: 1

Related Questions