⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serial interface (rs-232) 4.rs-232 receiver module.mht

📁 用FPGA实现RS232通信,此代码是用VHDL语言编写
💻 MHT
字号:
From: <由 Microsoft Internet Explorer 5 保存>
Subject: FPGAs are fun!
Date: Wed, 19 Apr 2006 00:29:31 +0800
MIME-Version: 1.0
Content-Type: multipart/related;
	boundary="----=_NextPart_000_000E_01C66348.53969350";
	type="text/html"
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000

This is a multi-part message in MIME format.

------=_NextPart_000_000E_01C66348.53969350
Content-Type: text/html;
	charset="gb2312"
Content-Transfer-Encoding: quoted-printable
Content-Location: http://www.fpga4fun.com/SerialInterface4.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>FPGAs are fun!</TITLE>
<META http-equiv=3DContent-Type content=3D"text/html; charset=3Dgb2312">
<STYLE>TH {
	FONT-SIZE: 10px; FONT-FAMILY: Verdana,Tahoma,Arial
}
TD {
	FONT-SIZE: 10px; FONT-FAMILY: Verdana,Tahoma,Arial
}
PRE {
	FONT-SIZE: 10px; FONT-FAMILY: Verdana,Tahoma,Arial
}
H1 {
	FONT-SIZE: 240%
}
H2 {
	FONT-SIZE: 210%
}
H3 {
	FONT-SIZE: 190%
}
H4 {
	FONT-SIZE: 160%
}
H5 {
	FONT-SIZE: 130%
}
H6 {
	FONT-SIZE: 110%
}
A {
	FONT-WEIGHT: bold; COLOR: black; TEXT-DECORATION: none
}
A:hover {
	TEXT-DECORATION: underline
}
.link {
	COLOR: #000000
}
.link:hover {
	COLOR: blue; TEXT-DECORATION: underline
}
PRE {
	PADDING-RIGHT: 5px; PADDING-LEFT: 5px; BACKGROUND: #d0d0f0; =
PADDING-BOTTOM: 5px; MARGIN: 5px; PADDING-TOP: 5px
}
</STYLE>

<META content=3D"MSHTML 6.00.2712.300" name=3DGENERATOR></HEAD>
<BODY leftMargin=3D0 topMargin=3D0 rightMargin=3D0 marginheight=3D"0" =
marginwidth=3D"0">
<TABLE cellSpacing=3D5 cellPadding=3D2 width=3D"100%" border=3D0 nowrap>
  <TBODY>
  <TR>
    <TD vAlign=3Dtop bgColor=3D#90a0c0><FONT =
size=3D7>fpga4fun.com</FONT> - where=20
      FPGAs are fun<BR><BR></TD></TR>
  <TR>
    <TD bgColor=3Dblack height=3D1></TD></TR></TBODY></TABLE>
<TABLE height=3D"100%" cellSpacing=3D5 cellPadding=3D5>
  <TBODY>
  <TR>
    <TD vAlign=3Dtop width=3D140 bgColor=3D#dddddd><FONT =
color=3D#9090c0><B>Home<BR><A=20
      class=3Dlink href=3D"http://www.fpga4fun.com/index.html">FPGAs are =

      fun!</A><BR><A class=3Dlink=20
      href=3D"http://www.fpga4fun.com/SiteNews.html">Site news</A><BR><A =

      class=3Dlink href=3D"http://www.fpga4fun.com/WhyThisSite.html">Why =
this=20
      site?</A><BR><A class=3Dlink =
href=3D"http://www.fpga4fun.com/WhoAmI.html">Who=20
      am I?</A><BR><A class=3Dlink=20
      href=3D"http://www.fpga4fun.com/forum">Forum</A><BR><A =
class=3Dlink=20
      href=3D"http://www.fpga4fun.com/links.html">Links</A>
      <HR>
       <BR>FPGA projects<BR><A class=3Dlink=20
      href=3D"http://www.fpga4fun.com/MusicBox.html">Music box</A><BR><A =

      class=3Dlink href=3D"http://www.fpga4fun.com/RCServos.html">R/C=20
      servos</A><BR><A class=3Dlink=20
      href=3D"http://www.fpga4fun.com/SerialInterface.html">Serial=20
      interface</A><BR><A class=3Dlink=20
      href=3D"http://www.fpga4fun.com/TextLCDmodule.html">Text LCD=20
      module</A><BR><A class=3Dlink=20
      href=3D"http://www.fpga4fun.com/PWM_DAC.html">PWM and one-bit =
DAC</A><BR><A=20
      class=3Dlink=20
      href=3D"http://www.fpga4fun.com/QuadratureDecoder.html">Quadrature =

      decoder</A><BR><A class=3Dlink=20
      href=3D"http://www.fpga4fun.com/PongGame.html">Pong game</A><BR><A =

      class=3Dlink =
href=3D"http://www.fpga4fun.com/GraphicLCDpanel.html">Graphic LCD=20
      panel</A><BR><A class=3Dlink=20
      href=3D"http://www.fpga4fun.com/I2C.html">I2C</A><BR><BR>Advanced=20
      projects<BR><A class=3Dlink=20
      href=3D"http://www.fpga4fun.com/digitalscope.html">Digital=20
      oscilloscope</A><BR><A class=3Dlink=20
      href=3D"http://www.fpga4fun.com/10BASE-T.html">10BASE-T =
interface</A><BR><A=20
      class=3Dlink href=3D"http://www.fpga4fun.com/PCI.html">PCI =
interface</A>
      <HR>
       <BR>FPGAs<BR><A class=3Dlink=20
      href=3D"http://www.fpga4fun.com/WhatAreFPGAs.html">What are =
FPGAs</A><BR><A=20
      class=3Dlink =
href=3D"http://www.fpga4fun.com/HowFPGAsWork.html">How FPGAs=20
      work</A><BR><A class=3Dlink=20
      href=3D"http://www.fpga4fun.com/clocks.html">Clocks and global=20
      lines</A><BR><A class=3Dlink=20
      href=3D"http://www.fpga4fun.com/DownloadCables.html">Download=20
      cables</A><BR><A class=3Dlink=20
      =
href=3D"http://www.fpga4fun.com/configuration.html">Configuration</A><BR>=
<A=20
      class=3Dlink href=3D"http://www.fpga4fun.com/LearnMore.html">Learn =

      more</A><BR><BR>FPGA electronic<BR><A class=3Dlink=20
      href=3D"http://www.fpga4fun.com/SMD.html">SMD technology</A><BR><A =

      class=3Dlink =
href=3D"http://www.fpga4fun.com/oscillators.html">Crystals and=20
      oscillators</A><BR><BR>FPGA software<BR><A class=3Dlink=20
      href=3D"http://www.fpga4fun.com/DesignSoftware.html">Design=20
      software</A><BR><A class=3Dlink=20
      href=3D"http://www.fpga4fun.com/PinAssignment.html">Pin =
assignment</A><BR><A=20
      class=3Dlink=20
      =
href=3D"http://www.fpga4fun.com/DesignEntry.html">Design-entry/HDL</A><BR=
><A=20
      class=3Dlink=20
      =
href=3D"http://www.fpga4fun.com/simulation.html">Simulation/HDL</A><BR><A=
=20
      class=3Dlink =
href=3D"http://www.fpga4fun.com/synthesis&amp;pnr.html">Synthesis=20
      and P&amp;R</A><BR><BR>Quick-start guides<BR><A class=3Dlink=20
      href=3D"http://www.fpga4fun.com/ISEQuickStart.html">ISE</A><BR><A =
class=3Dlink=20
      =
href=3D"http://www.fpga4fun.com/QuartusQuickStart.html">Quartus</A><BR><B=
R>HDL=20
      Tips &amp; Tricks<BR><A class=3Dlink=20
      href=3D"http://www.fpga4fun.com/VerilogTips.html">Verilog =
tips</A><BR><A=20
      class=3Dlink href=3D"http://www.fpga4fun.com/VHDLTips.html">VHDL =
tips</A>
      <HR>
       <BR>FPGA Boards<BR><A class=3Dlink=20
      href=3D"http://www.fpga4fun.com/board.html">Overview</A><BR><A =
class=3Dlink=20
      href=3D"http://www.fpga4fun.com/board_pluto.html">Pluto =
board</A><BR><A=20
      class=3Dlink =
href=3D"http://www.fpga4fun.com/board_pluto-II.html">Pluto-II=20
      board</A><BR><A class=3Dlink=20
      href=3D"http://www.fpga4fun.com/board_dragon.html">Dragon =
board</A><BR><A=20
      class=3Dlink href=3D"http://www.fpga4fun.com/board_TXDI.html">TXDI =
serial=20
      interface</A><BR><A class=3Dlink=20
      href=3D"http://www.fpga4fun.com/board_flashy.html">Flashy =
acquisition=20
      board</A><BR><BR>Shop<BR><A class=3Dlink=20
      href=3D"http://www.fpga4fun.com/ShopConditions.html">Shop at=20
      fpga4fun.com</A><BR><A class=3Dlink=20
      href=3D"http://www.fpga4fun.com/ShopBoards.html">FPGA =
boards</A><BR><A=20
      class=3Dlink=20
      =
href=3D"http://www.fpga4fun.com/ShopSLBB.html">Breadboards</A><BR><A=20
      class=3Dlink =
href=3D"http://www.fpga4fun.com/ShopFlashy.html">Flashy=20
      boards</A><BR><A class=3Dlink=20
      href=3D"http://www.fpga4fun.com/ShopKits.html">FPGA kits</A><BR><A =

      class=3Dlink=20
      =
href=3D"http://www.fpga4fun.com/ShopAccessories.html">Accessories</A><BR>=
<A=20
      class=3Dlink href=3D"http://www.fpga4fun.com/ShopI2C.html">I2C =
bus</A><BR><A=20
      class=3Dlink href=3D"http://www.fpga4fun.com/ShopLCDs.html">LCD=20
      modules</A><BR><A class=3Dlink=20
      =
href=3D"http://www.fpga4fun.com/ShopOpto-electronic.html">Opto-electronic=
</A><BR><A=20
      class=3Dlink =
href=3D"http://www.fpga4fun.com/ShopFPGAcables.html">FPGA=20
      cables</A><BR><A class=3Dlink=20
      =
href=3D"http://www.fpga4fun.com/ShopOscillators.html">Oscillators</A><BR>=

      <HR>
      </B></FONT><BR><BR></TD>
    <TD vAlign=3Dtop>
      <H4>RS-232 receiver module</H4>Here's what we are trying to=20
      build:<BR><BR><IMG=20
      =
src=3D"http://www.fpga4fun.com/images/SerialRxDmodule.gif"><BR><BR>Our=20
      implementation works like that:=20
      <UL>
        <LI>The module assembles data from the RxD line as it comes=20
        <LI>As a byte is being received, it appears on the "data" bus. =
Once a=20
        complete byte has been received, "data_ready" is asserted for =
one clock=20
        </LI></UL>Note that "data" is valid only when "data_ready" is =
asserted.=20
      The rest of the time, don't use it as new data may come that =
shuffles it.=20
      <H4>Oversampling</H4>An asynchronous receiver has to somehow get =
in-sync=20
      with the incoming signal (it doesn't have access to the clock used =
during=20
      transmission).=20
      <UL>
        <LI>To determine when a new data is coming ("start" bit), we =
oversample=20
        the signal at a multiple of the baud rate frequency.=20
        <LI>Once the "start" bit is detected, we sample the line at the =
known=20
        baud rate to acquire the data bits. </LI></UL>Receivers =
typically=20
      oversample the incoming signal at 16 times the baud rate. Here we =
use 8=20
      times. At 115200 bauds, that gives a sampling rate of=20
      921600Hz.<BR><BR>Let's assume that we have a "Baud8Tick" signal =
available,=20
      asserted 921600 times a second.=20
      <H4>The design</H4>First, the incoming "RxD" signal has no =
relationship=20
      with our clock.<BR>We use two D-flipflops to oversample it, and=20
      synchronize it to our clock.=20
      <TABLE cellPadding=3D10>
        <TBODY>
        <TR>
          <TD bgColor=3D#d0d0f0><B>reg</B> [1:0] =
RxD_sync;<BR><B>always</B>=20
            @(<B>posedge</B> clk) <B>if</B>(Baud8Tick) RxD_sync &lt;=3D=20
            {RxD_sync[0], RxD}; </TD></TR></TBODY></TABLE><BR>We filter =
the data, so=20
      that short spikes on the RxD line aren't mistaken with start bits. =

      <TABLE cellPadding=3D10>
        <TBODY>
        <TR>
          <TD bgColor=3D#d0d0f0><B>reg</B> [1:0] RxD_cnt;<BR><B>reg</B>=20
            RxD_bit;<BR><BR><B>always</B> @(<B>posedge</B>=20
            clk)<BR><B>if</B>(Baud8Tick)<BR><B>begin</B><BR>&nbsp;=20
            <B>if</B>(RxD_sync[1] &amp;&amp; RxD_cnt!=3D2'b11) RxD_cnt =
&lt;=3D=20
            RxD_cnt + 1;<BR>&nbsp; <B>else</B> <BR>&nbsp; =
<B>if</B>(~RxD_sync[1]=20
            &amp;&amp; RxD_cnt!=3D2'b00) RxD_cnt &lt;=3D RxD_cnt - =
1;<BR><BR>&nbsp;=20
            <B>if</B>(RxD_cnt=3D=3D2'b00) RxD_bit &lt;=3D 0;<BR>&nbsp;=20
            <B>else</B><BR>&nbsp; <B>if</B>(RxD_cnt=3D=3D2'b11) RxD_bit =
&lt;=3D=20
            1;<BR><B>end</B> </TD></TR></TBODY></TABLE><BR>A state =
machine allows us=20
      to go through each bit received, once a "start" is detected.=20
      <TABLE cellPadding=3D10>
        <TBODY>
        <TR>
          <TD bgColor=3D#d0d0f0><B>reg</B> [3:0] =
state;<BR><BR><B>always</B>=20
            @(<B>posedge</B>=20
            clk)<BR><B>if</B>(Baud8Tick)<BR><B>case</B>(state)<BR>&nbsp; =

            4'b0000: <B>if</B>(~RxD_bit) state &lt;=3D 4'b1000; // start =
bit=20
            found?<BR>&nbsp; 4'b1000: <B>if</B>(next_bit) state &lt;=3D =
4'b1001;=20
            // bit 0<BR>&nbsp; 4'b1001: <B>if</B>(next_bit) state =
&lt;=3D 4'b1010;=20
            // bit 1<BR>&nbsp; 4'b1010: <B>if</B>(next_bit) state =
&lt;=3D 4'b1011;=20
            // bit 2<BR>&nbsp; 4'b1011: <B>if</B>(next_bit) state =
&lt;=3D 4'b1100;=20
            // bit 3<BR>&nbsp; 4'b1100: <B>if</B>(next_bit) state =
&lt;=3D 4'b1101;=20
            // bit 4<BR>&nbsp; 4'b1101: <B>if</B>(next_bit) state =
&lt;=3D 4'b1110;=20
            // bit 5<BR>&nbsp; 4'b1110: <B>if</B>(next_bit) state =
&lt;=3D 4'b1111;=20
            // bit 6<BR>&nbsp; 4'b1111: <B>if</B>(next_bit) state =
&lt;=3D 4'b0001;=20
            // bit 7<BR>&nbsp; 4'b0001: <B>if</B>(next_bit) state =
&lt;=3D 4'b0000;=20
            // stop bit<BR>&nbsp; default: state &lt;=3D=20
            4'b0000;<BR><B>endcase</B> =
</TD></TR></TBODY></TABLE><BR>Notice that we=20
      used a "next_bit" signal, to go from bit to bit.=20
      <TABLE cellPadding=3D10>
        <TBODY>
        <TR>
          <TD bgColor=3D#d0d0f0><B>reg</B> [2:0]=20
            bit_spacing;<BR><BR><B>always</B> @(<B>posedge</B>=20
            clk)<BR><B>if</B>(state=3D=3D0)<BR>&nbsp; bit_spacing =
&lt;=3D=20
            0;<BR><B>else</B><BR><B>if</B>(Baud8Tick)<BR>&nbsp; =
bit_spacing=20
            &lt;=3D bit_spacing + 1;<BR><BR><B>wire</B> next_bit =3D=20
            (bit_spacing=3D=3D7); </TD></TR></TBODY></TABLE><BR>Finally =
a shift register=20
      collects the data bits as they come.=20
      <TABLE cellPadding=3D10>
        <TBODY>
        <TR>
          <TD bgColor=3D#d0d0f0><B>reg</B> [7:0] =
RxD_data;<BR><B>always</B>=20
            @(<B>posedge</B> clk) <B>if</B>(Baud8Tick &amp;&amp; =
next_bit=20
            &amp;&amp; state[3]) RxD_data &lt;=3D {RxD_bit, =
RxD_data[7:1]};=20
        </TD></TR></TBODY></TABLE><BR>That's it! The complete code can =
be found <A=20
      href=3D"http://www.fpga4fun.com/files/async.zip">here</A>.<BR>It =
has a few=20
      improvements; follow the comments in the code.=20
      <H4>Links</H4>
      <UL>
        <LI>More details on <A=20
        =
href=3D"http://www.erg.abdn.ac.uk/users/gorry/course/phy-pages/async.html=
">Asynchronous=20
        Communication</A> </LI></UL><BR><BR><A=20
      href=3D"http://www.fpga4fun.com/SerialInterface5.html"><FONT =
color=3Dblue=20
      size=3D2>&gt;&gt;&gt; NEXT: How to use the Transmitter and =
Receiver modules=20
      &gt;&gt;&gt;</FONT></A><BR><BR><BR>
      <HR>
      This page was last updated on April 07=20
2005.<BR><BR></TD></TR></TBODY></TABLE></BODY></HTML>

------=_NextPart_000_000E_01C66348.53969350
Content-Type: image/gif
Content-Transfer-Encoding: base64
Content-Location: http://www.fpga4fun.com/images/SerialRxDmodule.gif

R0lGODlhxABYAIAAAAAAAP///ywAAAAAxABYAAAC/oyPqcvtD6OctNqLs968+w+GFkCW5omm6sqa
4gvHDyBvdI3n4K1XfA8MSn7C2aVUTO6UEGKE52RKJ9GpodogYq3cxFb6VWi7ZEdYefYe0mWwr9Xy
HUltuZB9zeADe/66PtTXsSeYR0GoBmjm1+Sy4Gh0qGdHlai42IQQBZUpiYHo+XeJGZnH+fMF+lg6
1GrIiEPHgfoESXNDS+q6qvtUCyUbC3lkiTn2CsugunZ7FSxaOYNUlPJZnAUNm/uc7avprH3dacYN
tEJJefoKzL0cfiLO+jhNzRI98n1KzweP7A3dTBm6eYU8wAlTUE81d7gCJuN3MKLEiRQpQqyIMWNG
/jEB9znTCDKkSI/ddKh4OG7EyJUsJWK70w+lvFC7OCY0CKdXj2Eydd6rqYnkLF4ET840uWkgzX9e
yg0iahMFUy7uIDJzUvXijo50ptGrNspnL3X+sgqdxO/duo9Ow8bTGSymv5TfBOIz5IIdMLdT6YrC
kpUcMVN/cJXke3Rs4W6BBQpqyKgr3puAqn4t1/ilysjNZElG/LNvaL+kQbtZijpxMcqmQzROpVTM
29ZJMqu2qxgMa2oDO3+UnbpuGZ5uGRrmJBwo8OFywzL86zHzbhFGnfdeXLal9u1gRz0nnDa56Lld
qnu/Pvnh6+XiXRdtWzm28sRJ2VMv2tq2WM2r/rne8tyWf1cx44c+tPXEH3j74ZYcZGQh19RaDzo0
XW1KpaEfcnldVd9FDX0WGYKIWVYgcRliV+Bayx2TIl4i8kViLrMxaNdnNoKzInY2QnggeXCFA9V4
EToYHogNxsUVkD36mGCLPelX3pKH/WgYjaUN0txdUr5oX4pZQrklGWAGeWWYJnHJpJWj3WZmLGi+
OaWQarYZg1chLkjmmnjS+YE6DjV5lJ1Yycene/mcQaIpHc5XqKGL7dXlTPBUMWaj1hBGYXQXvlWp
pXMcN5ktmyo4Y56emrNQbHaaCuips3AXx6V6uprDSZ22xyatdcZ0a5xz6loDT72mySqwuxaLneye
pRq7E6FlJstsrW9o5Gy0Z8ZnLRMVOpotb9h2620d24I76xTjkivnaeiiCutI674LrwznZslovPeN
K6O9bfy5aolIssgZOwM6+SGp+koCIYVqIdMPWQQ/bODBWkIKnsOJ6PWnhyF+aKTEVAirI4qWVMnY
xXf66nGQCqv4MEASorjywikDVbDLoa72W85WKbnzuTP7PPO3QWNQAAAAOw==

------=_NextPart_000_000E_01C66348.53969350--

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -