bit_vector和std_logic_vetcor

本文探讨了VHDL中bit_vector与std_logic_vector的数据类型差异,指出std_logic_vector具有更丰富的取值,包括'0','1','Z','U','X','W','L','H'和'-',而bit_vector仅包含'0'和'1'。因此,在使用std_logic_vector时,'when others=>'语句变得尤为重要,以确保所有可能的状态都被正确处理。

bit_vector只有’0‘和’1‘两种取值
但是std_logic_vetcor有九种取值
所以在写程序时 when others=>对于定义为std_logic_vector是很有必要的

library ieee; use ieee.std_logic_1164.all; use ieee.numeric_std.all; library std; use std.textio.all; package AdcDriverPkg is constant C_NB_BITS_ADC : integer := 12; constant C_ADC_ROM_DEPTH : integer := 20; constant C_ADC_ROM_WIDTH : integer := 8; constant C_NB_RAM_BUF : integer := 2; -- number of buffers in the OCM constant C_ADC_RANGE_SELECT_BIPOLAR_5V : std_logic_vector(2 downto 0) := "010"; -- Range is set to +/- 5V constant C_ADC_RANGE_SELECT_BIPOLAR_2V5 : std_logic_vector(2 downto 0) := "011"; -- Range is set to +/- 2V5 constant C_ADC_RANGE_SELECT_UNIPOLAR_5V : std_logic_vector(2 downto 0) := "110"; -- Range is set to 0V to 5V constant C_ADC_CHAN0 : std_logic_vector (2 downto 0) := std_logic_vector (to_unsigned (0, 3)); constant C_ADC_CHAN1 : std_logic_vector (2 downto 0) := std_logic_vector (to_unsigned (1, 3)); constant C_ADC_CHAN2 : std_logic_vector (2 downto 0) := std_logic_vector (to_unsigned (2, 3)); constant C_ADC_CHAN3 : std_logic_vector (2 downto 0) := std_logic_vector (to_unsigned (3, 3)); constant C_ADC_CHAN4 : std_logic_vector (2 downto 0) := std_logic_vector (to_unsigned (4, 3)); constant C_ADC_CHAN5 : std_logic_vector (2 downto 0) := std_logic_vector (to_unsigned (5, 3)); constant C_ADC_CHAN6 : std_logic_vector (2 downto 0) := std_logic_vector (to_unsigned (6, 3)); constant C_ADC_CHAN7 : std_logic_vector (2 downto 0) := std_logic_vector (to_unsigned (7, 3)); constant C_ADC_REGISTER_CONTINUE : std_logic_vector(6 downto 0) := "0000000"; -- 0X00 constant C_ADC_REGISTER_MANUAL : std_logic_vector(6 downto 0) := "0000100"; -- 0X04 constant C_ADC_REGISTER_AUTO : std_logic_vector(6 downto 0) := "0000101"; -- 0X05 constant C_ADC_REGISTER_AUX_CFG : std_logic_vector(6 downto 0) := "0000110"; -- 0X06 type t_scheduling_rom is array (0 to C_ADC_ROM_DEPTH - 1) of std_logic_vector (C_ADC_ROM_WIDTH - 1 downto 0); constant C_DEFAULT_SCHEDULING : t_scheduling_rom := ( 0 => '0' & C_ADC_RANGE_SELECT_BIPOLAR_5V & '0' & C_ADC_CHAN0, 1 => '0' & C_ADC_RANGE_SELECT_BIPOLAR_5V & '0' & C_ADC_CHAN1, 2 => '0' & C_ADC_RANGE_SELECT_BIPOLAR_5V & '0' & C_ADC_CHAN2, 3 => '0' & C_ADC_RANGE_SELECT_BIPOLAR_5V & '0' & C_ADC_CHAN3, 4 => '0' & C_ADC_RANGE_SELECT_UNIPOLAR_5V & '0' & C_ADC_CHAN4, 5 => '0' & C_ADC_RANGE_SELECT_UNIPOLAR_5V & '0' & C_ADC_CHAN5, 6 => '0' & C_ADC_RANGE_SELECT_UNIPOLAR_5V & '0' & C_ADC_CHAN6, 7 => '1' & C_ADC_RANGE_SELECT_UNIPOLAR_5V & '1' & C_ADC_CHAN7, -- end of microframe, end of macroframe others => (others => '0') ); function ss12DaqToSigned(n: in std_logic_vector(11 downto 0)) return std_logic_vector; function ss12InvertValue(n: in std_logic_vector(11 downto 0)) return std_logic_vector; end package AdcDriverPkg; package body AdcDriverPkg is ----------------------------------------------------- -- ss18DaqToFp18d3 function -- input is a std_logic_vector of 12 bits (UNSIGNED value from 0 to 4095 -> (-5V to 5V) ADC measurement) -- return value is a std_logic_vector of 12 bits (SIGNED value from -2048 to 2047 -> -5V to 5V) -- The function is basically substracting half of the maximum value (2048) to covert from unsigned to signed format ----------------------------------------------------- function ss12DaqToSigned(n: in std_logic_vector(11 downto 0)) return std_logic_vector is begin return (not n(11) & n(10 downto 0)); end; ----------------------------------------------------- -- ss18InvertValue function -- Input is a std_logic_vector of 12 bits (SIGNED value from -2048 to 2047) -- Inverting the value of a 2-complement number by inverting and adding 1 (instead of multiplying with -1). ----------------------------------------------------- function ss12InvertValue(n: in std_logic_vector(11 downto 0)) return std_logic_vector is begin return std_logic_vector(to_signed(to_integer(signed(not(n))) + 1, 12)); end; end;
08-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值