摘要:本文針對由FPGA構成的高速數據采集系統數據處理能力弱的問題,提出FPGA與單片機實現數據
串行通信的解決方案。在通信過程中*遵守RS232協議,具有較強的通用性和推廣價值。
1、前言
現場可編程邏輯器件(FPGA)在高速采集系統中的應用越來越廣,由于FPGA對采集到的數據的處理能力比較差,故需要將其采集到的數據送到其他CPU系統來實現數據的處理功能,這就使FPGA系統與其他CPU系統之間的數據通信提到日程上,得到人們的急切關注。本文介紹利用VHDL語言實現FPGA與單片機的串口異步通信電路。
整個設計采用模塊化的設計思想,可分為四個模塊:FPGA數據發送模塊,FPGA波特率發生控制模塊,FPGA總體接口模塊以及單片機數據接收模塊。本文著重對FPGA數據發送模塊實現進行說明。
2、FPGA數據發送模塊的設計
根據RS232異步串行通信來的幀格式,在FPGA發送模塊中采用的每一幀格式為:1位開始位+8位數據位+1位奇校驗位+1位停止位,波特率為2400。本系統設計的是將一個16位的數據封裝成高位幀和低位幀兩個幀進行發送,先發送低位幀,再發送高位幀,在傳輸數據時,加上文件頭和數據長度,文件頭用555555來表示,只有單片機收到555555時,才將下面傳輸的數據長度和數據位進行接收,并進行奇校驗位的檢驗,正確就對收到的數據進行存儲處理功能,數據長度可以根據需要任意改變。由設置的波特率可以算出分頻系數,具體算法為分頻系數X=CLK/(BOUND*2)。可由此式算出所需的任意波特率。下面是實現上述功能的VHDL源程序。
Libraryieee;
useieee.std_logic_1164.all;
useieee.std_logic_arith.all;
useieee.std_logic_unsigned.all;
entitya2_binis
port(txclk:instd_logic;--2400Hz的波特率時鐘
reset:instd_logic;--復位信號
din:instd_logic_vector(15downto0);--發送的數據
start:instd_logic;--允許傳輸信號
sout:outstd_logic--串行輸出端口
);
enda2_bin;
architecturebehavofa2_binis
signalthr,len:std_logic_vector(15downto0);
signaltxcnt_r:std_logic_vector(2downto0);
signalsout1:std_logic;
signalcou:integer:=0;
signaloddb:std_logic;
typesis(start1,start2,shift1,shift2,odd1,odd2,s*,stop2);
signalstate:s:=start1;
begin
process(txclk)
begin
ifrising_edge(txclk)then
ifcou<3thenthr<="0000000001010101";--發送的文件頭
elsifcou=3then
thr<="0000000000000010";--發送的文件長度
elsif(cou>3andstate=stop2)thenthr<=din;--發送的數據
endif;
endif;
endprocess;
process(reset,txclk)
variabletsr,tsr1,oddb1,oddb2:std_logic_vector(7downto0);
begin
ifreset='1'then
txcnt_r<=(others=>'0');
sout1<='1';
state<=start1;
cou<=0;
elsiftxclk'eventandtxclk='1'then
casestateis
whenstart1=>
ifstart='1'then
ifcou=3then
len<=thr;
endif;
tsr:=thr(7downto0);
oddb1:=thr(7downto0);
sout1<='0';--起始位
txcnt_r<=(others=>'0');
state<=shift1;
else
state<=start1;
endif;
whenshift1=>
oddb<=oddb1(7)xoroddb1(6)xoroddb1(5)xoroddb1(4)xoroddb1(3)xoroddb1(2)xoroddb1(1)xoroddb1(0);
sout1<=tsr(0);--數據位
tsr(6downto0):=tsr(7downto1);
tsr(7):='0';
txcnt_r<=txcnt_r+1;
if(txcnt_r=7)then
state<=odd1;cou<=cou+1;
endif;
whenodd1=>--奇校驗位
ifoddb='1'then
sout1<='0';state<=s*;
else
sout1<='1';state<=s*;
endif;
whens*=>
sout1<='1';--停止位
ifcou<4then
state<=start1;
else
state<=start2;
endif;
whenstart2=>
tsr1:=thr(15downto8);
oddb2:=thr(15downto8);
sout1<='0';--起始位
txcnt_r<=(others=>'0');
state<=shift2;
whenshift2=>
oddb<=oddb2(7)xoroddb2(6)xoroddb2(5)xoroddb2(4)xoroddb2(3)xoroddb2(2)xoroddb2(1)xoroddb2(0);
sout1<=tsr1(0);--數據位
tsr1(6downto0):=tsr1(7downto1);
tsr1(7):='0';
txcnt_r<=txcnt_r+1;
if(txcnt_r=7)then
state<=odd2;
endif;
whenodd2=>--奇校驗位
ifoddb='1'then
sout1<='0';state<=stop2;
else
sout1<='1';state<=stop2;
endif;
whenstop2=>
sout1<='1';--停止位
iflen="0000000000000000"then
state<=stop2;
else
state<=start1;
len<=len-1;
endif;
endcase;
endif;
endprocess;
sout<=sout1;
endbehav;
其中各信號的說明已在程序中標明了。波形仿真圖如圖1所示。
圖1FPGA數據發送時序仿真圖
圖中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的設置即可實現。
3、總結
目前電子產品的開發中經常要綜合運用EDA技術、計算機控制技術、數字信號處理技術,那么電路各部分經常需要數據交換。本文也是基于此給出這方面應用的實例,供交流。