SoftwareSerial verursacht Probleme - Seeeduino Lite = Leonardo

Hallo

Ich habe zu einem funktionierenden Code den SoftwareSerial den ich für das GPS Modul benötige hinzugefügt aber es scheint dass dadurch irgendwie die Timer oder der Ram in mitleidenschaft gezogen wird... Und das nur durch das hinzufügen von
mySerial.begin(9600);
Jedenfalls funktioniert dann nix mehr, wobei das Timing eine grosse rolle spielt...

hier der Code - bitte nicht haun, ist ein wenig chaotisch..

#define wb_analog 0
#define log_enable 5  //Switch to ground to enable logging
#define LED 13
#define chipSelect 6


////////////////////// K Options /////////////////////////
#define Serial1_baud 10400
#define TX 1

const uint16_t t_01 = 6000;       // delay bevore starting
const uint16_t t_02 = 25;         // fast init delay
const uint16_t t_03 = 150;        // timeout time
const uint16_t t_04 = 43;         // read write delay

uint32_t time, tr_1, tr_2, tr_3;
uint8_t mode, outCntr, inCntr, inByte, chksm, lngt;
boolean started_;

byte K_START_COM[5] = {
  0x81, 0x12, 0xF1, 0x81, 0x05
};
byte K_READ_ALL_SENS[7] = {
  0x80, 0x12, 0xF1, 0x02, 0x21, 0x08, 0xAE
};

//byte K_BUFFER[0xFF];
String K_BUFFER, PRINT_BUFFER, GPS_BUFFER, NMEA;
/////////////////////////////////////////////////////////


#include <SdFat.h>
#include <SoftwareSerial.h>

SdFat sd;
SdFile myFile;
SoftwareSerial gps(8, 7);




////////////////// Global Variables ///////////////////////
boolean logging;










void setup() {
  Serial.begin(115200);                      // Start Serial Comunication
  //  gps.begin(9600);
  delay(400);                                // catch Due reset problem

  pinMode(TX, OUTPUT);                       // Set TX to Output - Needed for K
  pinMode(LED, OUTPUT);                      // Set LED to Output



  /////////////// LOGING OPTIONS ///////////////////////

  pinMode(log_enable, INPUT_PULLUP);
  if (!sd.begin(chipSelect, SPI_FULL_SPEED)) {
    Serial.println("No SD Card available!!");
    logging = false;
  }
  else {
    Serial.println("SD Card available!!");
    logging = true;
  }

  /////////////// GPS OPTIONS ///////////////////////
  //  gps.write("$PMTK251,57600*2C\r\n");
  //  gps.end();
  //  gps.begin(57600); // Reconnect at new speed
  //  delay(1000);  // Delay to let change take effect
  //  gps.write("$PMTK314,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*29\r\n");    // RMC
  //  gps.write("$PMTK220,100*2F\r\n");                                      // update 10HZ
  //  gps.write("$PGCMD,33,0*6D\r\n");                                       // DISABLE ANTENNA OUTPUT
}



void loop() {

  if (Serial.available()) {                    // Reset K_ only for debugg
    Serial.read();
    started_ = false;
  }


  if (mode == 0) {                              // Start Sequence
    if (k_transmit(K_START_COM, 5)) {
      switchLed();                              // toggle LED
      Serial.println(K_BUFFER);
      K_BUFFER = "";
      mode++;
    }
  }

  if (mode == 1) {                                // Run over Sentence Request
    if (k_transmit(K_READ_ALL_SENS, 7)) {
      switchLed();                                // toggle LED
      //      Serial.println();
      //      for (int x = 0; x < K_BUFFER_Cntr; x++) {
      //        Serial.print(K_BUFFER[x]);
      //        Serial.print(",");
      //      }
      //      Serial.println();
      time = millis();
      PRINT_BUFFER += time;
      PRINT_BUFFER += ",";
      //      Serial.println(K_BUFFER);
      PRINT_BUFFER += K_BUFFER;
      Serial.println(PRINT_BUFFER);

      if (log_enable == LOW) {                          // write to sd
        writetosd(PRINT_BUFFER);
      }
      PRINT_BUFFER = "";
      K_BUFFER = "";
    }
  }




  //////////////////////////////////// GPS Section //////////////////////////
  //  if (gps.available()) {
  //    char c = gps.read();
  //    GPS_BUFFER += c;
  //    if (c == 36) {
  //      NMEA += GPS_BUFFER;
  //      Serial.println(NMEA);
  //      NMEA = "";
  //      GPS_BUFFER = "";
  //    }
  //  }
}





boolean k_transmit(byte * function, byte num) {
  if (!started_) {
    if (Serial1.available()) {              // Empty Buffer
      Serial1.read();
    }
    Serial1.end();

    time = millis();                        // get Time
    if (!tr_3) {                            // reset Timer
      tr_3 = millis();
    }

    if (tr_3 + t_01 > time) {                // Switch t_01 High
      digitalWrite (TX, HIGH);
    }
    if ((tr_3 + t_01 < time) && (tr_3 + t_01 + t_02 > time)) {                      // Switch t_02 LOW
      digitalWrite (TX, LOW);
    }
    if ((tr_3 + t_01 + t_02 < time) && (tr_3 + t_01 + t_02 + t_02 > time)) {        // Switch t_02 High
      digitalWrite (TX, HIGH);
    }
    if (tr_3 + t_01 + t_02 + t_02 < time) {                                          // Start Serial Comunication
      Serial1.begin(Serial1_baud);
      mode = 0;
      outCntr = 0;
      inCntr = 0;
      inByte = 0;
      chksm = 0;
      lngt = 0;
      tr_1 = 0;
      tr_3 = 0;
      tr_2 = millis();
      started_ = true;
    }
  }
  else {
    time = millis();
    if ((num > outCntr) && (time >= tr_1 + t_04)) {
      Serial1.write(function[outCntr]);
      outCntr++;
    }

    if (Serial1.available()) {
      inByte = Serial1.read();
      inCntr++;



      //      Serial.print(inByte);
      //      Serial.print(",");

      //      if ((inCntr > outCntr + 4) && (inCntr < lngt + 5 + outCntr)) {
      //        K_BUFFER[K_BUFFER_Cntr] = inByte;
      //        K_BUFFER_Cntr++;
      //      }


      K_BUFFER += inByte;                           // Write data to Buffer
      K_BUFFER += ",";                           // Write data to Buffer


      //////////// Calculate Checksum //////////////
      if (inCntr == 4 + outCntr) {
        lngt = inByte;
      }

      if ((inCntr > outCntr) && (inCntr < lngt + 5 + outCntr)) {
        chksm = chksm + inByte;
      }

      if (inCntr == lngt + 5 + outCntr) {
        if (chksm == inByte) {
          //          time = millis();
          //          Serial.print(time - tr_1);
          outCntr = 0;
          inCntr = 0;
          inByte = 0;
          chksm = 0;
          lngt = 0;
          tr_2 = millis();
          tr_1 = millis();
          return 1;
        }
      }
    }
    else {
      time = millis();
      if (time > tr_2 + t_03) {
        Serial.println("TIMEOUT");
        started_ = false;
      }
    }
  }
  return 0;
}

int read_wideband() {
  uint16_t  afr = map(analogRead(wb_analog), 0, 1023, 1000, 1998);
  return afr;
}

void writetosd(String data) {
  if (logging) {
    if (!myFile.open("logfile.csv", O_RDWR | O_CREAT | O_AT_END)) {
      Serial.println("No SD Card available!!");
      logging = false;
    }
    myFile.println(data);
    myFile.close();
  }
}

void switchLed() {
  digitalWrite (LED, !digitalRead(LED));
}

Probier mal AltSoftSerial:
https://www.pjrc.com/teensy/td_libs_AltSoftSerial.html

Das verwendet einen Timer und ist dadurch genauer. Beachte die Pin-Belegung. Durch den Timer verwendet das festgelegte Pins.

Hallo

Danke für die info, habs mir mal angeschaut, das mit der Pinbelegung ist ein ziemliches problem weil ich ein uno shield benutze und deshalb die pins 11-13 nicht aktiv sind, bzw an der rückseite unterbrochen... Gibts ne möglichkeit die Pins in der Library zu ändern?

Wenn Du einen Leonardo-Nachbau hast dann kannst Du das GPS modul auf Pin 0 und 1 anschließen. USB erfolgt über andere Pins.
Grüße Uwe

Hallo

Die Standard Serial Pins verwende ich bereits.
Hab da nen Teil in der library gefunden wo die pins zugewiesen werden
kann ich das einfach ändern? zu 8 und 7?

// Arduino Leonardo & Yun (from Cristian Maglie)
//
#elif defined(ARDUINO_AVR_YUN) || defined(ARDUINO_AVR_LEONARDO) || defined(__AVR_ATmega32U4__)

  //#define ALTSS_USE_TIMER1
  //#define INPUT_CAPTURE_PIN 4  // receive
  //#define OUTPUT_COMPARE_A_PIN 9 // transmit
  //#define OUTPUT_COMPARE_B_PIN 10 // unusable PWM
  //#define OUTPUT_COMPARE_C_PIN 11 // unusable PWM

  #define ALTSS_USE_TIMER3
  #define INPUT_CAPTURE_PIN 13 // receive
  #define OUTPUT_COMPARE_A_PIN 5 // transmit

Nein. Du kannst nur zwischen Timer1 (5 + 13) und Timer3 (4 + 9) wählen. Die Pins sind fest, da die direkt in Hardware geschaltet werden, ohne dass Code ausgeführt wird.

Achso, sind dann die Pins 10 und 11 automatisch auch nicht mehr benutzbar?

Es steht doch dabei. Nur PWM geht dann darauf mehr. Als normale I/Os gehen die natürlich noch.

super jetzt gehts mit 57600!!!
Super Sache..
:grinning: