SPI RTC met Arduino Ethernet

Heey,

Ik heb een Arduino Ethernet, het is de bedoeling dat deze door middel van een Real Time Clock ( https://www.sparkfun.com/products/10160 ) nog steeds kan werken zonder internet. (Uiteindelijk moet het een kooikarper voeding systeem worden...) Het internet gedeelte werkt goed; het Real time Clock gedeelte werkt goed, maar bij het combineren werkt het internet niet meer, hij blijft dan een oneindige loop ´stilstaan´. Zijn er mensen met ervaring met dit probleem, volgensmij is de ethernet library een beetje log ofzo, waardoor als je met pin 10 gaat spelen hij ermee kapt.

Mijn vraag is dus of wanneer je het internet gebruikt (zegmaar naar Ethernet.begin() ) je pin 10 hoog kan maken, om dan de RTC te lezen/schrijven, zonder dat de library vastloopt.

Het probleem zit hem waarschijnlijk in RTC_init()
Dit is de orginele code:

int RTC_init(){ 
	  pinMode(spiRtc,OUTPUT); // chip select
          pinMode(spiEthernet, OUTPUT); // start the SPI library:
	  SPI.begin();
	  SPI.setBitOrder(MSBFIRST); 
	  SPI.setDataMode(SPI_MODE1); // both mode 1 & 3 should work 
	  //set control register 
	  digitalWrite(spiRtc, LOW);  
	  digitalWrite(spiEthernet, HIGH);  
	  SPI.transfer(0x8E);
	  SPI.transfer(0x60); //60= disable Osciallator and Battery SQ wave @1hz, temp compensation, Alarms disabled
	  digitalWrite(spiRtc, HIGH);
          digitalWrite(spiEthernet, LOW);
	  delay(10);
}

Met de onderstaande code werkt het internet wel; SPI_MODE0 schijnt te werken met Ethernet.

int RTC_init(){ 
	  pinMode(spiRtc,OUTPUT); // chip select
          pinMode(spiEthernet, OUTPUT); // start the SPI library:
	  SPI.begin();
	  SPI.setBitOrder(MSBFIRST); 
	  SPI.setDataMode(SPI_MODE0); // both mode 1 & 3 should work 
	  //set control register 
	  digitalWrite(spiRtc, LOW);  
	  digitalWrite(spiEthernet, HIGH);  
	  SPI.transfer(0x8E);
	  SPI.transfer(0x60); //60= disable Osciallator and Battery SQ wave @1hz, temp compensation, Alarms disabled
	  digitalWrite(spiRtc, HIGH);
          digitalWrite(spiEthernet, LOW);
	  delay(10);
}

lemio
Wekom op het nederlandse forum.
Je kan de pin 10 niet gebruiken als je ethernet kaart wil gebruiken.
Op http://arduino.cc/en/Main/ArduinoEthernetShield kan je lezen

Arduino communicates with both the W5100 and SD card using the SPI bus (through the ICSP header). This is on digital pins 11, 12, and 13 on the Duemilanove and pins 50, 51, and 52 on the Mega. On both boards, pin 10 is used to select the W5100 and pin 4 for the SD card. These pins cannot be used for general i/o.

Kan je geen andere pin gebruiken voor de rtc?
Met vriendelijke groet
Jantje

Ik móet pin 10 (Slave Select van het shield) juist gebruiken om te switchen tussen het internet shield en de RTC (pin 8). Alleen doordat het Ethernet shield waarschijnlijk een andere SPI mode heeft als de Real Time Clock, kan SPI niet functioneren. Is er een mogelijkheid om wél te kunnen switchen tussen die verschillende modes?.

Ik móet pin 10 (Slave Select van het shield) juist gebruiken om te switchen tussen het internet shield en de RTC

Wel ik vrees dat je geluk een beetje op is, want dat is bij mijn weten niet mogelijk (maar ik altijd verkeer zijn). Dit wil echter niet zeggen dat je je project nu niet vervoledigen. Er zijn 2 mogelijkheden:

1 je neemt een andere RTC die werkt met een I2C verbinding zoals deze: SparkFun Real Time Clock Module - BOB-12708 - SparkFun Electronics (wat in mijn eigen eigenlijk handiger is)
2 je hangt je klok aan een BareBoneArduino via SPI en laat de BBA communiceren over I2C met je andere Arduino:
BBA : http://www.iprototype.nl/products/kits/barebone-arduino-kit
howto BBA: http://www.jo3ri.be/a/jo3ri.be/jo3ri/arduino/projects/barebonearduino-1

Als je een "zogezegde 2de arduino in je project steekt, kan je natuurlijk ook veel meer sensors gaan gebruiken. Je maakt dan van je hoofd Arduino met Ethernet je master die alle data verzamelt en bewaart. De slave arduino dient dan om tijd en sensors te checken.

Ik weet niet welke RTC je hebt maar het lijkt erop dat je een DS32xx (DS3234) of iets dergelijks hebt.

Als dat zo is dan kan deze code je misschien verder helpen;
(Deze code synchroniseerd een DS3234 met een NTP server, je hebt dus naast een Arduino een EthernetShield en een DS3234 nodig. De DS is als volgt aangesloten; SS – pin 8, MOSI – pin 11, MISO – pin12, and SCK – Pin13).

/*
 
 Udp NTP Client
  
 Get the time from a Network Time Protocol (NTP) time server
 Demonstrates use of UDP sendPacket and ReceivePacket 
 For more on NTP time servers and the messages needed to communicate with them, 
 see http://en.wikipedia.org/wiki/Network_Time_Protocol
   
 This code is in the public domain.
 
 */
 
#include <SPI.h>         
#include <Wire.h>
#include <RTClib.h>
#include <RTC_DS3234.h>
#include <Ethernet.h>
#include <EthernetUdp.h>

// Avoid spurious warnings
#undef PROGMEM
#define PROGMEM __attribute__(( section(".progmem.data") ))
#undef PSTR
#define PSTR(s) (__extension__({static prog_char __c[] PROGMEM = (s); &__c[0];}))

// Create an RTC instance, using the chip select pin it's connected to
const int  RTCchipSelect = 8; //chip select 
RTC_DS3234 RTC(RTCchipSelect);

// Enter a MAC address for your controller below.
 // Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {  
  0x90, 0xA2, 0xDA, 0x0D, 0x09, 0x24 }; // Change this to your Ethernet Shield's mac!
 
unsigned int localPort = 8888;      // local port to listen for UDP packets
 
IPAddress timeServer(192, 43, 244, 18); // time.nist.gov NTP server
 
const int NTP_PACKET_SIZE= 48; // NTP time stamp is in the first 48 bytes of the message
 
byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets 

// A UDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
 
void setup() 
{
  // Open serial communications and wait for port to open:
   Serial.begin(9600);
    while (!Serial) {
     ; // wait for serial port to connect. Needed for Leonardo only
   }
 
  
  // start Ethernet and UDP
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    for(;;)
      ;
   }
   Udp.begin(localPort);
 }

void loop()
{
  sendNTPpacket(timeServer); // send an NTP packet to a time server
   
  // wait to see if a reply is available
  delay(1000);  
  if ( Udp.parsePacket() ) {  
    // We've received a packet, read the data from it
    Udp.read(packetBuffer,NTP_PACKET_SIZE);  // read the packet into the buffer
    
    //the timestamp starts at byte 40 of the received packet and is four bytes,
    // or two words, long. First, esxtract the two words:
    
    unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
    unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);  
    // combine the four bytes (two words) into a long integer
    // this is NTP time (seconds since Jan 1 1900):
    unsigned long secsSince1900 = highWord << 16 | lowWord;  
    Serial.print("Seconds since Jan 1 1900 = " );
    Serial.println(secsSince1900);               
    
    // now convert NTP time into everyday time:
    Serial.print("Unix time = ");
    // Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
    const unsigned long seventyYears = 2208988800UL;     
    // subtract seventy years:
    unsigned long epoch = secsSince1900 - seventyYears;  
    // print Unix time:
    Serial.println(epoch);
        
    // Syncing the UTC to the RTC
    SPI.begin();
    RTC.begin();
    SetTimeDate(DateTime(epoch).day(), DateTime(epoch).month(), DateTime(epoch).year() - 2000, DateTime(epoch).hour(), DateTime(epoch).minute(), DateTime(epoch).second());
    Serial.println(ReadTimeDate());
    SPI.setDataMode(SPI_MODE0);
    
    // print the hour, minute and second:
    Serial.print("The UTC time is ");       // UTC is the time at Greenwich Meridian (GMT)
    Serial.print((epoch  % 86400L) / 3600); // print the hour (86400 equals secs per day)
    Serial.print(':');  
    if ( ((epoch % 3600) / 60) < 10 ) {
      // In the first 10 minutes of each hour, we'll want a leading '0'
      Serial.print('0');
    }
    Serial.print((epoch  % 3600) / 60); // print the minute (3600 equals secs per minute)
    Serial.print(':'); 
    if ( (epoch % 60) < 10 ) {
      // In the first 10 seconds of each minute, we'll want a leading '0'
      Serial.print('0');
    }
    Serial.println(epoch %60); // print the second
  }
  // wait a second before asking for the time again
  delay(1000); 
}
 
// send an NTP request to the time server at the given address 
unsigned long sendNTPpacket(IPAddress& address)
{
  // set all bytes in the buffer to 0
  memset(packetBuffer, 0, NTP_PACKET_SIZE); 
  // Initialize values needed to form NTP request
  // (see URL above for details on the packets)
  packetBuffer[0] = 0b11100011;   // LI, Version, Mode
  packetBuffer[1] = 0;     // Stratum, or type of clock
  packetBuffer[2] = 6;     // Polling Interval
  packetBuffer[3] = 0xEC;  // Peer Clock Precision
  // 8 bytes of zero for Root Delay & Root Dispersion
  packetBuffer[12]  = 49; 
  packetBuffer[13]  = 0x4E;
  packetBuffer[14]  = 49;
  packetBuffer[15]  = 52;
  
  // all NTP fields have been given values, now
  // you can send a packet requesting a timestamp:         
  Udp.beginPacket(address, 123); //NTP requests are to port 123
  Udp.write(packetBuffer,NTP_PACKET_SIZE);
  Udp.endPacket(); 
}

//=====================================
String ReadTimeDate(){
	String temp;
	int TimeDate [7]; //second,minute,hour,null,day,month,year		
	SPI.setDataMode(SPI_MODE1);
        for(int i=0; i<=6;i++){
		if(i==3)
			i++;
		digitalWrite(RTCchipSelect, LOW);
		SPI.transfer(i+0x00); 
		unsigned int n = SPI.transfer(0x00);        
		digitalWrite(RTCchipSelect, HIGH);
		int a=n & B00001111;    
		if(i==2){	
			int b=(n & B00110000)>>4; //24 hour mode
			if(b==B00000010)
				b=20;        
			else if(b==B00000001)
				b=10;
			TimeDate[i]=a+b;
		}
		else if(i==4){
			int b=(n & B00110000)>>4;
			TimeDate[i]=a+b*10;
		}
		else if(i==5){
			int b=(n & B00010000)>>4;
			TimeDate[i]=a+b*10;
		}
		else if(i==6){
			int b=(n & B11110000)>>4;
			TimeDate[i]=a+b*10;
		}
		else{	
			int b=(n & B01110000)>>4;
			TimeDate[i]=a+b*10;	
			}
	}
        //Year
	temp.concat("20") ;
        if (TimeDate[6] < 10 ) {
           // In the first 10 minutes of each hour, we'll want a leading '0'
           temp.concat("0");
        }
	temp.concat(TimeDate[6]);
	temp.concat("-") ;
        //Month
        if (TimeDate[5] < 10 ) {
           // In the first 10 minutes of each hour, we'll want a leading '0'
           temp.concat("0");
        }
	temp.concat(TimeDate[5]);
	temp.concat("-") ;
        //Day
        if (TimeDate[4] < 10 ) {
           // In the first 10 minutes of each hour, we'll want a leading '0'
           temp.concat("0");
        }
	temp.concat(TimeDate[4]);
	temp.concat("\t") ;
        //Hours
        if (TimeDate[2] < 10 ) {
           // In the first 10 minutes of each hour, we'll want a leading '0'
           temp.concat("0");
        }
	temp.concat(TimeDate[2]);
	temp.concat(":") ;
        //Minutes
        if (TimeDate[1] < 10 ) {
           // In the first 10 minutes of each hour, we'll want a leading '0'
           temp.concat("0");
        }
	temp.concat(TimeDate[1]);
	temp.concat(":") ;
        //Seconds
        if (TimeDate[0] < 10 ) {
           // In the first 10 minutes of each hour, we'll want a leading '0'
           temp.concat("0");
        }
	temp.concat(TimeDate[0]);
  return(temp);
}

//=====================================
int SetTimeDate(int d, int mo, int y, int h, int mi, int s){ 
	int TimeDate [7]={s,mi,h,0,d,mo,y};
	SPI.setDataMode(SPI_MODE1);
        for(int i=0; i<=6; i++){
		if(i==3)
			i++;
		int b= TimeDate[i]/10;
		int a= TimeDate[i]-b*10;
		if(i==2){
			if (b==2)
				b=B00000010;
			else if (b==1)
				b=B00000001;
		}	
		TimeDate[i]= a+(b<<4);
		  
		digitalWrite(RTCchipSelect, LOW);
		SPI.transfer(i+0x80); 
		SPI.transfer(TimeDate[i]);        
		digitalWrite(RTCchipSelect, HIGH);
  }
}

Ik zou inderdaad lekker van pin 10 afblijven. Je hebt net zelf al geconcludeerd dat spelen met pin 10 waarschijnlijk de oorzaak is van je probleem.

Pak een andere pin als CS voor je clock. Je hebt zelf een code geschreven met spiRtc, kleine moeite om dat zelf aan te passen.