UART - 為 UART 通信設置回調函數

材料準備

  • AmebaD [AMB21 / AMB22 / AMB23 / BW16] x 1

  • USB-TTL 適配器 x 1

範例説明

這個例子展示瞭如何為UART通信設置一個回調函數來處理UART數據。

本例需要一個USB到TTL的適配器。確保已安裝驅動程序並如圖將其連接到Ameba板上。

AMB21 / AMB22 接線圖:

../../../../_images/image177.png

AMB23 接線圖:

../../../../_images/image1-16.png

BW16 接線圖:

../../../../_images/image1-38.png

BW16-TypeC Wiring Diagram:

../../../../_images/image1-43.png

Open the example in “File” “Examples” “AmebaSoftwareSerial” “SoftwareSerial_Irq_Callback”

../../../../_images/image272.png

Upload the code and press the reset button on Ameba once the upload is finished. Next, using a terminal program, such as TeraTerm or PuTTY, open a serial port and configure it according to the settings. Make sure the serial port number corresponds to the USB to TTL adapter.

  • Speed: 38400

  • Data: 8 bit

  • Parity: none

  • Stop bits: 1 bit

  • Flow control: none

../../../../_images/image348.png

當串行口打開後,在串口終端中輸入並按回車鍵,就會看到相應的輸出。

../../../../_images/image436.png

程式碼説明

mySerial.setAvailableCallback (mySerialCallback); 用於將函數mySerialCallback設置為軟件串行的回調函數。當接收到新字符時,回調函數檢查該字符是否與enter鍵相對應,如果返回值為真,則釋放信號標,從而允許主循環打印出之前接收到的所有字符。