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键相对应,如果返回值为真,则释放信号标,从而允许主循环打印出之前接收到的所有字符。