當(dāng)前位置:首頁 > 嵌入式 > 嵌入式教程
[導(dǎo)讀] VHDL設(shè)計的串口通信程序

本模塊的功能是驗證實現(xiàn)和PC機進行基本的串口通信的功能。需要在
PC機上安裝一個串口調(diào)試工具來驗證程序的功能。
程序?qū)崿F(xiàn)了一個收發(fā)一幀10個bit(即無奇偶校驗位)的串口控
制器,10個bit是1位起始位,8個數(shù)據(jù)位,1個結(jié)束
位。串口的波特律由程序中定義的div_par參數(shù)決定,更改該參數(shù)可以實
現(xiàn)相應(yīng)的波特率。程序當(dāng)前設(shè)定的div_par 的值是0x104,對應(yīng)的波特率是
9600。用一個8倍波特率的時鐘將發(fā)送或接受每一位bit的周期時間
劃分為8個時隙以使通信同步.
程序的工作過程是:串口處于全雙工工作狀態(tài),按動key2,CPLD向PC發(fā)送皐elcome"
字符串(串口調(diào)試工具設(shè)成按ASCII碼接受方式);PC可隨時向CPLD發(fā)送0-F的十六進制
數(shù)據(jù),CPLD接受后顯示在7段數(shù)碼管上。
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;

ENTITY UART IS
   PORT (
      clk                     : IN std_logic;  
      rst                     : IN std_logic;  
      rxd                     : IN std_logic;   串行數(shù)據(jù)接收端
      txd                     : OUT std_logic; 串行數(shù)據(jù)發(fā)送端 
      en                      : OUT std_logic_vector(7 downto 0); 數(shù)碼管使能
      seg_data                : OUT std_logic_vector(7 DOWNTO 0); 數(shù)碼管數(shù)據(jù) 
      key_input               : IN std_logic  按鍵輸入
      );
END UART;

ARCHITECTURE arch OF UART IS
   //////////////////inner reg////////////////////
   SIGNAL div_reg                  :  std_logic_vector(15 DOWNTO 0);分頻計數(shù)器,分頻值由波特率決定。分頻后得到頻率8倍波特率的時鐘  
   SIGNAL div8_tras_reg            :  std_logic_vector(2 DOWNTO 0);該寄存器的計數(shù)值對應(yīng)發(fā)送時當(dāng)前位于的時隙數(shù)  
   SIGNAL div8_rec_reg             :  std_logic_vector(2 DOWNTO 0);  寄存器的計數(shù)值對應(yīng)接收時當(dāng)前位于的時隙數(shù)
   SIGNAL state_tras               :  std_logic_vector(3 DOWNTO 0);  發(fā)送狀態(tài)寄存器
   SIGNAL state_rec                :  std_logic_vector(3 DOWNTO 0); 接受狀態(tài)寄存器
   SIGNAL clkbaud_tras             :  std_logic; 以波特率為頻率的發(fā)送使能信號 
   SIGNAL clkbaud_rec              :  std_logic; 以波特率為頻率的接受使能信號 
   SIGNAL clkbaud8x                :  std_logic; 以8倍波特率為頻率的時鐘,它的作用是將發(fā)送或接受一個bit的時鐘周期分為8個時隙 
   SIGNAL recstart                 :  std_logic; 開始發(fā)送標(biāo)志
   SIGNAL recstart_tmp             :  std_logic; 開始接受標(biāo)志 
   SIGNAL trasstart                :  std_logic;  
   SIGNAL rxd_reg1                 :  std_logic; 接收寄存器1 
   SIGNAL rxd_reg2                 :  std_logic; 接收寄存器2,因為接收數(shù)據(jù)為異步信號,故用兩級緩存 
   SIGNAL txd_reg                  :  std_logic; 發(fā)送寄存器 
   SIGNAL rxd_buf                  :  std_logic_vector(7 DOWNTO 0);接受數(shù)據(jù)緩存  
   SIGNAL txd_buf                  :  std_logic_vector(7 DOWNTO 0);發(fā)送數(shù)據(jù)緩存  
   SIGNAL send_state               :  std_logic_vector(2 DOWNTO 0);每次按鍵給PC發(fā)送"Welcome"字符串,這是發(fā)送狀態(tài)寄存器  
   SIGNAL cnt_delay                :  std_logic_vector(19 DOWNTO 0);延時去抖計數(shù)器  
   SIGNAL start_delaycnt           :  std_logic;  開始延時計數(shù)標(biāo)志
   SIGNAL key_entry1               :  std_logic;  確定有鍵按下曛?
   SIGNAL key_entry2               :  std_logic;  確定有鍵按下標(biāo)志
   //////////////////////////////////////////////
   CONSTANT  div_par               :  std_logic_vector(15 DOWNTO 0) := "0000000100000100";
   分頻參數(shù),其值由對應(yīng)的波特率計算而得,按此參數(shù)分頻的時鐘頻率是波倍特率的8倍,此處值對應(yīng)9600的波特率,即分頻出的時鐘頻率是9600*8    
   SIGNAL txd_xhdl3                :  std_logic;  

BEGIN
   en <="01010101" ;7段數(shù)碼管使能信號賦值
   txd <= txd_xhdl3;  
   txd_xhdl3 <= txd_reg ;

   PROCESS(clk,rst)
   BEGIN
      
      IF (NOT rst = ’1’) THEN
         cnt_delay <= "00000000000000000000";   
         start_delaycnt <= ’0’;   
      ELSIF(clk’EVENT AND clk=’1’)THEN
         IF (start_delaycnt = ’1’) THEN
            IF (cnt_delay /= "11000011010100000000") THEN
               cnt_delay <= cnt_delay + "00000000000000000001";   
            ELSE
               cnt_delay <= "00000000000000000000";   
               start_delaycnt <= ’0’;   
            END IF;
         ELSE
            IF ((NOT key_input=’1’) AND (cnt_delay = "00000000000000000000")) THEN
               start_delaycnt <= ’1’;   
            END IF;
         END IF;
      END IF;
   END PROCESS;

   PROCESS(clk,rst)
   BEGIN
      
      IF (NOT rst = ’1’) THEN
         key_entry1 <= ’0’;   
      ELSIF(clk’EVENT AND clk=’1’)THEN
         IF (key_entry2 = ’1’) THEN
            key_entry1 <= ’0’;   
         ELSE
            IF (cnt_delay = "11000011010100000000") THEN
               IF (NOT key_input = ’1’) THEN
                  key_entry1 <= ’1’;   
               END IF;
            END IF;
         END IF;
      END IF;
   END PROCESS;

   PROCESS(clk,rst)
   BEGIN
      
      IF (NOT rst = ’1’) THEN
         div_reg <= "0000000000000000";   
      ELSIF(clk’EVENT AND clk=’1’)THEN
         IF (div_reg = div_par - "0000000000000001") THEN
            div_reg <= "0000000000000000";   
         ELSE
            div_reg <= div_reg + "0000000000000001";   
         END IF;
      END IF;
   END PROCESS;

   PROCESS(clk,rst)  分頻得到8倍波特率的時鐘
   BEGIN
      
      IF (NOT rst = ’1’) THEN
         clkbaud8x <= ’0’;   
      ELSIF(clk’EVENT AND clk=’1’)THEN
         IF (div_reg = div_par - "0000000000000001") THEN
            clkbaud8x <= NOT clkbaud8x;   
         END IF;
      END IF;
   END PROCESS;

   PROCESS(clkbaud8x,rst)
   BEGIN
      IF (NOT rst = ’1’) THEN
         div8_rec_reg <= "000";   
      ELSE IF(clkbaud8x’EVENT AND clkbaud8x = ’1’) THEN
         IF (recstart = ’1’) THEN  接收開始標(biāo)志
            div8_rec_reg <= div8_rec_reg + "001";接收開始后,時隙數(shù)在8倍波特率的時鐘下加1循環(huán)   
         END IF;
    END IF;
      END IF;
   END PROCESS;

   PROCESS(clkbaud8x,rst)
   BEGIN
      IF (NOT rst = ’1’) THEN
         div8_tras_reg <= "000";   
      ELSE IF(clkbaud8x’EVENT AND clkbaud8x = ’1’) THEN
         IF (trasstart = ’1’) THEN
            div8_tras_reg <= div8_tras_reg + "001";發(fā)送開始后,時隙數(shù)在8倍波特率的時鐘下加1循環(huán)   
         END IF;
    END IF;
      END IF;
   END PROCESS;

   PROCESS(div8_rec_reg)
   BEGIN
      IF (div8_rec_reg = "111") THEN
         clkbaud_rec <= ’1’; -在第7個時隙,接收  
      ELSE
         clkbaud_rec <= ’0’;   
      END IF;
   END PROCESS;

   PROCESS(div8_tras_reg)
   BEGIN
      IF (div8_tras_reg = "111") THEN
         clkbaud_tras <= ’1’;  在第7個時隙,發(fā)送使能信號有效,將數(shù)據(jù)發(fā)出 
      ELSE
         clkbaud_tras <= ’0’;   
      END IF;
   END PROCESS;

   PROCESS(clkbaud8x,rst)
   BEGIN
      IF (NOT rst = ’1’) THEN
         txd_reg <= ’1’;   
         trasstart <= ’0’;   
         txd_buf <= "00000000";   
         state_tras <= "0000";   
         send_state <= "000";   
         key_entry2 <= ’0’;   
      ELSE IF(clkbaud8x’EVENT AND clkbaud8x = ’1’) THEN
         IF (NOT key_entry2 = ’1’) THEN
            IF (key_entry1 = ’1’) THEN
               key_entry2 <= ’1’;   
               txd_buf <= "01110111";   "w"
            END IF;
         ELSE
            CASE state_tras IS
               WHEN "0000" =>  發(fā)送起始位
                        IF ((NOT trasstart=’1’) AND (send_state < "111") ) THEN
                           trasstart <= ’1’;    [!--empirenews.page--]
                        ELSE
                           IF (send_state < "111") THEN
                              IF (clkbaud_tras = ’1’) THEN
                                 txd_reg <= ’0’;   
                                 state_tras <= state_tras + "0001";   
                              END IF;
                           ELSE
                              key_entry2 <= ’0’;   
                              state_tras <= "0000";   
                           END IF;
                        END IF;
               WHEN "0001" => 發(fā)送第1位
                        IF (clkbaud_tras = ’1’) THEN
                           txd_reg <= txd_buf(0);   
                           txd_buf(6 DOWNTO 0) <= txd_buf(7 DOWNTO 1);   
                           state_tras <= state_tras + "0001";   
                        END IF;
               WHEN "0010" =>  發(fā)送第2位
                        IF (clkbaud_tras = ’1’) THEN
                           txd_reg <= txd_buf(0);   
                           txd_buf(6 DOWNTO 0) <= txd_buf(7 DOWNTO 1);   
                           state_tras <= state_tras + "0001";   
                        END IF;
               WHEN "0011" =>  發(fā)送第3位
                        IF (clkbaud_tras = ’1’) THEN
                           txd_reg <= txd_buf(0);   
                           txd_buf(6 DOWNTO 0) <= txd_buf(7 DOWNTO 1);   
                           state_tras <= state_tras + "0001";   
                        END IF;
               WHEN "0100" => 發(fā)送第4位
                        IF (clkbaud_tras = ’1’) THEN
                           txd_reg <= txd_buf(0);   
                           txd_buf(6 DOWNTO 0) <= txd_buf(7 DOWNTO 1);   
                           state_tras <= state_tras + "0001";   
                        END IF;
               WHEN "0101" => 發(fā)送第5位
                        IF (clkbaud_tras = ’1’) THEN
                           txd_reg <= txd_buf(0);   
                           txd_buf(6 DOWNTO 0) <= txd_buf(7 DOWNTO 1);   
                           state_tras <= state_tras + "0001";   
                        END IF;
               WHEN "0110" => 發(fā)送第6位
                        IF (clkbaud_tras = ’1’) THEN
                           txd_reg <= txd_buf(0);   
                           txd_buf(6 DOWNTO 0) <= txd_buf(7 DOWNTO 1);   
                           state_tras <= state_tras + "0001";   
                        END IF;
               WHEN "0111" => 發(fā)送第7位
                        IF (clkbaud_tras = ’1’) THEN
                           txd_reg <= txd_buf(0);   
                           txd_buf(6 DOWNTO 0) <= txd_buf(7 DOWNTO 1);   
                           state_tras <= state_tras + "0001";   
                        END IF;
               WHEN "1000" =>  發(fā)送第8位
                        IF (clkbaud_tras = ’1’) THEN
                           txd_reg <= txd_buf(0);   
                           txd_buf(6 DOWNTO 0) <= txd_buf(7 DOWNTO 1);   
                           state_tras <= state_tras + "0001";   
                        END IF;
               WHEN "1001" =>  發(fā)送停止位
                        IF (clkbaud_tras = ’1’) THEN
                           txd_reg <= ’1’;   
                           txd_buf <= "01010101";   
                           state_tras <= state_tras + "0001";   
                        END IF;
               WHEN "1111" =>
                        IF (clkbaud_tras = ’1’) THEN
                           state_tras <= state_tras + "0001";   
                           send_state <= send_state + "001";   
                           trasstart <= ’0’;   
                           CASE send_state IS
                              WHEN "000" =>
                                       txd_buf <= "01100101"; "e"  
                              WHEN "001" =>
                                       txd_buf <= "01101100"; "l"  
                              WHEN "010" =>
                                       txd_buf <= "01100011";  "c" 
                              WHEN "011" =>
                                       txd_buf <= "01101111"; "o" 
                              WHEN "100" =>
                                       txd_buf <= "01101101";  "m" 
                              WHEN "101" =>
                                       txd_buf <= "01100101"; "e"  
                              WHEN OTHERS  =>
                                       txd_buf <= "00000000"; 
                             
                           END CASE;
                        END IF;
               WHEN OTHERS  =>
                        IF (clkbaud_tras = ’1’) THEN
                           state_tras <= state_tras + "0001";   
                           trasstart <= ’1’;   
                        END IF;
              
            END CASE;
         END IF;
      END IF;
 END IF;
   END PROCESS;

   PROCESS(clkbaud8x,rst)  接受PC機的數(shù)據(jù)
   BEGIN
      IF (NOT rst = ’1’) THEN
         rxd_reg1 <= ’0’;   
         rxd_reg2 <= ’0’;   
         rxd_buf <= "00000000";   
         state_rec <= "0000";   
         recstart <= ’0’;   
         recstart_tmp <= ’0’;   
      ELSE IF(clkbaud8x’EVENT AND clkbaud8x = ’1’) THEN
         rxd_reg1 <= rxd;   
         rxd_reg2 <= rxd_reg1;   
         IF (state_rec = "0000") THEN
            IF (recstart_tmp = ’1’) THEN
               recstart <= ’1’;   
               recstart_tmp <= ’0’;   
               state_rec <= state_rec + "0001";   
            ELSE
               IF ((NOT rxd_reg1 AND rxd_reg2) = ’1’) THEN 檢測到起始位的下降沿,進入接受狀態(tài)
                  recstart_tmp <= ’1’;   
               END IF;
            END IF;
         ELSE
            IF (state_rec >= "0001" AND state_rec<="1000") THEN
               IF (clkbaud_rec = ’1’) THEN
                  rxd_buf(7) <= rxd_reg2;   
                  rxd_buf(6 DOWNTO 0) <= rxd_buf(7 DOWNTO 1);   
                  state_rec <= state_rec + "0001";   
               END IF;
            ELSE
               IF (state_rec = "1001") THEN
                  IF (clkbaud_rec = ’1’) THEN
                     state_rec <= "0000";   
                     recstart <= ’0’;   
                  END IF;
               END IF;
            END IF;
         END IF;
      END IF;
 END IF;
   END PROCESS;

   PROCESS(rxd_buf)   將接受的數(shù)據(jù)用數(shù)碼管顯示出來
   BEGIN
      CASE rxd_buf IS
         WHEN "00110000" =>
                  seg_data <= "00000011"; 0  
         WHEN "00110001" =>
                  seg_data <= "10011111"; 1 
         WHEN "00110010" =>
                  seg_data <= "00100101"; 2 
         WHEN "00110011" =>
                  seg_data <= "00001101"; 3      
         WHEN "00110100" =>
                  seg_data <= "10011001"; 4  
&nbs p;        WHEN "00110101" =>
                  seg_data <= "01001001"; 5 
         WHEN "00110110" =>
                  seg_data <= "01000001"; 6  
         WHEN "00110111" =>
                  seg_data <= "00011111"; 7  
         WHEN "00111000" =>
                  seg_data <= "00000001"; 8   
         WHEN "00111001" =>
                  seg_data <= "00001001"; 9  
         WHEN "01000001" =>
                  seg_data <= "00010001"; A  
         WHEN "01000010" =>
                  seg_data <= "11000001"; B  
         WHEN "01000011" =>
                  seg_data <= "01100011"; C  
         WHEN "01000100" =>
                  seg_data <= "10000101"; D 
         WHEN "01000101" =>
                  seg_data <= "01100001"; E  
         WHEN "01000110" =>
                  seg_data <= "01110001"; F   
         WHEN OTHERS  =>
                  seg_data <= "11111111";          
      END CASE;
   END PROCESS;
END arch;

本站聲明: 本文章由作者或相關(guān)機構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點,本站亦不保證或承諾內(nèi)容真實性等。需要轉(zhuǎn)載請聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請及時聯(lián)系本站刪除。
換一批
延伸閱讀

9月2日消息,不造車的華為或?qū)⒋呱龈蟮莫毥谦F公司,隨著阿維塔和賽力斯的入局,華為引望愈發(fā)顯得引人矚目。

關(guān)鍵字: 阿維塔 塞力斯 華為

加利福尼亞州圣克拉拉縣2024年8月30日 /美通社/ -- 數(shù)字化轉(zhuǎn)型技術(shù)解決方案公司Trianz今天宣布,該公司與Amazon Web Services (AWS)簽訂了...

關(guān)鍵字: AWS AN BSP 數(shù)字化

倫敦2024年8月29日 /美通社/ -- 英國汽車技術(shù)公司SODA.Auto推出其旗艦產(chǎn)品SODA V,這是全球首款涵蓋汽車工程師從創(chuàng)意到認證的所有需求的工具,可用于創(chuàng)建軟件定義汽車。 SODA V工具的開發(fā)耗時1.5...

關(guān)鍵字: 汽車 人工智能 智能驅(qū)動 BSP

北京2024年8月28日 /美通社/ -- 越來越多用戶希望企業(yè)業(yè)務(wù)能7×24不間斷運行,同時企業(yè)卻面臨越來越多業(yè)務(wù)中斷的風(fēng)險,如企業(yè)系統(tǒng)復(fù)雜性的增加,頻繁的功能更新和發(fā)布等。如何確保業(yè)務(wù)連續(xù)性,提升韌性,成...

關(guān)鍵字: 亞馬遜 解密 控制平面 BSP

8月30日消息,據(jù)媒體報道,騰訊和網(wǎng)易近期正在縮減他們對日本游戲市場的投資。

關(guān)鍵字: 騰訊 編碼器 CPU

8月28日消息,今天上午,2024中國國際大數(shù)據(jù)產(chǎn)業(yè)博覽會開幕式在貴陽舉行,華為董事、質(zhì)量流程IT總裁陶景文發(fā)表了演講。

關(guān)鍵字: 華為 12nm EDA 半導(dǎo)體

8月28日消息,在2024中國國際大數(shù)據(jù)產(chǎn)業(yè)博覽會上,華為常務(wù)董事、華為云CEO張平安發(fā)表演講稱,數(shù)字世界的話語權(quán)最終是由生態(tài)的繁榮決定的。

關(guān)鍵字: 華為 12nm 手機 衛(wèi)星通信

要點: 有效應(yīng)對環(huán)境變化,經(jīng)營業(yè)績穩(wěn)中有升 落實提質(zhì)增效舉措,毛利潤率延續(xù)升勢 戰(zhàn)略布局成效顯著,戰(zhàn)新業(yè)務(wù)引領(lǐng)增長 以科技創(chuàng)新為引領(lǐng),提升企業(yè)核心競爭力 堅持高質(zhì)量發(fā)展策略,塑強核心競爭優(yōu)勢...

關(guān)鍵字: 通信 BSP 電信運營商 數(shù)字經(jīng)濟

北京2024年8月27日 /美通社/ -- 8月21日,由中央廣播電視總臺與中國電影電視技術(shù)學(xué)會聯(lián)合牽頭組建的NVI技術(shù)創(chuàng)新聯(lián)盟在BIRTV2024超高清全產(chǎn)業(yè)鏈發(fā)展研討會上宣布正式成立。 活動現(xiàn)場 NVI技術(shù)創(chuàng)新聯(lián)...

關(guān)鍵字: VI 傳輸協(xié)議 音頻 BSP

北京2024年8月27日 /美通社/ -- 在8月23日舉辦的2024年長三角生態(tài)綠色一體化發(fā)展示范區(qū)聯(lián)合招商會上,軟通動力信息技術(shù)(集團)股份有限公司(以下簡稱"軟通動力")與長三角投資(上海)有限...

關(guān)鍵字: BSP 信息技術(shù)
關(guān)閉
關(guān)閉