EmbDev.net

Forum: FPGA, VHDL & Verilog LED intensity


von Oer P. (Company: None) (ponta)


Rate this post
useful
not useful
Hello, I got an assignment to make LED lights change intensity, by 
switching 4 switches on pegasus board. I got some code but I don't think 
it's good. Can you check it or improve it? Thanks


1
library ieee;
2
use ieee.std_logic_1164.all;
3
use ieee.numeric_std.all;
4
 
5
entity PWM is
6
  generic
7
  (
8
    sd        : unsigned(11 downto 0) := X"C35" -- 3125 in hex
9
  );
10
  port
11
  (
12
    clock     : in   std_logic;
13
    switches  : in   unsigned(3 downto 0);
14
    pwm       : out  std_logic
15
  );
16
end PWM;
17
 
18
architecture behave_PWM of PWM is
19
  signal counter : unsigned(15 downto 0);
20
 
21
begin -- architecture
22
  pwm_proc: process (clock)
23
  begin
24
    if (counter <= switches*sd) then
25
      pwm <= '1';
26
    else
27
      pwm <= '0';
28
    end if;
29
 
30
    if (counter >= 50000) then
31
      counter <= (others => '0');
32
    else
33
      counter <= counter + 1;
34
    end if;
35
  end process;
36
 
37
end behave_PWM;

von Lothar M. (Company: Titel) (lkmiller) (Moderator)


Rate this post
useful
not useful
Oer Pdw wrote:
> I got some code but I don't think it's good.
You are right. This will work in a simulator at best. But you will never 
ever get it on hardware.

The major drawback is the missing clock inside the process. It is not 
enough to have the clock in the sensitivity list. Look how all the 
others do it...

: Edited by Moderator
von Oer P. (Company: None) (ponta)


Rate this post
useful
not useful
The problem is that everyone has different task so I can't check
Can you help me?

von Lothar M. (Company: Titel) (lkmiller) (Moderator)


Rate this post
useful
not useful
Oer Pdw wrote:
> The problem is that everyone has different task
What should this sentence say?

What I said is: there is no clock in your design! But each counter needs 
a clock because its built of flipflops. Have a look how everybody on the 
world describes a counter. There must be a rising_edge() or a 'event 
in your code!

: Edited by Moderator
von Oer P. (Company: None) (ponta)


Rate this post
useful
not useful
Is it good now?


1
library ieee;
2
use ieee.std_logic_1164.all;
3
use ieee.numeric_std.all;
4
 
5
entity PWM is
6
  generic
7
  (
8
    sd        : unsigned(7 downto 0) := X"C3" -- 195 in hex
9
  );
10
  port
11
  (
12
    clock     : in   std_logic;
13
    switches  : in   unsigned(7 downto 0);
14
    pwm       : out  std_logic
15
  );
16
end PWM;
17
 
18
architecture behave_PWM of PWM is
19
  signal counter : unsigned(15 downto 0);
20
 
21
begin -- architecture
22
  pwm_proc: process (clock)
23
  begin
24
if(clock'event and clock='0'") then
25
    if (counter <= switches*sd) then
26
      pwm <= '1';
27
    else
28
      pwm <= '0';
29
    end if;
30
 
31
    if (counter >= 50000) then
32
      counter <= (others => '0');
33
    else
34
      counter <= counter + 1;
35
end if;
36
    end if;
37
  end process;
38
 
39
end behave_PWM;

von Lothar M. (Company: Titel) (lkmiller) (Moderator)


Rate this post
useful
not useful
Oer Pdw wrote:
> Is it good now?
There is a syntax error in it, but the concept is ok.

> Is it good now?
Take a little test bench and check it out...

Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
No account? Register here.