I'd like to use a micro SD shield and DS 3237 RTC devices together using SPI protocol with a FIO.
When I had tried the SD and RTC separately with both devices connected, the sketch runs fine,
However, when I have tried them both, it does not work.
The following code has problem to write something on SD memory. "Not OK" message is printed on the serial terminal. I attached the source code.
#include <SPI.h>
#include <SD.h>
#define ECHO_TO_SERIAL 1
#define RTC_SS 9 // slave select for RTC
#define SD_SS 8 // slave select for SD
#define MOSI 11
#define MISO 12
#define SCK 13
File logFile;
void setup() {
// pinMode(CS, OUTPUT);
pinMode(MOSI, OUTPUT);
pinMode(MISO, INPUT);
pinMode(SCK, OUTPUT);
// pinMode(10, OUTPUT);
pinMode(SD_SS, OUTPUT);
digitalWrite(SD_SS, HIGH);
pinMode(RTC_SS, OUTPUT);
digitalWrite(RTC_SS, HIGH);
Serial.begin(9600);
//disable the RTC
//pinMode(RTC_SS, OUTPUT);
// digitalWrite(RTC_SS, HIGH);
SD_init();
SPI.begin();
//SPI.setClockDivider(SPI_CLOCK_DIV8);
RTC_init();
//day(1-31), month(1-12), year(0-99), hour(0-23), minute(0-59), second(0-59)
SetTimeDate(21,5,14,12,0,0);
}
void loop() {
Serial.println(ReadTimeDate());
//sd_loop();
fileTest();
delay(2000);
}
void fileTest() {
if (digitalRead(RTC_SS) == LOW) {
Serial.println("RTC is LOW1");
digitalWrite(RTC_SS, HIGH);
}
if (digitalRead(SD_SS) == HIGH) {
Serial.println("SD is HIGH1");
digitalWrite(SD_SS, LOW);
}
if (logFile) {
if (logFile.println("Hello world 3")) {
Serial.println(" File wirte OK");
logFile.flush();
}
else Serial.print(" Write Not OK ");
} else Serial.println("logFile is NULL");
digitalWrite(SD_SS, HIGH);
Serial.println();
}
int SD_init() {
if (digitalRead(SD_SS) == HIGH) {
Serial.println("SD is HIGH2");
digitalWrite(SD_SS, LOW);
}
if (digitalRead(RTC_SS) == LOW) {
Serial.println("RTC is LOW");
digitalWrite(RTC_SS, HIGH);
}
if (!SD.begin(SD_SS)) {
Serial.println("initialization failed!");
return -1;
} else Serial.println("initialization done.");
// create a new file
char filename[] = "LOGGER00.xls";
for (uint8_t i = 0; i < 300; i++) {
filename[6] = i/10 + '0';
filename[7] = i%10 + '0';
if (! SD.exists(filename)) {
// only open a new file if it doesn't exist
logFile = SD.open(filename, FILE_WRITE);
break; // leave the loop!
}
}
if (! logFile) {
#if ECHO_TO_SERIAL
Serial.println("Could not create file");
delay(20);
#endif //ECHO_TO_SERIAL
return -1;
}
#if ECHO_TO_SERIAL
Serial.print("Logging to: ");
Serial.println(filename);
#endif //ECHO_TO_SERIAL
logFile.println("date \t time \t\t\t temp");
logFile.println("Hello World ");
logFile.flush();
//logFile.close();
//
logFile.println("Hello World 2");
logFile.flush();
#if ECHO_TO_SERIAL
Serial.println("date \t time \t\t\t temp");
#endif //ECHO_TO_SERIAL
}
//=====================================
int RTC_init(){
pinMode(RTC_SS,OUTPUT); // chip select
// start the SPI library:
// SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE1); // both mode 1 & 3 should work
//set control register
digitalWrite(RTC_SS, LOW);
SPI.transfer(0x8E);
SPI.transfer(0x60); //60= disable Osciallator and Battery SQ wave @1hz, temp compensation, Alarms disabled
digitalWrite(RTC_SS, HIGH);
delay(10);
}
//=====================================
int SetTimeDate(int d, int mo, int y, int h, int mi, int s){
int TimeDate [7]={s,mi,h,0,d,mo,y};
for(int i=0; i<=6;i++){
if(i==3)
i++;
int b= TimeDate*/10;*
int a= TimeDate_-b10;_
_ if(i==2){_
_ if (b==2)_
_ b=B00000010;_
_ else if (b==1)_
_ b=B00000001;_
_ } _
_ TimeDate= a+(b<<4);*_
* digitalWrite(RTC_SS, LOW);
_ SPI.transfer(i+0x80);
SPI.transfer(TimeDate); _
digitalWrite(RTC_SS, HIGH);
_ }
}
//=====================================
String ReadTimeDate(){
String temp;
int TimeDate [7]; //second,minute,hour,null,day,month,year _
if (digitalRead(SD_SS) == LOW) {
_ Serial.println("SD is LOW");_
digitalWrite(SD_SS, HIGH);
_ }*_
* for(int i=0; i<=6;i++){*
* if(i==3)*
* i++;*
* digitalWrite(RTC_SS, LOW);
_ SPI.transfer(i+0x00);
unsigned int n = SPI.transfer(0x00); _
digitalWrite(RTC_SS, 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=a+b;
}
else if(i==4){
int b=(n & B00110000)>>4;
TimeDate=a+b10;
* }
else if(i==5){
int b=(n & B00010000)>>4;
TimeDate=a+b10;
* }
else if(i==6){
int b=(n & B11110000)>>4;
TimeDate=a+b10;
* }
else{
int b=(n & B01110000)>>4;
TimeDate=a+b10;
* }
}
temp.concat(TimeDate[4]);
temp.concat("/") ;
temp.concat(TimeDate[5]);
temp.concat("/") ;
temp.concat(TimeDate[6]);
temp.concat(" ") ;
temp.concat(TimeDate[2]);
temp.concat(":") ;
temp.concat(TimeDate[1]);
temp.concat(":") ;
temp.concat(TimeDate[0]);
return(temp);
}*_