sabato 2 gennaio 2010

Arduino 2009 + Micro SD + RTC Module

La prima puntata.

Proseguendo il progetto precedente aggiungo anche il modulo RTC (Real Time Clock) della SparkFun, seguendo questo ottimo tutorial. La cosa interessante è l’utilizzo del protocollo I2C, trasmissione seriale bifilare (SDA e SCL). Codici colori per le resistenze. Per ora non saldo i fili, collego solo, a progetto finito vedo di integrare tutto su una mille fori. Ecco la bozza di codice per un primo file di log (integrato dai vari codici trovati):

#include <Wire.h>
#include <SdFat.h>
#include <SdFatUtil.h>

#define DS1307_I2C_ADDRESS 0x68

#define LOG_INTERVAL 1000 // mills between entries
#define ECHO_TO_SERIAL 1 // echo data to serial port
#define WAIT_TO_START 1 // Wait for serial input in setup()
#define SYNC_INTERVAL 1000 // mills between calls to sync()
uint32_t syncTime = 0; // time of last sync()


Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;

// store error strings in flash to save RAM
#define error(s) error_P(PSTR(s))
void error_P(const char *str)
{
PgmPrint("error: ");
SerialPrintln_P(str);
if (card.errorCode()) {
PgmPrint("SD error: ");
Serial.print(card.errorCode(), HEX);
Serial.print(',');
Serial.println(card.errorData(), HEX);
}
while(1);
}


// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val){
return ( (val/10*16) + (val%10) );
}

// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val){
return ( (val/16*10) + (val%16) );
}


// Gets the date and time from the ds1307
void getDateDs1307(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
// Reset the register pointer
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.send(0);
Wire.endTransmission();

Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

// A few of these need masks because certain bits are control bits
*second = bcdToDec(Wire.receive() & 0x7f);
*minute = bcdToDec(Wire.receive());
*hour = bcdToDec(Wire.receive() & 0x3f); // Need to change this if 12 hour am/pm
*dayOfWeek = bcdToDec(Wire.receive());
*dayOfMonth = bcdToDec(Wire.receive());
*month = bcdToDec(Wire.receive());
*year = bcdToDec(Wire.receive());
}

void setup(){
Serial.begin(9600);
Serial.println();

#if WAIT_TO_START
Serial.println("Type any character to start");
while (!Serial.available());
#endif //WAIT_TO_START

Wire.begin();

//SD CARD
pinMode(8, OUTPUT);
digitalWrite(8, HIGH);

// initialize the SD card
if (!card.init(true)) error("card.init");

// initialize a FAT volume
if (!volume.init(card)) error("volume.init");

// open root directory
if (!root.openRoot(volume)) error("openRoot");

// create a new file
char name[] = "LOGGER00.CSV";
for (uint8_t i = 0; i < 100; i++) {
name[6] = i/10 + '0';
name[7] = i%10 + '0';
if (file.open(root, name, O_CREAT | O_EXCL | O_WRITE)) break;
}
if (!file.isOpen()) error ("file.create");

#if ECHO_TO_SERIAL
Serial.print("Logging to: ");
Serial.println(name);
#endif;

}

void loop()
{
file.writeError = 0;
delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL));

byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;

getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
file.print(hour, DEC);
file.print(":");
file.print(minute, DEC);
file.print(":");
file.print(second, DEC);
file.print(" ");
file.print(year, DEC);
file.print("-");
file.print(month, DEC);
file.print("-");
file.print(dayOfMonth, DEC);
file.println();

#if ECHO_TO_SERIAL
Serial.print(hour, DEC);
Serial.print(":");
Serial.print(minute, DEC);
Serial.print(":");
Serial.print(second, DEC);
Serial.print(" ");
Serial.print(year, DEC);
Serial.print("-");
Serial.print(month, DEC);
Serial.print("-");
Serial.print(dayOfMonth, DEC);
Serial.println();
#endif;

if (file.writeError) error("write data");

//don't sync too often - requires 2048 bytes of I/O to SD card
if ((millis() - syncTime) < SYNC_INTERVAL) return;
syncTime = millis();
if (!file.sync()) error("sync");

delay(1000);
}



Prima di eseguire il codice precedente vi consiglio di eseguire un setting del modulo RTC con l’ora corrente.Per il prossimo passo prevedo di integrare qualche sensore esterno.



Lib: sdFatLib



Hardware: Arduino 2009, Micro SD Module Libelium e RTC Module Sparkfun.



IMG_6439 IMG_6438 



1 commento:

Anonimo ha detto...

Grazie interessante