VHDL - 10% duty cycle

Guest #4567867
Rate this post
• ▲ useful
▼ not useful
Hello, I'm new to VHDL. I need to generate 500Hz from 50MHz clock 
frequency. I already got it. My problem here is how should the code to 
be adjusted if I want to change the duty cycle to 10%, 30%, and 50%?

Thank you.
Attached files:
Guest #4568190
Rate this post
• ▲ useful
▼ not useful
You should reformat your code. You cannot read which "else" belongs to 
which "if".
I don't kniwnif its even possible to add 1 to a std_logic_vector.
What you need, are 2 values. One for counting up and one for down. 
Typically you set a flag if you are counting up or not.
Moderator (Company: Titel) #4569041
Rate this post
• ▲ useful
▼ not useful
Chris wrote:
> What you need, are 2 values. One for counting up and one for down.
Indeed 2 values are needed.
But only 1 for a counter, and the second for a compare value to toggle 
the output. Then its stupidly easy:
1
library IEEE;
2
use IEEE.STD_LOGIC_1164.ALL;
3
use IEEE.numeric_std.ALL;
4

5
entity PWM500Hz is
6
    port( clock   : in STD_LOGIC; -- 50 Mhz
7
          pwmout  : out STD_LOGIC);
8
end PWM500Hz ;
9

10
architecture Behavioral of PWM500Hz is
11
  signal cnt: integer range 0 to (50000000/500)-1 := 0;
12
  constant cmp: integer := (50000000/5000); -- PWM = 10%
13
begin
14

15
  process(clock) begin
16
    if rising_edge(clock) then
17
      if cnt<(50000000/500)-1 then cnt<=cnt+1;
18
      else cnt<=0;
19
      end if;
20
    end if;
21
  end process;
22
  
23
  pwmout <= '1' when cnt<cmp else '0';
24
end Behavioral;

Reply

Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.

Formatting
  • [c]C-Code[/c]
  • [vhdl]VHDL-Code[/vhdl]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]
  • [[Title]] – link to an article
  • Link to another post: right-click its post ID (#8078628), copy the address, paste it into the text

More about formatting

Post long source code as an attachment, not in the text.

Already registered? Log in

Not shown to anyone. We send you a link; your post appears once you confirm it there.

By submitting you create a free account and accept the terms of use.