1 / 85

RS-232 實習

RS-232 實習. VHDL 數位電路實習與專題設計 文魁資訊 -UE301. 單元 12-1 鮑率產生器實習. 實驗目的 了解 RS-232 串列傳輸原理 了解鮑率之原理與鮑率產生器之實現. 相關知識. 串列傳輸示意圖. 電話通信網路系統. 相關知識. RS-232 DB9 與 DB25 腳位對照表. RS-232 DB9 與 DB25 連接頭. 相關知識. RS-232 的信號電壓位準定義 最大電壓範圍為 ±25V ; 在輸入端, -3V 至 -25V 定義為邏輯’ 1’ ; 在輸入端, +3V 至 +25V 定義為邏輯’ 0’ ;

kelda
Télécharger la présentation

RS-232 實習

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. RS-232實習 VHDL數位電路實習與專題設計 文魁資訊-UE301

  2. 單元12-1 鮑率產生器實習 • 實驗目的 • 了解RS-232串列傳輸原理 • 了解鮑率之原理與鮑率產生器之實現 VHDL數位電路實習與專題設計

  3. 相關知識 串列傳輸示意圖 電話通信網路系統 VHDL數位電路實習與專題設計

  4. 相關知識 RS-232 DB9與DB25腳位對照表 RS-232 DB9與DB25連接頭 VHDL數位電路實習與專題設計

  5. 相關知識 • RS-232的信號電壓位準定義 • 最大電壓範圍為±25V; • 在輸入端,-3V至-25V定義為邏輯’1’; • 在輸入端,+3V至+25V定義為邏輯’0’; • 在輸出端,-5V至-15V定義為邏輯’1’; • 在輸出端,+5V至+15V定義為邏輯’0’; • ±3V之間定義為轉態區; RS-232的信號電壓位準示意圖 VHDL數位電路實習與專題設計

  6. 相關知識 • 位準轉換電路 MAX232結構 VHDL數位電路實習與專題設計

  7. 相關知識 • RS-232傳輸格式 • 同位元偵錯法 VHDL數位電路實習與專題設計

  8. 鮑率產生器 • 每秒所傳送的位元數(bps, bit per second)為單位,也就是所謂的鮑率(Baud rate),例如9600的鮑率就是一秒鐘可傳送9600位元的資料,也就是說傳送一個位元需要花1/9600秒。 鮑率選擇表 VHDL數位電路實習與專題設計

  9. 鮑率產生器 • 鮑率產生器方塊圖 VHDL數位電路實習與專題設計

  10. 實驗功能 • 以指撥開關當作鮑率選擇信號sel,以選擇8種不同的鮑率(bclk),包括有38400, 19200, 9600, 4800, 2400, 1200, 600, 300等,以及鮑率的8倍頻率(bclkx8)。 VHDL數位電路實習與專題設計

  11. 1 ------------------------------------------------------------------- 2 --實驗名稱:RS-232實習 3 --檔案名稱:br_gen.vhd 4 --功 能:鮑率產生器 5 --日 期:2003.8.8 6 ------------------------------------------------------------------- 7 library ieee; 8 use ieee.std_logic_1164.all; 9 use ieee.std_logic_unsigned.all; 11 entity br_gen is 12 generic(divisor: integer := 3); 13 port( 14 sysclk:in std_logic; --system clock 15 sel :in std_logic_vector(2 downto 0); 16 bclkx8:buffer std_logic; --baud rate X 8 17 bclk :out std_logic --baud rate 18 ); 19 end br_gen; 21 architecture arch of br_gen is 22 signal cnt2,clkdiv: std_logic; 23 signal ctr1: std_logic_vector (7 downto 0):= "00000000"; 24 signal ctr2: std_logic_vector (2 downto 0):= "000"; 25 begin 26 ----- clk divider ----- 27 process (sysclk) 28 variable cnt1 : integer range 0 to divisor; 29 variable divisor2 : integer range 0 to divisor; 30 begin 31 divisor2 := divisor/2; 32 if (sysclk'event and sysclk='1') then 33 if cnt1=divisor then 34 cnt1 := 1; 35 else 36 cnt1 := cnt1 + 1; 37 end if; 38 end if; 39 if (sysclk'event and sysclk='1') then 40 if (( cnt1=divisor2) or (cnt1=divisor)) then 41 cnt2 <= not cnt2; 42 end if; 43 end if; 44 end process; 45 clkdiv<= cnt2 ; 46 ----- 8 bits counter ----- 47 process (clkdiv) 48 begin 49 if(rising_edge(clkdiv)) then 50 ctr1 <= ctr1+1; 51 end if; 52 end process; 53 ----- MUX ----- 54 bclkx8<=ctr1(CONV_INTEGER(sel)); 55 ----- clkdiv8 ----- 56 process (bclkx8) 57 begin 58 if(rising_edge(bclkx8)) then 59 ctr2 <= ctr2+1; 60 end if; 61 end process; 62 bclk <= ctr2(2); 63 end arch; 鮑率產生器程式碼 VHDL數位電路實習與專題設計

  12. 功能模擬與CPLD下載驗證 • br_gen功能模擬圖 VHDL數位電路實習與專題設計

  13. 功能模擬與CPLD下載驗證 • sel = ”000”時的功能模擬圖 VHDL數位電路實習與專題設計

  14. 功能模擬與CPLD下載驗證 • sel = ”001”時的功能模擬圖 VHDL數位電路實習與專題設計

  15. 功能模擬與CPLD下載驗證 • 腳位配置表 VHDL數位電路實習與專題設計

  16. 鮑率產生器 • 練習 • 請設計出具有鮑率5倍頻率的bclkx5之鮑率產生器。 • 請設計出系統頻率為4.9152MHz的鮑率產生器。 • 請設計出系統頻率為7.3728MHz的鮑率產生器。 VHDL數位電路實習與專題設計

  17. 單元12-2 RS-232串列傳輸接收器實習I • 實驗目的 • 了解正確擷取串列傳輸資料的方法。 • 了解串列傳輸接收器之實現方法。 VHDL數位電路實習與專題設計

  18. 相關知識 • 以bclkx8作rxd串列資料信號的取樣示意圖 VHDL數位電路實習與專題設計

  19. 相關知識 • 串列傳輸接收器功能方塊圖 VHDL數位電路實習與專題設計

  20. 串列傳輸接收器信號說明表 VHDL數位電路實習與專題設計

  21. 串列傳輸接收器 • 我們以8個資料位元(data)、0個同位元(parity bit)、1個停止位元(stop bit)的串列傳輸接收器為例,說明設計方法的步驟如下 • 判斷rxd的電位,當接收器偵測到低電位,則跳到狀態2,否則持續在狀態1判斷。 • 以取樣頻率bclkx8來計數,若連續四個時脈週期都為低電位,則確定為起始位元,並跳到狀態3,否則將視為雜訊,並回到狀態1。 • 以取樣頻率bclkx8來計數,每數8次時,通知移位暫存器往左移位並將rxd訊號載入,另外通知位元接收計數器加一。當接收完8筆資料位元後,也就是位元接收計數器由0加到8時,必須判斷rxd是否為高電位,若是低電位,則視為接收錯誤並回到步驟1,若是高電位,則確定為結束位元,並將移位暫存器資料載入接收器輸出暫存器,並回到步驟1等待下一筆資料。 VHDL數位電路實習與專題設計

  22. 串列傳輸接收器SM圖 VHDL數位電路實習與專題設計

  23. 實驗功能 • 接收RS-232介面的rxd串列資料訊號,當接收完一筆串列資料時,rxd_readyH信號會有一個脈衝信號產生,接收到的資料會由RDR輸出。 VHDL數位電路實習與專題設計

  24. 1 ------------------------------------------------------------------- 2 --實驗名稱:RS-232實習 3 --檔案名稱:uart_receiver.vhd 4 --功 能:串列傳輸接收器;1 start bit, 8 data bits, 1 stop bit 5 --日 期:2003.8.8 6 ------------------------------------------------------------------- 7 library ieee; 8 use ieee.std_logic_1164.all; 9 use ieee.std_logic_unsigned.all; 10 use ieee.std_logic_arith.all; 11 12 entity uart_receiver is 13 port( 14 sysclk :in std_logic; 15 rst_n :in std_logic; 16 bclkx8 :in std_logic; 17 rxd :in std_logic; 18 rxd_readyH:out std_logic; 19 RDR:out std_logic_vector(7 downto 0) 20 ); 21 end uart_receiver; 22 23 architecture arch of uart_receiver is 24 type statetype is (idle,start_detected,recv_data); 25 signal state,nextstate:statetype; 26 signal inc1,inc2,clr1,clr2:std_logic; 27 signal shftRSR,load_RDR:std_logic; 28 signal bclkx8_dlayed,bclkx8_rising:std_logic; 29 signal RSR:std_logic_vector(7 downto 0); 30 signal ct1:integer range 0 to 7; 31 signal ct2:integer range 0 to 8; 32 signal ok_en: std_logic; 33 begin 34 bclkx8_rising<=bclkx8 and(not bclkx8_dlayed); 35 ---------- FSM of UART receiver --------- 36 process(state,rxd,ct1,ct2,bclkx8_rising) 37 begin 38 ----- initial value ----- 39 inc1<='0';inc2<='0';clr1<='0';clr2<='0'; 40 shftRSR<='0';load_RDR<='0';ok_en<='0'; 41 ----- state machine ----- 42 case state is 43 ----- idle state; standby, wait until rxd='0' ----- 44 when idle=> 45 if (rxd='0') then 46 nextstate<=start_detected; 47 else 48 nextstate<=idle; 49 end if; 50 ----- start_detected state; determine whether start bit ----- 51 when start_detected=> 52 if (bclkx8_rising='0') then 53 nextstate<=start_detected; 54 elsif (rxd='1') then 55 clr1<='1'; 56 nextstate<=idle; 57 elsif (ct1=3) then 58 clr1<='1'; 59 nextstate<=recv_data; 串列傳輸接收器程式碼 VHDL數位電路實習與專題設計

  25. 60 else 61 inc1<='1'; 62 nextstate<=start_detected; 63 end if; 64 ----- receive data state ---- 65 when recv_data=> 66 if (bclkx8_rising='0') then 67 nextstate<=recv_data; 68 else 69 inc1<='1'; 70 if (ct1/=7) then 71 nextstate<=recv_data; 72 elsif (ct2/=8) then 73 shftRSR<='1'; 74 inc2<='1'; 75 clr1<='1'; 76 nextstate<=recv_data; 77 elsif (rxd='0') then 78 nextstate<=idle; 79 clr1<='1'; 80 clr2<='1'; 81 else 82 load_RDR<='1'; 83 ok_en<='1'; 84 clr1<='1'; 85 clr2<='1'; 86 nextstate<=idle; 87 end if; 88 end if; 89 end case; 90 end process; 91 ---------- update state and value of register ---------- 92 process(sysclk,rst_n) 93 begin 94 if (rst_n='0') then 95 state<=idle; 96 bclkx8_dlayed<='0'; 97 ct1<=0; 98 ct2<=0; 99 RDR<=”00000000”; 100 elsif (sysclk'event and sysclk='1') then 101 state<=nextstate; 102 if(clr1='1')then ct1<=0;elsif(inc1='1')then ct1<=ct1+1;end if; 103 if(clr2='1')then ct2<=0;elsif(inc2='1')then ct2<=ct2+1;end if; 104 if(shftRSR='1')then RSR<=rxd & RSR(7 downto 1);end if; 105 if(load_RDR='1')then RDR<=RSR;end if; 106 if(ok_en='1')then rxd_readyH<='1';else rxd_readyH<='0';end if; 107 ---- generator bclk delay signal for determine bclkx8 rising ---- 108 bclkx8_dlayed<=bclkx8; 109 end if; 110 end process; 111 end arch; 串列傳輸接收器程式碼 VHDL數位電路實習與專題設計

  26. 1 1 0 0 1 0 0 stop bit 1 start bit 功能模擬 • 串列傳輸接收器功能模擬圖 VHDL數位電路實習與專題設計

  27. 練習 • 請設計出可選擇資料位元數的串列傳輸接收器,資料位元數可為5, 6, 7, 或8位元。 • 請設計出可選擇停止位元數的串列傳輸接收器,停止位元數可為1, 1.5, 或2位元。 • 請設計出具有同位元檢查功能的串列傳輸接收器,而且可選擇偶同位元檢查或是奇同位元檢查。 VHDL數位電路實習與專題設計

  28. 單元12-3 RS-232串列傳輸接收器實習II 紅外線距離感測器資料擷取實習 • 實驗目的 • 將串列傳輸接收器應用於紅外線距離感測器信號的擷取。 • 以DIRRS+紅外線距離感測器的Serial HEX模式來擷取距離資料。 VHDL數位電路實習與專題設計

  29. 相關知識 • 紅外線距離感測器 • 紅外線距離感測器DIRRS+ (Digital Infra-Red Ranging System Plus) • 此模組包含有夏普(Sharp)的GP2D12距離感測器以及具有類比轉數位功能的轉接電路板 • 可感測的距離範圍為10公分到80公分之間 • 感測距離在10公分時,感測器會有2.6V的電壓輸出 • 感測距離在80公分時,感測器會有0V的電壓輸出 • 類比電壓訊號經過轉換電路後分別可以得到8位元數位信號 • ”11111111”代表10公分的距離信號 • ”00000000”代表80公分的距離信號。 VHDL數位電路實習與專題設計

  30. 相關知識 • 紅外線距離感測器 紅外線距離感測器之腳位表 紅外線距離感測器之規格表 VHDL數位電路實習與專題設計

  31. 相關知識 • 紅外線距離感測器量測示意圖 • GP2D12內部架構圖 VHDL數位電路實習與專題設計

  32. 相關知識 • DIRRS+有以下三種使用模式 • 模式一:Serial HEX (預設模式) • 偵測到的距離信號會由Pin 4傳送8位元的串列資料,資料傳送的格式就是本單元所學的RS-232,但是邏輯準位為TTL規格的5V邏輯準位並非是RS-232的±12V,資料傳送格式為4800鮑率、8 bits資料位元、0 bit同位元、1 bit停止位元。所傳送的資料若為”11111111”則代表10公分的距離信號,若為”00000000”則代表80公分的距離信號。 • 模式二:Original DIRRS (J1 Shorted, J2 Open) • 此模式為同步串列傳輸模式,必須要以Vin給予脈衝信號,而Vout為距離的串列資料輸出。資料傳送的格式如下圖所示,必須要注意的是Vin的電壓輸入最高只能為3V。 VHDL數位電路實習與專題設計

  33. 相關知識 • 模式三:Serial CM (J1 Open, J2 Shorted) • 偵測到的距離信號會由Pin 4傳送8位元的串列資料,資料傳送的格式與Serial HEX模式相同為4800鮑率、8 bits資料位元、0 bit同位元、1 bit停止位元。但是所傳送的資料以3 bytes為一筆完整的距離信號,而且要以ASCII碼來看,若所傳送的資料為1, 0, 0則代表10公分的距離信號,若為8, 0, 0則代表80公分的距離信號。 Original DIRRS模式的資料傳輸格式 VHDL數位電路實習與專題設計

  34. 實驗功能 • 將DIRRS+紅外線距離感測器設定為Serial HEX模式,結合鮑率產生器與串列傳輸接收器的程式碼,完成一個DIRRS+的距離信號擷取模組,並將擷取到的距離信號顯示在LED上。 VHDL數位電路實習與專題設計

  35. 程式與說明 • 此處我們以Graphic Editor的方式插入鮑率產生器模組BR_GEN.sym與串列傳輸接收器UART_RECEIVER.sym元件,並完成電路圖如下(uart_DIRRS.gdf)。因為我們必須將鮑率產生器設定為產生4800鮑率,所以將SEL信號設定為”011”。 主程式電路架構圖(uart_DIRRS.gdf) VHDL數位電路實習與專題設計

  36. CPLD下載驗證 • 紅外線距離感測器實習腳位配置表 VHDL數位電路實習與專題設計

  37. CPLD下載驗證 紅外線距離感測器實體測試I 紅外線距離感測器實體測試II VHDL數位電路實習與專題設計

  38. 練習 • 請設計出可將DIRRS+所感測到的距離信號顯示在七段顯示器上的電路。 • 請設計出DIRRS+在Original DIRRS模式下的信號擷取電路,並將擷取到的信號顯示在LED燈。 • 請設計出DIRRS+在Serial CM模式下的信號擷取電路,並將擷取到的信號顯示在七段顯示器。 VHDL數位電路實習與專題設計

  39. 單元12-4 RS-232串列傳輸發送器實習I • 實驗目的 • 了解正確傳送串列傳輸資料的方法。 • 了解串列傳輸傳送器之實現方法。 VHDL數位電路實習與專題設計

  40. 相關知識 • 串列傳輸傳送器功能方塊圖 VHDL數位電路實習與專題設計

  41. 串列傳輸傳送器信號說明表 VHDL數位電路實習與專題設計

  42. 相關知識 • 我們以8個資料位元(data)、0個同位元(parity bit)、1個停止位元(stop bit)的串列傳輸傳送器為例,說明設計方法的步驟如下。 • 判斷txd_startH的電位,若被通知有資料要傳送(txd_startH=’1’)則將匯流排的資料載入傳送資料暫存器中,並且進入狀態2準備傳送起始位元,若沒有資料要傳送,則繼續等待。 • 當傳送頻率(bclk)正緣觸發時,傳送一個低電位的起始位元,以做同步的動作,並且進入狀態3準備傳送資料。 • 每當傳送頻率(bclk)正緣觸發時,就將傳送資料暫存器的低位元傳送出去,並且將傳送資料暫存器的資料往右移一位,最高位元補’1’,當傳送9位元的資料(8 data bits + 1 stop bit)後就致能傳送完畢通知信號,回到狀態1準備傳送下一筆資料。 VHDL數位電路實習與專題設計

  43. 串列傳輸傳送器SM圖 VHDL數位電路實習與專題設計

  44. 實驗功能 • 將並列的匯流排資料DBUS以RS-232介面的txd串列方式傳送 VHDL數位電路實習與專題設計

  45. 1 ------------------------------------------------------------------- 2 --實驗名稱:RS-232實習 3 --檔案名稱:uart_transmitter.vhd 4 --功 能:串列傳輸;1 start bit, 8 data bits, 1 stop bit 5 --日 期:2003.8.8 6 ------------------------------------------------------------------- 7 library ieee; 8 use ieee.std_logic_1164.all; 10 entity uart_transmitter is 11 port( 12 sysclk : in std_logic; 13 rst_n : in std_logic; 14 bclk : in std_logic; 15 txd_startH : in std_logic; 16 DBUS : in std_logic_vector(7 downto 0); 17 txd_doneH : out std_logic; 18 txd : out std_logic 19 ); 20 end uart_transmitter; 22 architecture arch of uart_transmitter is 23 type statetype is (idle, synch, tdata); 24 signal state, nextstate : statetype; 25 signal tsr:std_logic_vector (8 downto 0); 26 signal bct: integer range 0 to 9; 27 signal inc, clr, loadTSR, shftTSR, start: std_logic; 28 signal bclk_rising, bclk_dlayed, txd_done: std_logic; 29 begin 30 txd <= tsr(0); 31 txd_doneH <= txd_done; 32 bclk_rising <= bclk and (not bclk_dlayed); 33 process(state, txd_startH, bct, bclk_rising) 34 begin 35 inc <= '0'; 36 clr <= '0'; 37 loadTSR <= '0'; 38 shftTSR <= '0'; 39 start <= '0'; 40 txd_done <= '0'; 41 case state is 42 ----- idle state; wait until txd_startH = '1' ----- 43 when idle => 44 if (txd_startH = '1' ) then 45 loadTSR <= '1'; 46 nextstate <= synch; 47 else 48 nextstate <= idle; 49 end if; 串列傳輸傳送器程式碼 VHDL數位電路實習與專題設計

  46. 50 ----- synch state ----- 51 when synch => 52 if (bclk_rising = '1') then 53 start <= '1'; 54 nextstate <= tdata; 55 else 56 nextstate <= synch; 57 end if; 58 ----- transmit data state ----- 59 when tdata => 60 if (bclk_rising = '0') then 61 nextstate <= tdata; 62 elsif (bct /= 9) then 63 shfttsr <= '1'; 64 inc <= '1'; 65 nextstate <= tdata; 66 else 67 clr <= '1'; 68 txd_done <= '1'; 69 nextstate <= idle; 70 end if; 71 end case; 72 end process; 73 ----- update data ----- 74 process (sysclk, rst_n) 75 begin 76 if (rst_n = '0') then 77 tsr <= "111111111"; 78 state <= idle; 79 bct <= 0; 80 bclk_dlayed <= '0'; 81 elsif (sysclk'event and sysclk = '1') then 82 state <= nextstate; 83 --counter of transmit bit 84 if (clr = '1') then 85 bct <= 0; 86 elsif (inc = '1') then 87 bct <= bct + 1; 88 end if; 89 --TSR assignment 90 if (loadtsr = '1') then 91 TSR <= DBUS & '1'; 92 elsif (start = '1') then 93 TSR(0) <= '0'; 94 elsif (shftTSR = '1') then 95 TSR <= '1' & TSR(8 downto 1); 96 end if; 97 -- Bclk delayed by 1 sysclk 98 bclk_dlayed <= bclk; 99 end if; 100 end process; 101 end arch; 串列傳輸傳送器程式碼 VHDL數位電路實習與專題設計

  47. 0 0 0 0 start bit 1 1 1 1 stop bit 功能模擬 • 我們模擬傳送0xA5的並列資料,在下面的模擬圖中我們可以發現,當txd_startH為高電位後,state會進入下一個狀態,而且將DBUS & ‘1’的資料載入TSR中(101001011, 0x14B),等待到bclk正緣觸發時,由低電位的起始位元開始傳送,接著依序由從DBUS的低位元到高位元傳送出去,最後一個位元是高電位的結束位元,當資料傳送完畢後,txd_doneH信號也有一個脈衝信號告知已經傳送完畢。 VHDL數位電路實習與專題設計

  48. 練習 • 請設計出可選擇資料位元數的串列傳輸傳送器,資料位元數可為5, 6, 7, 或8位元。 • 請設計出可選擇停止位元數的串列傳輸傳送器,停止位元數可為1, 1.5, 或2位元。 • 請設計出具有同位元功能的串列傳輸傳送器,而且可選擇偶同位元或是奇同位元。 VHDL數位電路實習與專題設計

  49. 單元12-5 RS-232串列傳輸發送器實習II CMOS感測器之影像資料擷取與傳送實習 • 實驗目的 • 了解CMOS感測器之影像資料擷取。 • 將串列傳輸發送器應用於CMOS感測器之影像資料傳送。 VHDL數位電路實習與專題設計

  50. 相關知識 • EZ Vision簡介 • 灰階影像資料:8位元解析度。 • 四種影像大小可選擇:Row × Column = 32×32、24×24、20×20、16×16。 • 兩種影像資料讀取介面可選擇:RS-232串列資料介面、Printer Port並列資料介面。 • 64個曝光時間可選擇:EO7~EO2,6個位元解析度。 • 每秒鐘可擷取畫面可達100F/S~200F/S以上。 • 具有紅外線LED加強光源。 • 電路板上含EM78156微控制器:可與其他單晶片或CPLD連接處理。 • 工作電壓:3.3V。 • 可結合RF模組進行無線影像傳輸。 VHDL數位電路實習與專題設計

More Related