EmbDev.net

Forum: FPGA, VHDL & Verilog I need help with a VHDL average calculator (sum contains 10 products)


von Henk H. (Company: Fisherman) (theodorush)


Rate this post
useful
not useful
My issue is written in the code (architecture) after the arrow -->
1
------------------------------------------
2
------------------------------------------
3
-- Date        : Sun Jan 13 02:28:08 2013
4
--
5
-- Author      : Just me
6
--
7
-- Company     : 
8
--
9
-- Description : Average reaction time
10
--
11
------------------------------------------
12
------------------------------------------
13
library ieee;
14
use ieee.std_logic_1164.all;
15
use ieee.std_logic_arith.all;
16
use ieee.std_logic_unsigned.all;
17
18
entity  Average_reaction_time  is
19
port(
20
  clk: in std_logic;
21
  reset: in std_logic;
22
  stop: in std_logic;
23
  reaction_count: in std_logic_vector (15 downto 0);
24
  reaction_count_average: out std_logic_vector (15 downto 0)
25
  );
26
end;
27
28
------------------------------------------
29
------------------------------------------
30
-- Date        : Sun Jan 13 02:28:08 2013
31
--
32
-- Author      : Just me
33
--
34
-- Company     : 
35
--
36
-- Description : The average of a sum with 10 products
37
--               The result will be shown on four 7segment display's 
38
--               with a bcd encoder 
39
--
40
------------------------------------------
41
------------------------------------------
42
architecture  Behavior  of  Average_reaction_time  is
43
signal reaction_count0: std_logic_vector (3 downto 0); 
44
signal reaction_count1: std_logic_vector (3 downto 0);
45
signal reaction_count2: std_logic_vector (3 downto 0); 
46
signal reaction_count3: std_logic_vector (3 downto 0); 
47
signal reaction_count_average0: std_logic_vector (3 downto 0); -
48
signal reaction_count_average1: std_logic_vector (3 downto 0);
49
signal reaction_count_average2: std_logic_vector (3 downto 0);
50
signal reaction_count_average3: std_logic_vector (3 downto 0);
51
signal count: integer range 0 to 9; 
52
signal sum0: integer range 0 to 90;
53
signal sum1: integer range 0 to 90;
54
signal sum2: integer range 0 to 90;
55
signal sum3: integer range 0 to 90;
56
signal product0: integer range 0 to 9;
57
signal product1: integer range 0 to 9;
58
signal product2: integer range 0 to 9;
59
signal product3: integer range 0 to 9;
60
signal average0: integer range 0 to 9;
61
signal average1: integer range 0 to 9;
62
signal average2: integer range 0 to 9;
63
signal average3: integer range 0 to 9;
64
begin
65
66
--synchronous process
67
average: process(clk)
68
69
begin
70
--info: assign the current value of input reaction_count to 
71
--      reactioncount0,1,2,3
72
reaction_count0 <= reaction_count (3 downto 0);
73
reaction_count1 <= reaction_count (7 downto 4);
74
reaction_count2 <= reaction_count (11 downto 8);
75
reaction_count3 <= reaction_count (15 downto 12);
76
77
--info: convert reaction_count0,1,2,3 to integer wich makes deviding by 10     
78
--      possible
79
product0 <= conv_integer(reaction_count0);
80
product1 <= conv_integer(reaction_count1);
81
product2 <= conv_integer(reaction_count2);
82
product3 <= conv_integer(reaction_count3);
83
84
--info: if reset pressed set every signal to 0
85
if (reset = '1') then
86
  reaction_count0 <= "0000";
87
  reaction_count1 <= "0000";
88
  reaction_count2 <= "0000";
89
  reaction_count3 <= "0000";
90
  reaction_count_average0 <= "0000";
91
  reaction_count_average1 <= "0000";
92
  reaction_count_average2 <= "0000";
93
  reaction_count_average3 <= "0000";
94
  count <= 0;
95
  sum0 <= 0;
96
  sum1 <= 0;
97
  sum2 <= 0;
98
  sum3 <= 0;
99
  product0 <= 0;
100
  product1 <= 0;
101
  product2 <= 0;
102
  product3 <= 0;
103
  average0 <= 0;
104
  average1 <= 0;
105
  average2 <= 0;
106
  average3 <= 0;
107
  
108
  elsif (clk'event and clk = '1') then
109
110
--> This is where the issue is: when stop = 1 it keeps adding the product 
111
--  to the sum until stop = 0. I want it to only add one product to the 
112
--  specific sum when stop = 1. I have tried stop = 0 and stop'event but 
113
--  that is not acceptable after a the clk'event. there might be a simple 
114
--  solution but I cant seem to find it. I really appreciate your help.
115
116
    if (stop = '1') then  
117
    sum0 <= sum0 + product0;
118
    sum1 <= sum1 + product1;
119
    sum2 <= sum2 + product2;
120
    sum3 <= sum3 + product3;
121
    count <= count + 1;
122
    end if;
123
  
124
    if (count = 10) then
125
    average0 <= sum0/10;
126
    average1 <= sum1/10;
127
    average2 <= sum2/10;
128
    average3 <= sum3/10;  
129
    count <= 0;  
130
    end if;
131
  reaction_count_average0 <= conv_std_logic_vector(average0, 4);
132
  reaction_count_average1 <= conv_std_logic_vector(average1, 4);
133
  reaction_count_average1 <= conv_std_logic_vector(average2, 4);
134
  reaction_count_average1 <= conv_std_logic_vector(average3, 4);
135
end if;
136
end process;
137
reaction_count_average (3 downto 0) <= reaction_count_average0; 
138
reaction_count_average (7 downto 4) <= reaction_count_average1;
139
reaction_count_average (11 downto 8) <= reaction_count_average2;
140
reaction_count_average (15 downto 12) <= reaction_count_average3;
141
end Behavior;

von Achim S. (Guest)


Rate this post
useful
not useful
is "stop" an external signal, which may switch without defined timing 
with respect to "clk"? If yes, you will run in trouble, cause sooner or 
later stop will switch just at that unfavourable point in time, where 
different parts of your logic recognize different levels of stop and 
compute nonsense.

>>I want it to only add one product to the
>>--  specific sum when stop = 1.

So what you really want is to add in that clk-cycle, where stop has a 
rising edge. You need an edge detector:

1
....
2
signal stop_old:std_logic:='1';
3
4
if (clk'event and clk = '1') then
5
6
    stop_old <= stop; --remember stop-signal of previous clock-cycle
7
8
    if (stop = '1') and (stop_old='0') then 
9
       sum0 <= sum0 + product0 
10
.....

von Henk H. (Company: Fisherman) (theodorush)


Rate this post
useful
not useful
The stop button is activated by the user, so it might switch without 
defined timing with respect to the "clk". There might indeed be a 
problem, but i think i can solve that by adding  another input.

But, thanks this solution solved my question and helped me for now.

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.