0

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?

Broadway
  • 3
  • 1
  • 2

1 Answers1

1

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
dave_59
  • 39,096
  • 3
  • 24
  • 63
  • Okay, so if I wanted to display a specific intensity level(1-16), how would I go about doing so? I've been trying to set PWM_input to something like [3:2] but I have not been able to achieve a specific level of brightness. More specifically, would changing PWM + PWM_input to PWM + PWM_input[3:3] achieve a brightness level of 16? What about 15, would it be [3:2]? – Broadway Sep 14 '16 at 16:17
  • The brightness level would be 1-15 or 4'b001 to 4'b1111. For this particular design, there is no predictable way to set the LED to be a constant on or off. I don't know what you mean by the value [3:2]. That is a bit range. Do you mean 4'b1100 or 12, that is setting bits [3] and [2] to a 1. – dave_59 Sep 14 '16 at 17:25