Arduino Nano IoT 上的串行通信 33

Serial Communication on Arduino Nano IoT 33

自从从 Uno 移动到 Nano 33 IoT 单元后,我遇到了麻烦。 我已经阅读了很多关于这个特定主题的信息,但我仍然很困惑,因为我很习惯使用 Nano 33 上不可用的软件序列。 根据: https://github.com/ostaquet/Arduino-Nano-33-IoT-Ultimate-Guide Guide using an addition port for serial as I'm all ready using Serial1 TX Pin 0, and RX Pin 1 my Sigfox unit: https://yadom.fr/carte-breakout-sfm10r1.html 工作正常。问题出在我的GPS上。如果我交换 GPS 周围的 2 工作正常而 Sigfox 装置不...

#include <TinyGPS++.h>
#include <Arduino.h>
#include "wiring_private.h"
static const uint32_t GPSBaud = 9600;
#define DEBUG 1
TinyGPSPlus gps;
uint8_t msg[12];

Uart mySerial (&sercom0, 5, 6, SERCOM_RX_PAD_1, UART_TX_PAD_2);

// Attach the interrupt handler to the SERCOM
void SERCOM0_Handler()
{
    mySerial.IrqHandler();
}


void setup() {
  pinMode(LED_BUILTIN, OUTPUT);      // set LED pin as output
  digitalWrite(LED_BUILTIN, LOW);    // switch off LED pin
  pinPeripheral(5, PIO_SERCOM);
  pinPeripheral(6, PIO_SERCOM);

   if(DEBUG){
    Serial.begin(9600);
  }
  
  mySerial.begin(GPSBaud);
  Serial1.begin(9600);
  delay(100);
  getID();
  delay(100);
  getPAC();
}

void loop()
{
  msg[0]=0xC0;
  msg[1]=0xFF;
  msg[2]=0xEE;

  sendMessage(msg, 3);
  
  printInt(gps.satellites.value(), gps.satellites.isValid(), 5);
  printFloat(gps.hdop.hdop(), gps.hdop.isValid(), 6, 1);
  printFloat(gps.location.lat(), gps.location.isValid(), 11, 6);
  printFloat(gps.location.lng(), gps.location.isValid(), 12, 6);
  printInt(gps.location.age(), gps.location.isValid(), 5);
  printDateTime(gps.date, gps.time);
  printFloat(gps.altitude.meters(), gps.altitude.isValid(), 7, 2);
  printFloat(gps.course.deg(), gps.course.isValid(), 7, 2);
  printFloat(gps.speed.kmph(), gps.speed.isValid(), 6, 2);
  printStr(gps.course.isValid() ? TinyGPSPlus::cardinal(gps.course.deg()) : "*** ", 6);

  printInt(gps.charsProcessed(), true, 6);
  printInt(gps.sentencesWithFix(), true, 10);
  printInt(gps.failedChecksum(), true, 9);
  Serial.println();

    delay(120000);

  if (millis() > 5000 && gps.charsProcessed() < 10)
    Serial.println(F("No GPS data received: check wiring"));
}

void blink(){
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);    
}

//Get Sigfox ID
String getID(){
  String id = "";
  char output;

  Serial1.print("AT$I=10\r");
  while (!Serial1.available()){
     blink();
  }

  while(Serial1.available()){
    output = Serial1.read();
    id += output;
    delay(10);
  }

  if(DEBUG){
    Serial.println("Sigfox Device ID: ");
    Serial.println(id);
  }

  return id;
}

//Get PAC number
String getPAC(){
  String pac = "";
  char output;

  Serial1.print("AT$I=11\r");
  while (!Serial1.available()){
     blink();
  }

  while(Serial1.available()){
    output = Serial1.read();
    pac += output;
    delay(10);
  }

  if(DEBUG){
    Serial.println("PAC number: ");
    Serial.println(pac);
  }

  return pac;
}

//Send Sigfox Message
void sendMessage(uint8_t msg[], int size)
{
  Serial.println("Sigfox Start");

  String status = "";
  String hexChar = "";
  String sigfoxCommand = "";
  char output;

  sigfoxCommand += "AT$SF=";

  for (int i = 0; i < size; i++)
  {
    hexChar = String(msg[i], HEX);

    //padding
    if (hexChar.length() == 1)
    {
      hexChar = "0" + hexChar;
    }

    sigfoxCommand += hexChar;
  }

  Serial.println("Sending Sigfox Message...");
  Serial.println(sigfoxCommand);
  Serial1.println(sigfoxCommand);

  while (!Serial1.available())
  {
    Serial.println("Waiting for Sigfox Response");
    delay(7000);
  }

  while (Serial1.available())
  {
    output = (char)Serial1.read();
    status += output;
    delay(10);
  }

  Serial.println();
  Serial.print("Sigfox Status \t");
  Serial.println(status);
}


// This custom version of delay() ensures that the gps object
// is being "fed".
static void smartDelay(unsigned long ms)
{
  unsigned long start = millis();
  do 
  {
    while (mySerial.available())
      gps.encode(mySerial.read());
  } while (millis() - start < ms);
}

static void printFloat(float val, bool valid, int len, int prec)
{
  if (!valid)
  {
    while (len-- > 1)
      Serial.print('*');
    Serial.print(' ');
  }
  else
  {
    Serial.print(val, prec);
    int vi = abs((int)val);
    int flen = prec + (val < 0.0 ? 2 : 1); // . and -
    flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
    for (int i=flen; i<len; ++i)
      Serial.print(' ');
  }
  smartDelay(0);
}

static void printInt(unsigned long val, bool valid, int len)
{
  char sz[32] = "*****************";
  if (valid)
    sprintf(sz, "%ld", val);
  sz[len] = 0;
  for (int i=strlen(sz); i<len; ++i)
    sz[i] = ' ';
  if (len > 0) 
    sz[len-1] = ' ';
  Serial.print(sz);
  smartDelay(0);
}

static void printDateTime(TinyGPSDate &d, TinyGPSTime &t)
{
  if (!d.isValid())
  {
    Serial.print(F("********** "));
  }
  else
  {
    char sz[32];
    sprintf(sz, "%02d/%02d/%02d ", d.month(), d.day(), d.year());
    Serial.print(sz);
  }
  
  if (!t.isValid())
  {
    Serial.print(F("******** "));
  }
  else
  {
    char sz[32];
    sprintf(sz, "%02d:%02d:%02d ", t.hour(), t.minute(), t.second());
    Serial.print(sz);
  }

  printInt(d.age(), d.isValid(), 5);
  smartDelay(0);
}

static void printStr(const char *str, int len)
{
  int slen = strlen(str);
  for (int i=0; i<len; ++i)
    Serial.print(i<slen ? str[i] : ' ');
  smartDelay(0);
}

因此,我的问题是通过此部分创建的附加端口:

#include <Arduino.h>
#include "wiring_private.h"

Uart mySerial (&sercom0, 5, 6, SERCOM_RX_PAD_1, UART_TX_PAD_0);

// Attach the interrupt handler to the SERCOM
void SERCOM0_Handler()
{
    mySerial.IrqHandler();
}

void setup() {
  // Reassign pins 5 and 6 to SERCOM alt
  pinPeripheral(5, PIO_SERCOM_ALT);
  pinPeripheral(6, PIO_SERCOM_ALT);

  // Start my new hardware serial
  mySerial.begin(9600);
}

void loop() {
  // Do something with mySerial...
}

在 Nano 上,我的 TX 位于 Nano Pin 6(从 开始),RX 位于 Pin 7“6”,因为它从 0 开始....

为了使 RX 和 TX 在电路板上相互通信而进行了交换,其中 GPS 上的 RX = Nano 上的 TX,GPS 上的 TX = Nano 上的 RX。

我是不是用错了引脚?因为 GPS 根本不想用 pin 5 和 6 来响应...

gps 上的灯闪烁表示有电,如果我将 Gps 与 Sigfox 装置互换,gps 工作,而 Sigfox 装置则不工作。

我在纳米板上使用了错误的针脚吗?请帮助....

使用上面更新的代码处理 Nano 33 IoT 的 Nano Pin D5 和 D6。感谢 Juraj :-)

带引脚 5 和 6 的 SERCOM0 是 Nano 33 IoT 上附加串行接口的不错选择。 variant.cpp 文件有

 +------------+------------------+--------+---------+---------+
 | Pin number | NANO  Board pin  |  PIN   | SERCOMx | SERCOMx |
 |            |                  |        | (x/PAD) | (x/PAD) |
 +------------+------------------+--------+---------+---------+
 | 5          | ~D5              |  PA05  |         |   0/01  |
 | 6          | ~D6              |  PA04  |         |   0/00  |

所以实例化应该是

  Uart mySerial (&sercom0, 5, 6, SERCOM_RX_PAD_1, UART_TX_PAD_0);

你有UART_TX_PAD_2

引脚 5 为 RX,引脚 6 为 TX。


pinPeripheral(5, PIO_SERCOM_ALT);
pinPeripheral(6, PIO_SERCOM_ALT);

命令应该在 mySerial.begin 之后,因为 begin 确实

pinPeripheral(uc_pinRX, g_APinDescription[uc_pinRX].ulPinType);

.ulPinType 不是 PIO_SERCOM_ALT