ESP8266 基于arduino 使用UART1
UART1 只能发送数据,UART1 TX1<---->GPIO2<----->D4
官方说明:
Serial
Serial object works much the same way as on a regular Arduino. Apart from hardware FIFO (128 bytes for TX and RX) Serial has additional 256-byte TX and RX buffers. Both transmit and receive is interrupt-driven. Write and read functions only block the sketch execution when the respective FIFO/buffers are full/empty. Note that the length of additional 256-bit buffer can be customized.
Serial uses UART0, which is mapped to pins GPIO1 (TX) and GPIO3 (RX). Serial may be remapped to GPIO15 (TX) and GPIO13 (RX) by calling Serial.swap() after Serial.begin. Calling swap again maps UART0 back to GPIO1 and GPIO3.
Serial1 uses UART1, TX pin is GPIO2. UART1 can not be used to receive data because normally it’s RX pin is occupied for flash chip connection. To use Serial1, call Serial1.begin(baudrate).
If Serial1 is not used and Serial is not swapped - TX for UART0 can be mapped to GPIO2 instead by calling Serial.set_tx(2) after Serial.begin or directly with Serial.begin(baud, config, mode, 2).
By default the diagnostic output from WiFi libraries is disabled when you call Serial.begin. To enable debug output again, call Serial.setDebugOutput(true). To redirect debug output to Serial1 instead, call Serial1.setDebugOutput(true).
You also need to use Serial.setDebugOutput(true) to enable output from printf() function.
The method Serial.setRxBufferSize(size_t size) allows to define the receiving buffer depth. The default value is 256.
Both Serial and Serial1 objects support 5, 6, 7, 8 data bits, odd (O), even (E), and no (N) parity, and 1 or 2 stop bits. To set the desired mode, call Serial.begin(baudrate, SERIAL_8N1), Serial.begin(baudrate, SERIAL_6E2), etc.
A new method has been implemented on both Serial and Serial1 to get current baud rate setting. To get the current baud rate, call Serial.baudRate(), Serial1.baudRate(). Return a int of current speed. For example
// Set Baud rate to 57600
Serial.begin(57600);
// Get current baud rate
int br = Serial.baudRate();
// Will print "Serial is 57600 bps"
Serial.printf("Serial is %d bps", br);
Serial and Serial1 objects are both instances of the HardwareSerial class.
I’ve done this also for official ESP8266 Software Serial library, see this pull request.
Note that this implementation is only for ESP8266 based boards, and will not works with other Arduino boards.
To detect an unknown baudrate of data coming into Serial use Serial.detectBaudrate(time_t timeoutMillis). This method tries to detect the baudrate for a maximum of timeoutMillis ms. It returns zero if no baudrate was detected, or the detected baudrate otherwise. The detectBaudrate() function may be called before Serial.begin() is called, because it does not need the receive buffer nor the SerialConfig parameters.
The uart can not detect other parameters like number of start- or stopbits, number of data bits or parity.
The detection itself does not change the baudrate, after detection it should be set as usual using Serial.begin(detectedBaudrate).
Detection is very fast, it takes only a few incoming bytes.
SerialDetectBaudrate.ino is a full example of usage.
具体使用参见例程:
/*
* ESP8266使用串口1例程,UART1仅能发送
* UART1 TX1<---->GPIO2<----->D4
*/
void setup() {
// put your setup code here, to run once:
Serial1.begin(74880);
}
void loop() {
// put your main code here, to run repeatedly:
Serial1.print("ESP8266 UART1!\n");
delay(1000);
}


本文详细介绍了ESP8266如何通过UART1进行数据发送,并提到了Serial1的使用方法,包括设置波特率、缓冲区大小,以及如何利用Serial.detectBaudrate()进行数据速率检测。此外,还涉及了如何配置硬件和软件串口,以及调试技巧。
4421

被折叠的 条评论
为什么被折叠?



