Ask Your Question
0

How to incorporate PWM in a Shift register using VHDL?

asked 2023-06-03 14:21:56 +0000

lalupa gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2023-06-03 14:51:02 +0000

david gravatar image

Here is an example VHDL code for incorporating PWM in a Shift register:

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;

entity shift_reg_pwm is
    Port ( clk: in STD_LOGIC;
           reset: in STD_LOGIC;
           data_in: in STD_LOGIC;
           duty_cycle: in UNSIGNED(7 downto 0);
           data_out: out STD_LOGIC_VECTOR(7 downto 0));
end shift_reg_pwm;

architecture Behavioral of shift_reg_pwm is

    signal shift_reg: STD_LOGIC_VECTOR(7 downto 0) := (others => '0');
    signal counter: UNSIGNED(7 downto 0) := (others => '0');
    signal pwm: STD_LOGIC := '0';

    begin

    process(clk, reset)
    begin
        if reset = '1' then
            shift_reg <= (others => '0');
            counter <= (others => '0');
            pwm <= '0';
        elsif rising_edge(clk) then
            if counter = duty_cycle then
                pwm <= not pwm;
                counter <= (others => '0');
            else
                counter <= counter + 1;
            end if;
            shift_reg <= data_in & shift_reg(7 downto 1);
        end if;
    end process;

    data_out <= shift_reg;

    data_out(7) <= pwm;

end Behavioral;

Explanation:

  • The entity shift_reg_pwm has four input ports: clk, reset, data_in, and duty_cycle, and one output port data_out.
  • clk and reset are the clock and reset signals, respectively.
  • data_in is the input data to the shift register.
  • duty_cycle is an 8-bit unsigned value that specifies the duty cycle of the PWM signal, where 255 corresponds to 100% duty cycle and 0 corresponds to 0% duty cycle.
  • data_out is the shifted data out of the shift register, with the most significant bit (MSB) representing the PWM signal.
  • The architecture Behavioral describes the functionality of the shift register with PWM.
  • The signal shift_reg is the shift register initialized to all zeroes.
  • The signal counter is an unsigned counter initialized to all zeroes, which counts from 0 to duty_cycle to generate the PWM signal.
  • The signal pwm is the PWM signal initially set to logic 0.
  • The process process(clk, reset) is triggered on the rising edge of clk or when reset is high.
  • When reset is high, the shift register, counter, and PWM signal are reset to their initial values.
  • When clk is high, the following operations take place:
    • The shift register shift_reg is shifted left by one bit, with the input data data_in occupying the least significant bit (LSB).
    • The counter counter is incremented by 1.
    • If the counter counter equals the duty cycle duty_cycle, the PWM signal pwm is toggled, and the counter is reset to all zeros.
  • The output port data_out is assigned the value of the shift register shift_reg, and the MSB of data_out is set to the PWM signal pwm.
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-03 14:21:56 +0000

Seen: 14 times

Last updated: Jun 03 '23