<?xml version="1.0" encoding="utf-8" ?>

<rss version="2.0" 
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:admin="http://webns.net/mvcb/"
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
   xmlns:wfw="http://wellformedweb.org/CommentAPI/"
   xmlns:content="http://purl.org/rss/1.0/modules/content/"
   >
<channel>
    <title>Lothar Miller - Graycode Umwandlung</title>
    <link>http://www.lothar-miller.de/s9y/</link>
    <description>Elektronik und Musik</description>
    <dc:language>de</dc:language>
    <generator>Serendipity 1.3.1 - http://www.s9y.org/</generator>
    <pubDate>Wed, 20 Apr 2011 06:50:55 GMT</pubDate>

    <image>
        <url>http://www.lothar-miller.de/s9y/templates/bulletproof/img/s9y_banner_small.png</url>
        <title>RSS: Lothar Miller - Graycode Umwandlung - Elektronik und Musik</title>
        <link>http://www.lothar-miller.de/s9y/</link>
        <width>100</width>
        <height>21</height>
    </image>

<item>
    <title>Graycode Umwandlung</title>
    <link>http://www.lothar-miller.de/s9y/archives/68-Graycode-Umwandlung.html</link>
            <category>Graycode Umwandlung</category>
    
    <comments>http://www.lothar-miller.de/s9y/archives/68-Graycode-Umwandlung.html#comments</comments>
    <wfw:comment>http://www.lothar-miller.de/s9y/wfwcomment.php?cid=68</wfw:comment>

    <slash:comments>0</slash:comments>
    <wfw:commentRss>http://www.lothar-miller.de/s9y/rss.php?version=2.0&amp;type=comments&amp;cid=68</wfw:commentRss>
    

    <author>nospam@example.com (Lothar Miller)</author>
    <content:encoded>
    
&lt;p&gt;Der &lt;b&gt;Graycode &lt;/b&gt;wird verwendet, wenn asynchron eine &lt;b&gt;Positionsinformation &lt;/b&gt;übertragen werden soll (wie z.B. von einem Drehencoder), denn bei der Gray-Codierung wird bei einem Zählschritt zum nächsten immer nur 1 Bit umgeschaltet. Eine weitere Anwendung für den Graycode ist auch die Übertragung des &lt;b&gt;Füllstands  eines Fifos&lt;/b&gt; (also eigentlich die Position eines Zeigers im Fifo). Auch hier ist von Vorteil, dass die auswertende Logik höchstens um 1 Adressschritt falsch liegt. &lt;/p&gt;&lt;p&gt;&lt;b&gt;Als Beispiel&lt;/b&gt;:&lt;br /&gt;Ist die Zählstand des Fifos auf 7 (= binär 0111) und es wird weitergezählt auf 8 (=binär 1000), dann werden bei binärer Übertragung &lt;i&gt;alle Bits&lt;/i&gt; umgeschaltet. Bei asynchroner Abtastung könnte jetzt kurzzeitig jede beliebige Kombination von 0000 bis 1111 auftauchen. &lt;br /&gt;Bei einer Übertragung im Graycode sieht der Schritt von 7 nach 8 so aus: 0100 --&amp;gt; 1100. Hier wird also &lt;i&gt;garantiert keine falsche Information&lt;/i&gt; generiert, sondern entweder der alte Wert (7) oder der neue Wert (8) erkannt.&lt;/p&gt;&lt;p&gt;Hier sind ein paar Umwandlungsroutinen, deren Grundlagen auf &lt;a href=&quot;http://www.wisc-online.com/ViewObject.aspx?ID=IAU8307&quot; target=&quot;_blank&quot; title=&quot;Basics Graycode-Umwandlung&quot;&gt;www.wisc-online.com&lt;/a&gt; schön erklärt sind.&lt;/p&gt;&lt;hr width=&quot;100%&quot; size=&quot;2&quot; /&gt;&lt;pre&gt;&lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;library&lt;/span&gt; IEEE;&lt;br /&gt;&lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;use&lt;/span&gt; IEEE.STD_LOGIC_1164.&lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;ALL&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;use&lt;/span&gt; IEEE.NUMERIC_STD.&lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;ALL&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;entity&lt;/span&gt; Gray &lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;is&lt;/span&gt;&lt;br /&gt;    &lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;Port&lt;/span&gt; ( gin  : &lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;in&lt;/span&gt;   &lt;span style=&quot;color: rgb(0, 119, 68); font-weight: bold;&quot;&gt;STD_LOGIC_VECTOR&lt;/span&gt; (&lt;span style=&quot;color: rgb(0, 0, 221); font-weight: bold;&quot;&gt;15&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;downto&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 221); font-weight: bold;&quot;&gt;0&lt;/span&gt;);&lt;br /&gt;           bout : &lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;out&lt;/span&gt;  &lt;span style=&quot;color: rgb(0, 119, 68); font-weight: bold;&quot;&gt;STD_LOGIC_VECTOR&lt;/span&gt; (&lt;span style=&quot;color: rgb(0, 0, 221); font-weight: bold;&quot;&gt;15&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;downto&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 221); font-weight: bold;&quot;&gt;0&lt;/span&gt;);&lt;br /&gt;           bin  : &lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;in&lt;/span&gt;   &lt;span style=&quot;color: rgb(0, 119, 68); font-weight: bold;&quot;&gt;STD_LOGIC_VECTOR&lt;/span&gt; (&lt;span style=&quot;color: rgb(0, 0, 221); font-weight: bold;&quot;&gt;15&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;downto&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 221); font-weight: bold;&quot;&gt;0&lt;/span&gt;);&lt;br /&gt;           gout : &lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;out&lt;/span&gt;  &lt;span style=&quot;color: rgb(0, 119, 68); font-weight: bold;&quot;&gt;STD_LOGIC_VECTOR&lt;/span&gt; (&lt;span style=&quot;color: rgb(0, 0, 221); font-weight: bold;&quot;&gt;15&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;downto&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 221); font-weight: bold;&quot;&gt;0&lt;/span&gt;));&lt;br /&gt;&lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;end&lt;/span&gt; Gray;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;architecture&lt;/span&gt; Behavioral &lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;of&lt;/span&gt; Gray &lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;is&lt;/span&gt;&lt;br /&gt; &lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;signal&lt;/span&gt; gi   : &lt;span style=&quot;color: rgb(0, 119, 68); font-weight: bold;&quot;&gt;unsigned&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 0, 221); font-weight: bold;&quot;&gt;15&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;downto&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 221); font-weight: bold;&quot;&gt;0&lt;/span&gt;);&lt;br /&gt; &lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;signal&lt;/span&gt; go   : &lt;span style=&quot;color: rgb(0, 119, 68); font-weight: bold;&quot;&gt;unsigned&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 0, 221); font-weight: bold;&quot;&gt;15&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;downto&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 221); font-weight: bold;&quot;&gt;0&lt;/span&gt;);&lt;br /&gt; &lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;signal&lt;/span&gt; bi   : &lt;span style=&quot;color: rgb(0, 119, 68); font-weight: bold;&quot;&gt;unsigned&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 0, 221); font-weight: bold;&quot;&gt;15&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;downto&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 221); font-weight: bold;&quot;&gt;0&lt;/span&gt;);&lt;br /&gt; &lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;signal&lt;/span&gt; bo   : &lt;span style=&quot;color: rgb(0, 119, 68); font-weight: bold;&quot;&gt;unsigned&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 0, 221); font-weight: bold;&quot;&gt;15&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;downto&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 221); font-weight: bold;&quot;&gt;0&lt;/span&gt;);&lt;br /&gt; &lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;signal&lt;/span&gt; bop2 : &lt;span style=&quot;color: rgb(0, 119, 68); font-weight: bold;&quot;&gt;unsigned&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 0, 221); font-weight: bold;&quot;&gt;15&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;downto&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 221); font-weight: bold;&quot;&gt;0&lt;/span&gt;);&lt;br /&gt;&lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;begin&lt;/span&gt;&lt;br /&gt;  bi &amp;lt;= &lt;span style=&quot;color: rgb(0, 119, 68); font-weight: bold;&quot;&gt;unsigned&lt;/span&gt;(bin);&lt;br /&gt;  gi &amp;lt;= &lt;span style=&quot;color: rgb(0, 119, 68); font-weight: bold;&quot;&gt;unsigned&lt;/span&gt;(gin);&lt;br /&gt;  gout &amp;lt;= &lt;span style=&quot;color: rgb(0, 119, 68); font-weight: bold;&quot;&gt;std_logic_vector&lt;/span&gt;(go);&lt;br /&gt;  bout &amp;lt;= &lt;span style=&quot;color: rgb(0, 119, 68); font-weight: bold;&quot;&gt;std_logic_vector&lt;/span&gt;(bo); &lt;span style=&quot;color: rgb(136, 136, 136);&quot;&gt;-- alternativ bop2;&lt;/span&gt;&lt;br /&gt;  &lt;br /&gt;  Gray2BinaryPower2: &lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;process&lt;/span&gt; (gi) &lt;br /&gt;  &lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;variable&lt;/span&gt; temp : &lt;span style=&quot;color: rgb(0, 119, 68); font-weight: bold;&quot;&gt;unsigned&lt;/span&gt;(&lt;span style=&quot;color: rgb(0, 0, 221); font-weight: bold;&quot;&gt;15&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;downto&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 221); font-weight: bold;&quot;&gt;0&lt;/span&gt;);&lt;br /&gt;  &lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;begin&lt;/span&gt;&lt;br /&gt;     temp := gi;&lt;br /&gt;     temp := temp &lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;xor&lt;/span&gt; shift_right(temp,&lt;span style=&quot;color: rgb(0, 0, 221); font-weight: bold;&quot;&gt;8&lt;/span&gt;);&lt;br /&gt;     temp := temp &lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;xor&lt;/span&gt; shift_right(temp,&lt;span style=&quot;color: rgb(0, 0, 221); font-weight: bold;&quot;&gt;4&lt;/span&gt;);&lt;br /&gt;     temp := temp &lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;xor&lt;/span&gt; shift_right(temp,&lt;span style=&quot;color: rgb(0, 0, 221); font-weight: bold;&quot;&gt;2&lt;/span&gt;);&lt;br /&gt;     temp := temp &lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;xor&lt;/span&gt; shift_right(temp,&lt;span style=&quot;color: rgb(0, 0, 221); font-weight: bold;&quot;&gt;1&lt;/span&gt;);&lt;br /&gt;     bop2 &amp;lt;= temp;&lt;br /&gt;  &lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;end&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;process&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;  Gray2Binary: &lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;process&lt;/span&gt; (gi) &lt;br /&gt;  &lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;variable&lt;/span&gt; temp : &lt;span style=&quot;color: rgb(0, 119, 68); font-weight: bold;&quot;&gt;unsigned&lt;/span&gt;(gi&lt;span style=&quot;color: rgb(0, 68, 221);&quot;&gt;&#039;l&lt;/span&gt;eft &lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;downto&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 221); font-weight: bold;&quot;&gt;0&lt;/span&gt;);&lt;br /&gt;  &lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;begin&lt;/span&gt;&lt;br /&gt;     temp := gi;&lt;br /&gt;     &lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;in&lt;/span&gt; gi&lt;span style=&quot;color: rgb(0, 68, 221);&quot;&gt;&#039;l&lt;/span&gt;eft-&lt;span style=&quot;color: rgb(0, 0, 221); font-weight: bold;&quot;&gt;1&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;downto&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 221); font-weight: bold;&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;loop&lt;/span&gt;&lt;br /&gt;        temp(i) := gi(i) &lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;xor&lt;/span&gt; temp(i+&lt;span style=&quot;color: rgb(0, 0, 221); font-weight: bold;&quot;&gt;1&lt;/span&gt;);&lt;br /&gt;     &lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;end&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;loop&lt;/span&gt;;&lt;br /&gt;     bo &amp;lt;= temp;&lt;br /&gt;  &lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;end&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;process&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;  Binary2Gray: &lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;process&lt;/span&gt; (bi) &lt;br /&gt;  &lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;begin&lt;/span&gt;&lt;br /&gt;     go &amp;lt;= bi &lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;xor&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 68, 221);&quot;&gt;&#039;0&#039;&lt;/span&gt;&amp;amp;bi(bi&lt;span style=&quot;color: rgb(0, 68, 221);&quot;&gt;&#039;l&lt;/span&gt;eft &lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;downto&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 0, 221); font-weight: bold;&quot;&gt;1&lt;/span&gt;);&lt;br /&gt;  &lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;end&lt;/span&gt; &lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;process&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(0, 136, 0); font-weight: bold;&quot;&gt;end&lt;/span&gt; Behavioral;&lt;/pre&gt;&lt;hr width=&quot;100%&quot; size=&quot;2&quot; /&gt;&lt;p&gt;Die Berechnung über &lt;i&gt;Gray2BinaryPower2 &lt;/i&gt;funktioniert nur mit Busbreiten, die eine 2er Potenz sind, also 4, 8, 16... Zudem muß dann die Anzahl der shift-Operationen angepasst werden. Im obigen Beispiel ist eine Busbreite von 16 Bits verwendet, für einen 8 Bit Graycode entfiele der &lt;i&gt;shift_right(temp,8)&lt;/i&gt;.&lt;br /&gt;&lt;br /&gt;Hier gibt es die VHDL-Datei und die passende Testbench gezippt: &lt;a href=&quot;http://www.lothar-miller.de/s9y/uploads/GrayBinary.zip&quot; title=&quot;GrayBinary.zip&quot; target=&quot;_blank&quot;&gt;GrayBinary.zip&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Wer sowas für C braucht, sollte mal dort schauen: &lt;a title=&quot;Graycode C&quot; target=&quot;_blank&quot; href=&quot;http://www.lothar-miller.de/s9y/archives/69-Graycode-Umwandlung.html&quot;&gt;Graycode-Umwandlung in C&lt;/a&gt;&lt;/p&gt;

 
    </content:encoded>

    <pubDate>Sun, 17 Jan 2010 20:40:00 +0100</pubDate>
    <guid isPermaLink="false">http://www.lothar-miller.de/s9y/archives/68-guid.html</guid>
    
</item>

</channel>
</rss>
