Lab 4: PWM Generation
Controlling LED brightness using PWM.

Controlling LED brightness using PWM.

Code

module pwm_brightness(
    input clk,           // Clock input
    input [7:0] duty,    // Duty cycle input
    output reg pwm_out,  // PWM output
    output reg LEDR1,
    output reg LEDR2,
    output reg LEDR3,
    output reg LEDR4,
    output reg LEDR5,
    output reg LEDR6,
    output reg LEDR7,
    output reg LEDR8,
    output reg LEDR9
);
    // Initialize all LEDs except LEDR0 to 'off'
    initial begin
        LEDR1 = 0;
        LEDR2 = 0;
        LEDR3 = 0;
        LEDR4 = 0;
        LEDR5 = 0;
        LEDR6 = 0;
        LEDR7 = 0;
        LEDR8 = 0;
        LEDR9 = 0;
    end

    reg [7:0] counter = 0;

    // PWM logic
    always @(posedge clk) begin
        counter <= counter + 1;
        pwm_out <= (counter < duty) ? 1'b1 : 1'b0;
    end
endmodule


Pin Planner

Node NameDirectionLocation
LEDR1OutputPIN_A9
LEDR2OutputPIN_A10
LEDR3OutputPIN_B10
LEDR4OutputPIN_D13
LEDR5OutputPIN_C13
LEDR6OutputPIN_E14
LEDR7OutputPIN_D14
LEDR8OutputPIN_A11
LEDR9OutputPIN_B11
clkInputPIN_P11
duty[7]InputPIN_A14
duty[6]InputPIN_A13
duty[5]InputPIN_B12
duty[4]InputPIN_A12
duty[3]InputPIN_C12
duty[2]InputPIN_D12
duty[1]InputPIN_C11
duty[0]InputPIN_C10
pwm_outOutputPIN_A8

Result