初始状态 接受串行数据 判 断数据 置位 控制操作结束 FIFO 发送数据 FPGA数据发送模块的设计
根据RS232 异步串行通信来的帧格式,在FPGA发送模块中采用的每一帧格式为:1位开始位+8位数据位+1位奇校验位+1位停止位,波特率为2400。本系统设计的是将一个8位的数据封装成高位帧和低位帧两个帧进行发送,先发送低位帧,再发送高位帧,在传输数据时,加上文件头和数据长度,文件头用555555来表示,只有单片机收到555555时,才将下面传输的数据长度和数据位进行接收,并进行奇校验位的检验,正确就对收到的数据进行存储处理功能,数据长度可以根据需要任意改变。由设置的波特率可以算出分频系数,具体算法为分频系数X=CLK/(BOUND*2)。可由此式算出所需的任意波特率。下面是实现上述功能的VHDL源程序。 Library ieee;
use ieee.std_logic_1164.all; use ieee.std_logic_arith.all; use ieee.std_logic_unsigned.all;
entity atel2_bin is
port( txclk: in std_logic; --2400Hz的波特率时钟 reset: in std_logic; --复位信号
din: in std_logic_vector(15 downto 0); --发送的数据 start: in std_logic; --允许传输信号 sout: out std_logic --串行输出端口 );
end atel2_bin;
architecture behav of atel2_bin is
signal thr,len: std_logic_vector(15 downto 0); signal txcnt_r: std_logic_vector(2 downto 0); signal sout1: std_logic; signal cou: integer:=0; signal oddb:std_logic;
type s is(start1,start2,shift1,shift2,odd1,odd2,stop1,stop2); signal state:s:=start1; begin
process(txclk)
begin
if rising_edge(txclk) then
if cou<3 then thr<=0000000001010101; --发送的文件头 elsif cou=3 then
thr<=0000000000000010; --发送的文件长度 elsif (cou>3 and state=stop2) then thr<=din;--发送的数据 end if; end if; end process;
process(reset,txclk)
variable tsr,tsr1,oddb1,oddb2: std_logic_vector(7 downto 0); begin
if reset=1 then txcnt_r<=(others=>0); sout1<=1;
state<=start1; cou<=0;
elsif txclkevent and txclk=1 then case state is when start1=>
if start=1 then if cou=3 then len<=thr; end if;
tsr:=thr(7 downto 0); oddb1:=thr(7 downto 0); sout1<=0; --起始位 txcnt_r<=(others=>0); state<=shift1; else
state<=start1; end if;
when shift1=>
oddb<=oddb1(7) xor oddb1(6) xor oddb1(5) xor oddb1(4) xor oddb1(3) xor oddb1(2) xor oddb1(1) xor oddb1(0);
sout1<=tsr(0); --数据位 tsr(6 downto 0):=tsr(7 downto 1); tsr(7):=0;
txcnt_r<=txcnt_r+1; if (txcnt_r=7) then
state<=odd1;cou<=cou+1; end if;
when odd1=> --奇校验位
if oddb=1 then
sout1<=0;state<=stop1; else
sout1<=1;state<=stop1; end if;
when stop1=>
sout1<=1; --停止位 if cou<4 then state<=start1; else
state<=start2; end if;
when start2=>
tsr1:=thr(15 downto 8); oddb2:=thr(15 downto 8); sout1<=0; --起始位 txcnt_r<=(others=>0); state<=shift2; when shift2=>
oddb<=oddb2(7) xor oddb2(6) xor oddb2(5) xor oddb2(4) xor oddb2(3) xor oddb2(2) xor oddb2(1) xor oddb2(0);
sout1<=tsr1(0);--数据位
tsr1(6 downto 0):=tsr1(7 downto 1); tsr1(7):=0;
txcnt_r<=txcnt_r+1; if (txcnt_r=7) then
state<=odd2; end if;
when odd2=> --奇校验位 if oddb=1 then
sout1<=0;state<=stop2; else
sout1<=1;state<=stop2; end if;
when stop2=>
sout1<=1; --停止位 if len=0000000000000000 then state<=stop2; else
state<=start1; len<=len-1; end if; end case;
end if;
end process;
sout<=sout1; end behav;
其中各信号的说明已在程序中标明了。波形仿真图如图1所示。
图1 FPGA数据发送时序仿真图
图中Din写入值为3355H,波特率为2400Hz,Start信号始终置逻辑1,即随时都能发送数据。Reset信号逻辑1时复位,逻辑0时电路开始工作。THR是数据寄存器,文件头、数据长度以及数据位都先寄存到THR中,Len是数据长度,TSR是低8位数据帧寄存器,TSR1是高8位数据帧寄存器。数据长度Len定为02H,发送时先发送低8位55H,后发送高8位33H,一共发送两遍。发送的数据格式说明:当发送55H时,其二进制为01010101,则发送的数据的二进制数为00101010111(1位开始位+8位数据位+1位奇校验位+1位停止位)。 单片机部分先对FPGA发送过来的文件头进行确认,正确就接收文件,否则放弃接收的数据。根据FPGA发送模块的协议,对串口控制寄存器SCON和波特率控制寄存器PCON的设置即可实现。
5.4 顶层模块设计
顶层模块起连接作用,将各个分模块很好的连接起来即可,顶层模块程序如下: library IEEE;
use IEEE.STD_LOGIC_1164.ALL; use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
-- Uncomment the following lines to use the declarations that are -- provided for instantiating Xilinx primitive components. --library UNISIM;
--use UNISIM.VComponents.all; entity adc_top is
Port ( clk0,eoc : in std_logic; -- 外部时钟信号
ale,adda,oe,start: out std_logic; -- adc0809的控制信号 dout : out std_logic_vector(7 downto 0);-- 8位二进制输出 gaoout,ddout : out std_logic_vector(6 downto 0));-- 两个数码管输出 end adc_top;
architecture Behavioral of adc_top is component div -- 分频元件定义语句 Port ( clk : in std_logic;
clk1 : out std_logic; shuout: out std_logic_vector(7 downto 0)); end component;
component adc_main -- 采样控制元件定义语句 port (d :in std_logic_vector(7 downto 0);
clk,eoc : in std_logic; lock1, ale, start, oe, adda : out std_logic;
q : out std_logic_vector(7 downto 0)); end component;
component seg -- 输出显示元件定义语句
Port (hh: in std_logic_vector(3 downto 0); ll: in std_logic_vector(3 downto 0); lockk : in std_logic;
y : out std_logic_vector(7 downto 0);
q1: out std_logic_vector(6 downto 0); q2: out std_logic_vector(6 downto 0)); end component;
signal r,f:std_logic; -- 定义中间信号
signal s,i:std_logic_vector(7 downto 0); -- 定义中间8位二进制信号 signal g,h:std_logic_vector(3 downto 0); -- 定义中间4位二进制信号 begin
u0:div port map(clk=>clk0,clk1=>r,shuout=>i);
u1:adc_main port map(clk=>r,d=>i,eoc=>eoc,lock1=>f,ale=>ale,
start=>start,oe=>oe,adda=>adda,q(7 downto 4)=>g,q(3 downto 0)=>h); u2:seg port map(hh=>g,ll=>h,lockk=>f,y=>dout,q1=>gaoout,q2=>ddout); end Behavioral;
第6章
系统的功能仿真及分析
6.1 采样控制模块仿真及分析
对采样控制模块建立的波形测试台窗口及输入值的设定如上图4.8所示,其仿真结果如下图4.9所示:
图4.9 采样控制模块的仿真结果
在图4.9的仿真波形图中可以看到:当时钟上升沿触发时,将next_current中的状态送入current_state中。从黄色虚线以后开始,当current_state为st0时,ale为低电平0--此时地址锁存信号无效,外界没有模拟信号输入;start为低电平0--此时还没有启动模数转换;oe为低电平0--此时三态缓冲寄存器没打开、无数据输出;lock为低电平0--此时无数据锁存功能;此状态称为初始状态,后面的其它状态只需判断与初始状态不同的部分即变化的信号。
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库FPGA直接控制ADC0809对模拟信号进行采样(4)在线全文阅读。
相关推荐: