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
#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();
}
}