我正在尝试为 SDI-12 协议编写代码
SDI-12的字节帧格式是
- 1 个起始位
- 7 个数据位,首先传输最低有效位
- 1个奇偶校验位,偶校验
- 1 个停止位
我想传输 24 位数据,即100001101011001010000100
当排列在框架中时看起来像
开始,1000011,P,停止,开始,0101100,P,停止,开始,1010000,P,停止,开始,100_ _ _ _,P,停止
其中 P 是奇偶校验位
问题是:
- 我应该在最后四位传输什么数据,即_ _ _ _
- 我怎么知道要发送的数据已经完成?
代码:
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
entity serialtx is
generic (data_width : integer);
port (clk, reset : in std_logic;
tx_data : in std_logic_vector(data_width-1 downto 0);
tx_out : out std_logic
);
end serialtx;
architecture behavioral of serialtx is
type ofstate is (IDLE, START_bit, STOP_bit);
signal state, nextstate : ofstate;
signal parity_bit, tx_enable : std_logic;
begin
process
variable count, p_val : integer := 0;
begin
if(clk'event and clk = '1' and tx_enable = '1')then
if(reset = '1')then
tx_out <= '0';
else
case state is
when IDLE =>
tx_out <= '0';
nextstate <= START_bit;
when START_bit =>
count := count+1;
if(count >= 0 and count < 7)then
for b in p_val to data_width-1 loop
tx_out <= tx_data(p_val);
end loop;
elsif(count = 7)then
tx_out <= parity_bit;
p_val := p_val+1;
elsif(count = 8) then
tx_out <= '1';
nextstate <= STOP_bit;
count := 0;
end if;
when STOP_bit =>
--if--data to be sent is completed then
tx_out <= '1';
tx_enable <= '0';
--else
nextstate <= IDLE;
--end if;
end case;
end if;
end if;
end process;
end behavioral;