51单片机与fpga进行串行通信.docx
- 文档编号:4031612
- 上传时间:2022-11-27
- 格式:DOCX
- 页数:15
- 大小:17.31KB
51单片机与fpga进行串行通信.docx
《51单片机与fpga进行串行通信.docx》由会员分享,可在线阅读,更多相关《51单片机与fpga进行串行通信.docx(15页珍藏版)》请在冰豆网上搜索。
51单片机与fpga进行串行通信
51单片机与fpga进行串行通信,并显示到LCD上。
下面给出串口通信USart部分程序:
1.FPGA程序:
Module1(协议部分):
Libraryieee;
useieee.std_logic_1164.all;
useieee.std_logic_arith.all;
useieee.std_logic_unsigned.all;
--Uncommentthefollowinglibrarydeclarationifusing
--arithmeticfunctionswithSignedorUnsignedvalues
--useIEEE.NUMERIC_STD.ALL;
--Uncommentthefollowinglibrarydeclarationifinstantiating
--anyXilinxprimitivesinthiscode.
--libraryUNISIM;
--useUNISIM.VComponents.all;
entitytxis
port(
din:
instd_logic_vector(7downto0);
sout:
outstd_logic;
clk:
instd_logic;
len_r:
outstd_Logic_vector(3downto0);
txclk:
instd_logic
);
endtx;
architectureBehavioraloftxis
typesis(start,shift,stop,delay1,delay2);
--typesis(start1,shift1,stop1,delay1,start2,shift2,stop2,delay2,start3,shift3,stop3,delay3,start4,shift4,stop4,delay4);
signalstate:
s:
=start;
signaltxcnt_r,len:
std_logic_vector(3downto0);
signalsout1:
std_logic;
signalthr:
std_logic_vector(7downto0);
signalclk1:
std_logic;
begin
process(txclk)
begin
iftxclk='1'andtxclk'eventthen
thr<=din;
endif;
endprocess;
process(txclk)
variabletsr:
std_logic_vector(7downto0);
variablecount:
integerrange0to480:
=0;
variablecount1:
integerrange0to4800:
=0;
begin
iftxclk='1'andtxclk'eventthen
casestateis
whenstart=>
tsr:
=thr;
sout1<='0';--传输起始位
txcnt_r<=(others=>'0');--传输数据位数计数清0
state<=shift;
whenshift=>
sout1<=tsr(0);--数据位传输第1位数据
tsr(6downto0):
=tsr(7downto1);--填充数据
tsr(7):
='0';
txcnt_r<=txcnt_r+1;
if(txcnt_r=7)then
state<=stop;
endif;
whenstop=>
sout1<='1';
state<=delay1;
whendelay1=>
ifcount<120then
count:
=count+1;
else
count:
=0;
iflen<7then
state<=start;
len<=len+1;
else
state<=delay2;
endif;
endif;
whendelay2=>
ifcount1<2400then
count1:
=count1+1;
else
count1:
=0;
len<="0000";
state<=start;
endif;
endcase;
sout<=sout1;
len_r<=len;
endif;
endprocess;
process(clk)
variablenc:
integerrange0to100000000:
=0;
begin
ifclk='1'andclk'eventthen
if(nc>50000000ornc<0)then
nc:
=0;
endif;
nc:
=nc+1;
if(nc>25000000)then
clk1<='0';
else
clk1<='1';
endif;
endif;
endprocess;
--process(clk1)
--begin
--ifclk1='1'andclk1'eventthen
--len<="000";
--endif;
--endprocess;
endBehavioral;
Module2(数据发送部分):
Libraryieee;
useieee.std_logic_1164.all;
useieee.std_logic_arith.all;
useieee.std_logic_unsigned.all;
--Uncommentthefollowinglibrarydeclarationifusing
--arithmeticfunctionswithSignedorUnsignedvalues
--useIEEE.NUMERIC_STD.ALL;
--Uncommentthefollowinglibrarydeclarationifinstantiating
--anyXilinxprimitivesinthiscode.
--libraryUNISIM;
--useUNISIM.VComponents.all;
entitydatais
port(
clk:
instd_logic;
len_r:
instd_logic_vector(3downto0);
dout:
outstd_logic_vector(7downto0)
);
enddata;
architectureBehavioralofdatais
signaldata:
std_logic_vector(39downto0):
="0000000000000000000000000000000000000000";
signalclk1:
std_logic;
signalclk2:
std_logic;
begin
--process(clk)
--begin
--ifclk='1'andclk'eventthen
--dout<="00100011";
--endif;
--endprocess;
process(clk)
variablenum:
integerrange0to4;
begin
iflen_r=7then
--dout<=data(39downto32);
dout<="00000010";
elsiflen_r=6then
--dout<=data(15downto8);
dout<=data(31downto24);
elsiflen_r=5then
--dout<=data(23downto16);
dout<=data(23downto16);
elsiflen_r=4then
--dout<=data(31downto24);
dout<=data(15downto8);
elsiflen_r=3then
--dout<=data(39downto32);
dout<=data(7downto0);
elsiflen_r=2then
--dout<=data(39downto32);
--dout<=data(7downto0);
dout<="01111110";
elsiflen_r=1then
--dout<=data(39downto32);
dout<="01111110";
elsiflen_r=0then
--dout<=data(39downto32);
dout<="01111110";
endif;
endprocess;
process(clk1)
begin
ifclk1='1'andclk1'eventthen
data<=data+1;
endif;
endprocess;
process(clk)
variablenc:
integerrange0to100000000:
=0;
begin
ifclk='1'andclk'eventthen
if(nc>100000000ornc<0)then
nc:
=0;
endif;
nc:
=nc+1;
if(nc>50000000)then
clk1<='0';
else
clk1<='1';
endif;
endif;
endprocess;
endBehavioral;
2.51单片机:
#include
#include"gui.h"
//--声明全局函数--//
voidUsartConfiguration();
/*******************************************************************************
*函数名:
main
*函数功能:
主函数
*输入:
无
*输出:
无
*******************************************************************************/
voidmain()
{
unsignedlongreceiveData,temp,div;
unsignedintstate,j=0;
unsignedchardata1[5];
unsignedcharcheck[3];
//unsignedchardatadisplay[8];
unsignedchardatadisplay1[10];
//unsignedchardatadisplay2[9];
TFT_Init();
TFT_ClearScreen(BLACK);//刷黑色
UsartConfiguration();
while
(1)
{
if(state==0)//检验头文件
{
if(RI==1)//查看是否接收到数据
{
check[2]=check[1];
check[1]=check[0];
check[0]=SBUF;
RI=0;//清除标志位
}
if(check[2]==0x7e&&check[1]==0x7e&&check[0]==0x7e)
{
state=1;
check[2]=check[1]=check[0]=0;
}
}
if(state==1)//接收数据
{
if(RI==1)//查看是否接收到数据
{
data1[j]=SBUF;//读取数据
RI=0;//清除标志位
j=j+1;
}
if(j>4)
{
if(data1[4]==0x00)//计数法测频率
{
state=0;
j=0;
//显示频率
receiveData=data1[3]*16777216+data1[2]*65536+data1[1]*256+data1[0];
if(receiveData>=1000000)
{
datadisplay1[0]='0'+receiveData%1000000000/100000000;
datadisplay1[1]='0'+receiveData%100000000/10000000;//千万
datadisplay1[2]='0'+receiveData%10000000/1000000;//百万
datadisplay1[3]='.';
datadisplay1[4]='0'+receiveData%1000000/100000;//十万
datadisplay1[5]='0'+receiveData%100000/10000;//万
datadisplay1[6]='0'+receiveData%10000/1000;//千
datadisplay1[7]='0'+receiveData%1000/100;//百
datadisplay1[8]='0'+receiveData%100/10;//十
datadisplay1[9]='0'+receiveData%10;//个
GUI_WriteASCII(0,65,datadisplay1,RED,BLACK);
GUI_WriteASCII(200,65,"MHz",RED,BLACK);
GUI_WriteASCII(0,24,"frequency:
",RED,BLACK);
}
elseif(receiveData<1000000&&receiveData>1000)
{
datadisplay1[0]='0'+receiveData%1000000000/100000000;
datadisplay1[1]='0'+receiveData%100000000/10000000;//千万
datadisplay1[2]='0'+receiveData%10000000/1000000;//百万
datadisplay1[3]='0'+receiveData%1000000/100000;//十万
datadisplay1[4]='0'+receiveData%100000/10000;//万
datadisplay1[5]='0'+receiveData%10000/1000;//千
datadisplay1[6]='.';
datadisplay1[7]='0'+receiveData%1000/100;//百
datadisplay1[8]='0'+receiveData%100/10;//十
datadisplay1[9]='0'+receiveData%10;//个
GUI_WriteASCII(0,65,datadisplay1,RED,BLACK);
GUI_WriteASCII(200,65,"KHz",RED,BLACK);
GUI_WriteASCII(0,24,"frequency:
",RED,BLACK);
}
//显示周期
receiveData=data1[3]*16777216+data1[2]*65536+data1[1]*256+data1[0];
receiveData=1000000000/receiveData;
datadisplay1[0]='0'+receiveData%1000000000/100000000;
datadisplay1[1]='0'+receiveData%100000000/10000000;
datadisplay1[2]='0'+receiveData%10000000/1000000;
datadisplay1[3]='0'+receiveData%1000000/100000;
datadisplay1[4]='0'+receiveData%100000/10000;
datadisplay1[5]='0'+receiveData%10000/1000;
datadisplay1[6]='.';
datadisplay1[7]='0'+receiveData%1000/100;
datadisplay1[8]='0'+receiveData%100/10;
datadisplay1[9]='0'+receiveData%10;//个
GUI_WriteASCII(0,141,datadisplay1,RED,BLACK);
GUI_WriteASCII(200,141,"us",RED,BLACK);
GUI_WriteASCII(0,100,"period:
",RED,BLACK);
}
if(data1[4]==0x01)//周期法测频率
{
state=0;
j=0;
//显示频率
receiveData=data1[3]*16777216+data1[2]*65536+data1[1]*256+data1[0];
if(receiveData>100000)
{
div=receiveData;
receiveData=100000000/receiveData;
datadisplay1[0]='0'+receiveData%10000/1000;//千
datadisplay1[1]='0'+receiveData%1000/100;//百
datadisplay1[2]='0'+receiveData%100/10;//十
datadisplay1[3]='0'+receiveData%10;//个
datadisplay1[4]='.';
receiveData=(100000000%div)*10/div;
temp=(100000000%div)*10%div;
datadisplay1[5]='0'+receiveData%10;//千
receiveData=temp*10/div;
temp=temp*10%div;
datadisplay1[6]='0'+receiveData%10;//百
receiveData=temp*10/div;
temp=temp*10%div;
datadisplay1[7]='0'+receiveData%10;//十
receiveData=temp*10/div;
temp=temp*10%div;
datadisplay1[8]='0'+receiveData%10;//个
receiveData=temp*10/div;
temp=temp*10%div;
datadisplay1[9]='0'+receiveData%10;//个
GUI_WriteASCII(0,65,datadisplay1,RED,BLACK);
GUI_WriteASCII(200,65,"Hz",RED,BLACK);
GUI_WriteASCII(200,65,"",BLACK,BLACK);
GUI_WriteASCII(0,24,"frequency:
",RED,BLACK);
}
elseif(receiveData<=100000)
{
div=receiveData;
receiveData=100000000/receiveData;
datadisplay1[0]='0'+receiveData%10000/1000;//千
datadisplay1[1]='.';
datadisplay1[2]='0'+receiveData%1000/100;//百
datadisplay1[3]='0'+receiveData%100/10;//十
datadisplay1[4]='0'+receiveData%10;//个
receiveData=(100000000%div)*10/div;
temp=(100000000%div)*10%div;
datadisplay1[5]='0'+receiveData%10;//千
receiveData=temp*10/div;
temp=temp*10%div;
datadisplay1[6]='0'+receiveData%10;//百
receiveData=temp*10/div;
temp=temp*10%div;
datadisplay1[7]='0'+receiveData%10;//十
receiveData=temp*10/div;
temp=temp*10%div;
datadisplay1[8]='0'+receiveData%10;//个
receiveData=temp*10/div;
temp=temp*10%div;
datadisplay1[9]='0'+receiveData%10;//个
GUI_WriteASCII(0,65,datadisplay1,RED,BLACK);
GUI_WriteASCII(200,65,"KHz",RED,BLACK);
GUI_WriteASCII(0,24,"frequency:
",RED,BLACK);
}
//显示周期
receiveData=
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 51 单片机 fpga 进行 串行 通信