#define counter_T_sec 10.0e-6 //time between counter ticks, in seconds
#define counter_thresh_sec 1005.4e-6 //want to detect when this much time elapses
#define counter_thresh_uint(counter_thresh_sec/counter_T_sec) //the threshold in counter ticks
#define ADC_VREF 3.0 //reference voltage of ADC in volts
#define ISENSE_GAIN 1.215 //gain of a current sense amplifier, in volts per amp
#define ADC_MAX 4095.0 //max value of ADC
#define ISENSE_THRESH_amp (-5.41) //threshold in amps
#define ISENSE_THRESH_lsb (ISENSE_THRESH_amp*ISENSE_GAIN/ADC_VREF*ADC_MAX) //threshold in ADC lsbs
uint16 counter;
uint16 counter_thresh=counter_thresh_uint;
int16 ADC_result;
int16 ADC_thresh=ISENSE_THRESH_lsb;
.....
if(counter==counter_thresh)
{
}
....
if(ADC_result<ADC_thresh)
{
}
package my_pkg is
constant ADC_VREF_mv : integer := 3000; --Vref is 3.000V
constant ADC_MAX : integer := 4095; --12 bit ADC
constant ISENSE_GAIN_m : integer := 1215; --Gain is 1.215 volts per amp
constant ISENSE_THRESH_ma : integer := 1410; --threshold is 1.41 amps
end my_pkg;
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
use work.my_pkg.all;
entity example_entity is
port(
ADC_IIN : in unsigned(11 downto 0); --data from ADC
clk10m : in std_logic; -- 10MHz clock
reset : in std_logic
);
end entity;
architecture rtl of example_entity is
signal Ithresh_flag : std_logic;
constant ISENSE_THRESH_lsb : unsigned(11 downto 0) := to_unsigned(ISENSE_THRESH_ma*ISENSE_GAIN_m/ADC_VREF_mv*ADC_MAX/1000,12);
-- when the current exceeds 1.41, set Ithresh_flag to 1
begin
Ithresh_proc:process(clk10m,reset)
begin
if reset = '1' then
Ithresh_flag <= '0';
elsif rising_edge(clk10m) then
if(ADC_IIN > ISENSE_THRESH_lsb) then
Ithresh_flag <= '1';
end if;
end if;
end process;
end rtl;
constant ISENSE_THRESH_lsb : unsigned(11 downto 0) := to_unsigned(ISENSE_THRESH_ma*ISENSE_GAIN_m/ADC_VREF_mv*ADC_MAX/1000,12);
constant ISENSE_THRESH_lsb : unsigned(11 downto 0) := to_unsigned(ISENSE_THRESH_ma*ISENSE_GAIN_m*ADC_MAX/ADC_VREF_mv/1000,12);
You mean do the calculations manually and then just write in the final result? That's what I'm currently doing, but some of these have many parameters, so it's an error-prone process.it's easier to perform real calculations and convert the final result unsigned.
You mean using hardware resources to calculate them at runtime, or with a method similar to my C example with? The former case isn't feasible for me since I don't have many extra multipliers around. For the latter case, I can't find a method of doing it.No, I mean to do the real calculation in VHDL.
Honestly I'm not sure how else to state it. I can do something very effectively in the C preprocessor, and want to do something similar in VHDL, but can't find a method that doesn't have pitfalls with loss of precision and/or variable overflow. Maybe the solution I'm looking for is simple and trivial, but searching around the internet hasn't revealed it yet.Still not really sure what the question is?
Code VHDL - [expand] 1 2 3 4 5 6 7 8 package my_pkg is constant ADC_VREF_mv : real := 3.000; --Vref is 3.000V constant ADC_MAX : real := 4095.0; --12 bit ADC constant ISENSE_GAIN_m : real := 1.215; --Gain is 1.215 volts per amp constant ISENSE_THRESH_ma : real := 1.410; --threshold is 1.41 amps end my_pkg; constant ISENSE_THRESH_lsb : unsigned(11 downto 0) := to_unsigned( integer(ISENSE_THRESH_ma*(ISENSE_GAIN_m/ADC_VREF_mv)*(ADC_MAX/1000.0) ) ,12);
I'm talking about compile time calculations.You mean using hardware resources to calculate them at runtime, or with a method similar to my C example with? The former case isn't feasible for me since I don't have many extra multipliers around. For the latter case, I can't find a method of doing it.
TO_UNSIGNED(INTEGER(0.5/LPFC/FCLK/ISCALE*U2SCALE*2.0**(15+N_TS-1)),15)
constant ISENSE_THRESH_lsb : unsigned(11 downto 0) := to_unsigned( integer(ISENSE_THRESH_ma*(ISENSE_GAIN_m/ADC_VREF_mv)*(ADC_MAX/1000.0) ) ,12);
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?