EmbDev.net

Forum: FPGA, VHDL & Verilog accurate counter


von Mohammad M. (mohammadmother)


Rate this post
useful
not useful
hi every body
i write a vhdl code that is nearly accurate counter
every 1.34 s the LED will be ++
(2^27*10ns = 1.34 s )
please tell me what is it's problem because in ISIM i don't got the 
answer
1
----------------------------------------------------------------------------------
2
-- Company: 
3
-- Engineer: 
4
-- 
5
-- Create Date:    08:48:18 09/28/2014 
6
-- Design Name: 
7
-- Module Name:    count - Behavioral 
8
-- Project Name: 
9
-- Target Devices: 
10
-- Tool versions: 
11
-- Description: 
12
--
13
-- Dependencies: 
14
--
15
-- Revision: 
16
-- Revision 0.01 - File Created
17
-- Additional Comments: 
18
--
19
----------------------------------------------------------------------------------
20
library IEEE;
21
use IEEE.STD_LOGIC_1164.ALL;
22
23
-- Uncomment the following library declaration if using
24
-- arithmetic functions with Signed or Unsigned values
25
use IEEE.STD_logic_unsigned.ALL;
26
--use IEEE.NUMERIC_STD.ALL;
27
-- Uncomment the following library declaration if instantiating
28
-- any Xilinx primitives in this code.
29
--library UNISIM;
30
--use UNISIM.VComponents.all;
31
32
entity count is
33
    Port ( clk : in  STD_LOGIC;
34
           reset : in  STD_LOGIC;
35
           sw1 : in  STD_LOGIC;
36
           LED : out std_logic_vector(7 downto 0)); 
37
end count;
38
architecture Behavioral of count is
39
begin
40
process (clk,reset,sw1)
41
variable c :std_logic_vector(7 downto 0):="00000000";
42
variable counter :std_logic_vector(27 downto 0):="0000000000000000000000000000";
43
begin
44
   if reset='1' then
45
        LED <= "00000000";
46
     elsif sw1='1' then
47
         LED <= "11111111";
48
     elsif(clk'event and clk='1') then
49
       counter := counter +1 ;
50
          if (counter ="1000000000000000000000000000") then
51
                c:=c+1;      
52
                LED<=c;
53
             counter:="0000000000000000000000000000";
54
        end if;
55
    end if;
56
end process;    
57
58
end Behavioral;

: Edited by Moderator
von asd (Guest)


Rate this post
useful
not useful
just some tipps:

- no need to use variables here, use signals instead for the counting 
logic. (and read about the difference of signals and variables)

- it's better style to use IEEE.NUMERIC_STD.ALL to calculate in vhdl and 
don't calculate with logic_vectors directly, but instead with integer or 
natural

- you can use hex-format for more compact style: x"00" for "00000000"

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


Rate this post
useful
not useful
First: please use the vhdl tags areund your VHDL code:
1
[vhdl]VHDL code[/vhdl]

Then this:
1
variable counter :std_logic_vector(27 downto 0):="0000000000000000000000000000";
Why do you use a variable?
Why don't you use an integer?

And where did you find this:
1
   if reset='1' then
2
      LED <= "00000000";
3
   elsif sw1='1' then
4
      LED <= "11111111";
5
   elsif(clk'event and clk='1') then
An asynchronous reset AND additionally a conbinatorial reset. Thats 
plain rubbish!

This here:
1
counter:="0000000000000000000000000000";
Could be shortened to that:
1
counter:=(others=>'0');


Mohammad Mothermohammad wrote:
> i write a vhdl code that is nearly accurate
My VHDL code usually is exactly accurate. Whats the problem with 
yours?

> please tell me what is it's problem because in ISIM i don't got the answer
What error messages do you get? What do you expect and what happens 
instead?

I would do it like this:
1
library IEEE; 
2
use IEEE.STD_LOGIC_1164.ALL; 
3
use IEEE.NUMERIC_STD.ALL; 
4
 
5
entity BlinkLED is 
6
    Port ( clk : in  STD_LOGIC; 
7
           led : out  STD_LOGIC); 
8
end BlinkLED; 
9
 
10
architecture Behavioral of BlinkLED is 
11
                                                    -- let the synthesizer do the hard work!
12
signal c : integer range 0 to 1340000000/10-1 := 0; -- 1.34s at 100MHz = 1340000000ns/10ns
13
signal x : std_logic:= '0';
14
15
begin 
16
   process begin  
17
      wait until rising_edge(clk); -- wait till next clock
18
      if (c<1340000000/10-1) then  -- check the counter
19
          c <= c+1;                -- if less: count up
20
      else                         -- if reached:
21
          c <= 0;                  -- reset counter
22
          x <= not x;              -- and toggle the local signal x
23
      end if; 
24
   end process; 
25
   led <= x;                       -- hand x over to the LED
26
end Behavioral;

: Edited by Moderator
von Mohammad M. (mohammadmother)


Rate this post
useful
not useful
thank you for every one i modified the code and implement this into FPGA 
and it's work.

correct code is:
1
----------------------------------------------------------------------------------
2
-- Company: 
3
-- Engineer: 
4
-- 
5
-- Create Date:    08:48:18 09/28/2014 
6
-- Design Name: 
7
-- Module Name:    count - Behavioral 
8
-- Project Name: 
9
-- Target Devices: 
10
-- Tool versions: 
11
-- Description: 
12
--
13
-- Dependencies: 
14
--
15
-- Revision: 
16
-- Revision 0.01 - File Created
17
-- Additional Comments: 
18
--
19
----------------------------------------------------------------------------------
20
library IEEE;
21
use IEEE.STD_LOGIC_1164.ALL;
22
23
-- Uncomment the following library declaration if using
24
-- arithmetic functions with Signed or Unsigned values
25
use IEEE.STD_logic_unsigned.ALL;
26
--use IEEE.NUMERIC_STD.ALL;
27
-- Uncomment the following library declaration if instantiating
28
-- any Xilinx primitives in this code.
29
--library UNISIM;
30
--use UNISIM.VComponents.all;
31
32
entity count is
33
    Port ( clk : in  STD_LOGIC;
34
           reset : in  STD_LOGIC;
35
           sw1 : in  STD_LOGIC;
36
           LED : out std_logic_vector(7 downto 0)); 
37
end count;
38
architecture Behavioral of count is
39
begin
40
process (clk,reset,sw1)
41
variable c :std_logic_vector(7 downto 0):="00000000";
42
variable counter :std_logic_vector(26 downto 0):="000000000000000000000000000";
43
begin
44
if(clk'event and clk='1') then
45
      if reset='1' then
46
         LED <= "00000000";
47
          counter := "000000000000000000000000000";
48
       c:="00000000";   
49
     elsif sw1='1' then
50
         LED <= "11111111";
51
          c := "11111111";
52
       LED <=c;  
53
      -- for i in 0 to 10000000 loop
54
           --      if (i=10000000) then
55
         --      counter:="000000000000000000000000000";
56
             --    c:="00000000";
57
              --  LED <= c;
58
             --end if;
59
              --  end loop  ;
60
         else
61
        counter := counter +1 ;
62
           if (counter ="101111101011110000100000000") then
63
                c:=c+1;      
64
                LED<=c;
65
              counter:="000000000000000000000000000";
66
            end if;
67
     end if;
68
end if;
69
end process;    
70
71
end Behavioral;

: Edited by User
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.