Go Down

Topic: Arduino DUE Can't reduce SPI ClockSpeed (Read 612 times) previous topic - next topic

gamer1

Hi,
i got the problem taht i cant reduce the clockspeed of my SPI Header on my DUE. Almost the same code on the Leonardo works.
The Clockspeed normal from the Due is 4MHz --> Can't reduced
The Clockspeed normal from the Leonardo is 1,3MHz --> Divided by 128 makes 117.5 kHz



So on the Leonardo it Works but it isn't the expected result from 10kHz. Is this a bug from the Library?

SPI.begin(slaveSelectPin); //DUE
   //  SPI.begin();//Leonardo
SPI.setClockDivider(slaveSelectPin, 40); //DUE
//SPI.setClockDivider(SPI_CLOCK_DIV64); //Leonardo


Code: [Select]
#include <Ethernet2.h>
#include <SPI.h>
#include <Wire.h>
//EInbinden der Biblioteken
const int slaveSelectPin = 10;

byte mac[] = {
  0x90, 0xA2, 0xDA, 0x10, 0x22, 0xEE
};
//Einsetllung MAC adresse
IPAddress ip(172, 17, 17, 210);
IPAddress gateway(172, 17, 17, 1);
IPAddress subnet(255, 255, 255, 0);
// IP Einsellungen

EthernetServer server(80);
//HTTP Portfreigabe

#define Addr 0x27
//Sensor Adresse Setzen


long lastReadingTime = 0;
float humidity =0;
float cTemp =0;

void setup() {
    pinMode (slaveSelectPin, OUTPUT);
Wire.begin();
 SPI.begin(slaveSelectPin); //DUE
   //  SPI.begin();//Leonardo
SPI.setClockDivider(slaveSelectPin, 40); //DUE
//SPI.setClockDivider(SPI_CLOCK_DIV64); //Leonardo
 //SPI.beginTransaction(SPISettings(117000, MSBFIRST, SPI_MODE0));
 
  Serial.begin(9600);
  Serial.print("Angefangen");
  // give the sensor and Ethernet shield time to set up:
delay(1000);
 
  Ethernet.begin(mac, ip);

  server.begin();
//Starte Ethernet und denn Server
 Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}

void loop()
{
  // check for a reading no more than once a second.
  if (millis() - lastReadingTime > 3000)
  {
tmp_lesen ();
lastReadingTime = millis();
    }
    listenForEthernetClients();
}


void tmp_lesen ()
{Serial.println();
  Serial.println("Beginn Temperatur einlesen");
 unsigned int data[4];
  Wire.beginTransmission(Addr);   //Start I²C
   Wire.write(0x00);  //Datenregister 0x00
    Wire.endTransmission();  //Beende Übertragung
     Wire.requestFrom(Addr, 4);  // Request 4 bytes of data
if (Wire.available() == 4)// Read 4 bytes of data
  {  // humidity msb, humidity lsb, temp msb, temp lsb
    data[0] = Wire.read();
    data[1] = Wire.read();
    data[2] = Wire.read();
    data[3] = Wire.read();
  }

  // Convert the data to 14-bits
   humidity = ((((data[0] & 0x3F) * 256) + data[1]) * 100.0) / 16383.0;
  int temp = ((data[2] * 256) + (data[3] & 0xFC)) / 4;
 cTemp = (temp / 16384.0) * 165.0 - 40.0;  //Umrechen in grad C
Serial.println("Humidity:");
Serial.println(humidity);
Serial.println("Temperatur:");
Serial.println(cTemp);
}


  void listenForEthernetClients() {
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
 
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
         
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
       
          client.print("Temperatur: ");
          client.print(cTemp);
          client.print(" degrees C");
          client.println("");
          client.print("Feuchtigkeit: ");
          client.print(humidity);
          client.print(" %RH ");
         
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        }
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop(); 
  }
}
welcome to the desert of the real

MorganS

The one you noted for the Leonardo should work. The setClockDivider() method is still in the Due's SPI library although it should no longer be used for new programs.

The one with the hardcoded "40" probably won't work, particularly since my copy of the Due SPI library shows that 42 is the nearest valid number. Try not to use those numbers directly. Use the constants defined in SPI.h

Try to write your program to use the newer system of SPI settings:
Code: [Select]
SPI.beginTransaction(SPISettings(2000000, MSBFIRST, SPI_MODE0));
"The problem is in the code you didn't post."

gamer1

I tried it but i hadn't a well formed square wave signal like on my leonardo
welcome to the desert of the real

Go Up