mtwieg
Advanced Member level 6
When coding in C, I often use preprocessors to calculate thresholds and scaling factors based on various parameters. For example, see below:
I put most of my #define parameters as floats, to preserve precision, and the compiler automatically casts it to the right type when assigning it to my variables.
I'm trying to find a similar way to do this in VHDL, but so far haven't found anything close. Generics work fine when I have a finite set of implementations I want to choose from. But in my case some of these parameters could be anything (like the gain of an amplifier).
I see this sort of question brought up often, but usually the requirement is for determining port or variable widths. In my case I just want to control the values of constants (I'm ok with fixing them at a large width to ensure against overflow).
Any tips? I'm working in quartus.
C:
#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)
{
}
I'm trying to find a similar way to do this in VHDL, but so far haven't found anything close. Generics work fine when I have a finite set of implementations I want to choose from. But in my case some of these parameters could be anything (like the gain of an amplifier).
I see this sort of question brought up often, but usually the requirement is for determining port or variable widths. In my case I just want to control the values of constants (I'm ok with fixing them at a large width to ensure against overflow).
Any tips? I'm working in quartus.