【得捷电子Follow me第4期】基础任务二(补充):RA-08H板子做显示
[复制链接]
我是申请了第二块板子,作为开发用的,有些可能买了模块,我觉得模块没啥性价比,选开发板,然后通过串口接受数据,进行显示。
RA-08H也是树莓派RP2040的芯片,驱动了一个1.8寸的显示屏幕。所以从开发上来说,和W5500-PICO是一样的。
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library for ST7735
#include <SPI.h>
// #include "SoftwareSerial.h"
// SoftwareSerial uart1(8,9);//串口定义
#include <SerialPIO.h>
#define txpin 0
#define rxpin 1
#define fifosize 32
SerialPIO mySerial(0, 1); // 创建SerialPIO对象
#define TFT_CS 17
#define TFT_RST 22 // Or set to -1 and connect to Arduino RESET pin
#define TFT_DC 16
#define TFT_MOSI 19 // Data out
#define TFT_SCLK 18 // Clock out
#define TFT_BL 23
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);
void setup() {
Serial.begin(9600);
// uart1.begin(115200);
Serial.print(F("Hello! ST77xx TFT Test"));
pinMode(TFT_BL, OUTPUT);
// put your setup code here, to run once:
// OR use this initializer if using a 1.8" TFT screen with offset such as WaveShare:
tft.initR(INITR_BLACKTAB); // Init ST7735S chip, green tab
digitalWrite(TFT_BL, HIGH);
mySerial.begin(115200);
tft.setTextWrap(false);
tft.fillScreen(ST77XX_BLACK);
}
String receivedData; // 存储接收到的字符流
void loop() {
// put your main code here, to run repeatedly:
// tft.setTextWrap(false);
// tft.fillScreen(ST77XX_BLACK);
static unsigned long startTime = millis(); // 记录起始时间
while (mySerial.available()) { // 检查是否有可用数据
char data = mySerial.read(); // 读取串口接收到的数据
receivedData += data; // 将数据添加到接收到的字符流中
startTime = millis(); // 重置起始时间
}
// 检查超时
if (millis() - startTime >= 2000) {
if (receivedData.length() > 0) {
// Serial.print("Received: "); // 通过USB CDC打印接收到的数据
// Serial.println(receivedData);
tft.setCursor(0, 30);
tft.setTextColor(ST77XX_RED);
tft.setTextWrap(false);
tft.fillScreen(ST77XX_BLACK);
tft.setTextSize(1);
tft.println(receivedData);
receivedData = ""; // 清空接收到的字符流
}
startTime = millis(); // 重置起始时间
}
}
我们先做好这边的驱动。
红色框是串口0,我们通过初始化,完成其接收数据功能,然后加入屏幕驱动,接收到的数据让其显示到屏幕上。
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <SerialPIO.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 2, 177);
unsigned int localPort = 8888; // local port to listen on
// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; // buffer to hold incoming packet,
char ReplyBuffer[] = "acknowledged"; // a string to send back
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
#define txpin 0
#define rxpin 1
#define fifosize 32
SerialPIO mySerial(0, 1); // 创建SerialPIO对象
void setup() {
// You can use Ethernet.init(pin) to configure the CS pin
//Ethernet.init(10); // Most Arduino shields
//Ethernet.init(5); // MKR ETH shield
//Ethernet.init(0); // Teensy 2.0
//Ethernet.init(20); // Teensy++ 2.0
//Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet
//Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet
Ethernet.init(17); // WIZnet W5100S-EVB-Pico W5500-EVB-Pico W6100-EVB-Pico
// start the Ethernet
Ethernet.begin(mac, ip);
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
// start UDP
Udp.begin(localPort);
mySerial.begin(115200);
}
String SenddData; // 存储接收到的字符流
void loop() {
// if there's data available, read a packet
int packetSize = Udp.parsePacket();
if (packetSize) {
Serial.print("Received packet of size ");
SenddData += "Received size:";
Serial.println(packetSize);
SenddData += packetSize;
SenddData += "\r\n";
Serial.print("From ");
SenddData += "From:";
IPAddress remote = Udp.remoteIP();
for (int i=0; i < 4; i++) {
Serial.print(remote[i], DEC);
SenddData += remote[i];
if (i < 3) {
Serial.print(".");
SenddData += ".";
}
}
Serial.print(", port ");
SenddData += ", \r\nport ";
SenddData += "\r\n";
Serial.println(Udp.remotePort());
SenddData += Udp.remotePort();
SenddData += "\r\n";
// read the packet into packetBufffer
Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
Serial.println("Contents:");
Serial.println(packetBuffer);
SenddData += "Contents:\r\n";
SenddData += packetBuffer;
mySerial.println(SenddData);
SenddData="";
// send a reply to the IP address and port that sent us the packet we received
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(ReplyBuffer);
Udp.endPacket();
}
delay(10);
}
同样修改w5500这边的代码,这边代码主要就是添加了串口0初始化,以及串口数据的一个整理发送。
完成后效果就是他可以接受到UDP数据,然后通过串口发送,内容是UDP发送端的信息,IP和端口,然后发送的内容,发送内容中间加了那个换行有点问题,显示的。
从这边接受到显示的内容其实就是乱码的。所以屏幕上显示的也是乱码的。
除了这个功能,也可以直接用串口发送数据显示。也算便携串口显示了,没电脑的时候可以看下,不过目前没有支持中文显示。
|