Go Down

Topic: Concatenate int to make a file name (Read 2047 times) previous topic - next topic

mrescude

Oct 10, 2012, 12:31 am Last Edit: Oct 10, 2012, 12:40 am by Coding Badly Reason: 1
Hello,

I have an RTC DS1307 attached to my Arduino Leonardo with the Ethernet/SD Shield. I want to use the hour, dayOfMonth, month and year variables to make a char array that look like this 16091012.txt (4pm in 09/10/2012--Today). This way i could create a file in the SD Card. I'm using

I tried to use strcpy and strcat, but the program stops responding before arriving to these instructions. when i delete them, the programs runs fine.
This also happens when using itoa function.

Code: [Select]



#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68  // This is the I2C address
// Arduino version compatibility Pre-Compiler Directives
#if defined(ARDUINO) && ARDUINO >= 100   // Arduino v1.0 and newer
 #define I2C_WRITE Wire.write
 #define I2C_READ Wire.read
#else                                   // Arduino Prior to v1.0
 #define I2C_WRITE Wire.send
 #define I2C_READ Wire.receive
#endif
// Global Variables
int command = 0;       // This is the command char, in ascii form, sent from the serial port    
int i;
long previousMillis = 0;        // will store last time Temp was updated
int second, minute, hour, dayOfWeek, dayOfMonth, month, year;
byte test;
byte zero;
char *nombrearchivo, *hora, *dia, *mes, *ano;


// initialize the library with the numbers of the interface pins

// 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) );
}

// 1) Sets the date and time on the ds1307
// 2) Starts the clock
// 3) Sets hour mode to 24 hour clock
// Assumes you're passing in valid numbers, Probably need to put in checks for valid numbers.

void setDateDs1307()                
{

  second = (byte) ((Serial.read() - 48) * 10 + (Serial.read() - 48)); // Use of (byte) type casting and ascii math to achieve result.  
  minute = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
  hour  = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
  dayOfWeek = (byte) (Serial.read() - 48);
  dayOfMonth = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
  month = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
  year= (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  I2C_WRITE(zero);
  I2C_WRITE(decToBcd(second) & 0x7f);    // 0 to bit 7 starts the clock
  I2C_WRITE(decToBcd(minute));
  I2C_WRITE(decToBcd(hour));      // If you want 12 hour am/pm you need to set
                                  // bit 6 (also need to change readDateDs1307)
  I2C_WRITE(decToBcd(dayOfWeek));
  I2C_WRITE(decToBcd(dayOfMonth));
  I2C_WRITE(decToBcd(month));
  I2C_WRITE(decToBcd(year));
  Wire.endTransmission();
}

// Gets the date and time from the ds1307 and prints result
void getDateDs1307()
{
 // Reset the register pointer
 Wire.beginTransmission(DS1307_I2C_ADDRESS);
 I2C_WRITE(zero);
 Wire.endTransmission();

 Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
 

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

 if (hour < 10)
   Serial.print('0');
 Serial.print(hour, DEC);
 Serial.print(":");
 if (minute < 10)
   Serial.print("0");
 Serial.print(minute, DEC);
 Serial.print(":");
 if (second < 10)
   Serial.print("0");
 Serial.print(second, DEC);
 Serial.print(", ");
 if (dayOfMonth < 10)
   Serial.print('0');
 Serial.print(dayOfMonth, DEC);
 Serial.print(" ,");
 Serial.print(month, DEC);
 Serial.print(" 20");
 if (year < 10)
   Serial.print("0");
 Serial.println(year);
 
 //#####Concatenatestrins into filename######

}


void setup() {
 
 Wire.begin();
 Serial.begin(57600);
 zero=0x00;
}

void loop() {
    if (Serial.available()) {      // Look for char in serial que and process if found
     command = Serial.read();
     if (command == 84 || command == 116) {      //If command = "Tt" Set Date
      //setDateDs1307();
      getDateDs1307();
      Serial.println(" ");
     }
     else if (command == 82 || command == 114) {      //If command = "Rr" Read Date ... BBR
      getDateDs1307();
      Serial.println(" ");
     }
     else if (command == 81 || command == 113) {      //If command = "Qq" RTC1307 Memory Functions
       delay(100);    
       if (Serial.available()) {
        command = Serial.read();
        if (command == 49) {        //If command = "1" RTC1307 Initialize Memory - All Data will be set to
                                      // 255 (0xff).  Therefore 255 or 0 will be an invalid value.  
          Wire.beginTransmission(DS1307_I2C_ADDRESS);   // 255 will be the init value and 0 will be considered
                                                         // an error that occurs when the RTC is in Battery mode.
          I2C_WRITE(0x08); // Set the register pointer to be just past the date/time registers.
          for (i = 1; i <= 24; i++) {
              I2C_WRITE(0Xff);
             delay(10);
          }  
          Wire.endTransmission();
          Wire.beginTransmission(DS1307_I2C_ADDRESS);  
          I2C_WRITE(0x21); // Set the register pointer to 33 for second half of registers. Only 32 writes per connection allowed.
          for (i = 1; i <= 33; i++) {
              I2C_WRITE(0Xff);
             delay(10);
          }  
          Wire.endTransmission();
          getDateDs1307();
          Serial.println(": RTC1307 Initialized Memory");
        }
        else if (command == 50) {      //If command = "2" RTC1307 Memory Dump
         getDateDs1307();
         Serial.println(": RTC 1307 Dump Begin");
         Wire.beginTransmission(DS1307_I2C_ADDRESS);
         I2C_WRITE(zero);
         Wire.endTransmission();
         Wire.requestFrom(DS1307_I2C_ADDRESS, 32);
         for (i = 0; i <= 31; i++) {  //Register 0-31 - only 32 registers allowed per I2C connection
            test = I2C_READ();
            Serial.print(i);
            Serial.print(": ");
            Serial.print(test, DEC);
            Serial.print(" : ");
            Serial.println(test, HEX);
         }
         Wire.beginTransmission(DS1307_I2C_ADDRESS);
         I2C_WRITE(0x20);
         Wire.endTransmission();
         Wire.requestFrom(DS1307_I2C_ADDRESS, 32);  
         for (i = 32; i <= 63; i++) {         //Register 32-63 - only 32 registers allowed per I2C connection
            test = I2C_READ();
            Serial.print(i);
            Serial.print(": ");
            Serial.print(test, DEC);
            Serial.print(" : ");
            Serial.println(test, HEX);
         }
         Serial.println(" RTC1307 Dump end");
        }
       }  
      }
     Serial.print("Command: ");
     Serial.println(command);     // Echo command CHAR in ascii that was sent
     }
     command = 0;                 // reset command
     delay(100);
   }



This is a code for RTC that i found. I want to use the function getDateDs1307(), this way i'll have the hour,dayOfMonth, month and year in integer variables.

What i need is like

char filedate[13]="16091012.txt"

Any ideas?

Thank you!

KeithRB

I don't see any str* commands in your code.

I also don't see where you created the char buffer to hold the filename.

sprintf() might be an easier solution, it can create the thing in one command.

mrescude

Srry, i copied the original sketch, without the strings tests and such.

char *nombrearchivo is the variable for the file name.
char *hora, *dia, *mes, *ano, are to store the hour, dayOfMonth, month and year as characters

Code: [Select]


void getDateDs1307()
{
  // Reset the register pointer
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  I2C_WRITE(zero);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
 

  // A few of these need masks because certain bits are control bits
  second     = bcdToDec(I2C_READ() & 0x7f);
  minute     = bcdToDec(I2C_READ());
  hour       = bcdToDec(I2C_READ() & 0x3f);  // Need to change this if 12 hour am/pm
  dayOfWeek  = bcdToDec(I2C_READ());
  dayOfMonth = bcdToDec(I2C_READ());
  month      = bcdToDec(I2C_READ());
  year       = bcdToDec(I2C_READ());
 
////This prints the RTC properly
  if (hour < 10)
    Serial.print('0');
  Serial.print(hour, DEC);
  Serial.print(":");
  if (minute < 10)
    Serial.print("0");
  Serial.print(minute, DEC);
  Serial.print(":");
  if (second < 10)
    Serial.print("0");
  Serial.print(second, DEC);
  Serial.print(", ");
  if (dayOfMonth < 10)
    Serial.print('0');
  Serial.print(dayOfMonth, DEC);
  Serial.print(" ,");
  Serial.print(month, DEC);
  Serial.print(" 20");
  if (year < 10)
    Serial.print("0");
  Serial.println(year);
 
  //#####Concatenatestrins into filename######

}


When i call the above function (Sending r or R with the serial monitor), the result is the following
Quote

17:22:17, 09 ,10 2012

Command: 114


I tried using itoa and then strcpy and strcat. The following code would be in the same function, after the date is printed, to see if it matches.

Code: [Select]


  itoa(hour, hora, 10);
  strcpy(nombrearchivo, hora);
  Serial.println(nombrearchivo); //Result should be 14
  itoa(dayOfMonth, dia, 10);
  strcat(nombrearchivo, dia);
  Serial.println(nombrearchivo);//Result should be 1409
  itoa(month, mes, 10);
  strcat(nombrearchivo, mes);
  Serial.println(nombrearchivo);//Result should be 140910
  itoa(year, ano, 10);
  strcat(nombrearchivo, ano);
  Serial.println(nombrearchivo);//Result should be 14091012


It compiles without a problem, but with the serial monitor responds this way

Quote

16:25


and then the whole program stops responding, that's what I don't understand, because even if the code added doesn't work, it stops working before getting there.


mrescude

I finally did it with sprintf(), I wasn't aware of this function.

The final code is this. I you notice a way to reduce program memory, please tell me!
Code: [Select]

void getDateDs1307()
{
  // Reset the register pointer
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  I2C_WRITE(zero);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
 

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

//  if (hour < 10)
//    Serial.print('0');
//  Serial.print(hour, DEC);
//  Serial.print(":");
//  if (minute < 10)
//    Serial.print("0");
//  Serial.print(minute, DEC);
//  Serial.print(":");
//  if (second < 10)
//    Serial.print("0");
//  Serial.print(second, DEC);
//  Serial.print(", ");
//  if (dayOfMonth < 10)
//    Serial.print('0');
//  Serial.print(dayOfMonth, DEC);
//  Serial.print(" ,");
//  Serial.print(month, DEC);
//  Serial.print(" 20");
//  if (year < 10)
//    Serial.print("0");
//  Serial.println(year);
 

  if(hour<10){
    if(dayOfMonth<10){
      if(month<10){
        sprintf(nombrearchivo,"0%d0%d0%d%d.txt",hour, dayOfMonth, month, year); //merge together
        Serial.println(nombrearchivo);
      }
      else{
        sprintf(nombrearchivo,"0%d0%d%d%d.txt",hour, dayOfMonth, month, year); //merge together
        Serial.println(nombrearchivo);
      }
    }
    else{
      if(month<10){
        sprintf(nombrearchivo,"0%d%d0%d%d.txt",hour, dayOfMonth, month, year); //merge together
        Serial.println(nombrearchivo);
      }
      else{
        sprintf(nombrearchivo,"0%d%d%d%d.txt",hour, dayOfMonth, month, year); //merge together
        Serial.println(nombrearchivo);
      }
       
    }
  }
  else{
    if(dayOfMonth<10){
      if(month<10){
        sprintf(nombrearchivo,"%d0%d0%d%d.txt",hour, dayOfMonth, month, year); //merge together
        Serial.println(nombrearchivo);
      }
      else{
        sprintf(nombrearchivo,"%d0%d%d%d.txt",hour, dayOfMonth, month, year); //merge together
        Serial.println(nombrearchivo);
      }
    }
    else{
      if(month<10){
        sprintf(nombrearchivo,"%d%d0%d%d.txt",hour, dayOfMonth, month, year); //merge together
        Serial.println(nombrearchivo);
      }
      else{
        sprintf(nombrearchivo,"%d%d%d%d.txt",hour, dayOfMonth, month, year); //merge together
        Serial.println(nombrearchivo);
      }
    }
  }
 
}



This way I maintain the 8 character filename and the extension of the file.

Thanks!

PeterH


I finally did it with sprintf(), I wasn't aware of this function.

The final code is this. I you notice a way to reduce program memory, please tell me!



You've got a lot of repetition there. You should be able to deal with the optional leading zeroes by using the %02d format. The '0' means pad with zeroes instead of spaces, and the '2' means pad the output as necessary to produce two digits. The printf formatting codes can be pretty obscure at the first encounter but the capabilities are described in the documentation for the AVR C library, and they're common to most C runtime libraries, so you shouldn't have any trouble finding tutorials showing how to use them.
I only provide help via the forum - please do not contact me for private consultancy.

mrescude

Ok, so I finally changed it to (day)(month)(year).csv format

Code: [Select]
snprintf(nombrearchivo,13,"%02d%02d20%02d.csv", dayOfMonth, month, year);

Is this correct?

Thanks!

PaulS

Snippets-are-us is down the road a ways. If you want help here, you'll post all of your code.

Quote
char *nombrearchivo is the variable for the file name.
char *hora, *dia, *mes, *ano, are to store the hour, dayOfMonth, month and year as characters

And where do you make these pointers actually point to anything?

mrescude

I already deleted that, i was doing a lot of thing at one.

Here is the corrected code. I works now, but maybe I can reduce program memory and such.

Code: [Select]
#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68  // This is the I2C address
// Arduino version compatibility Pre-Compiler Directives
#define I2C_WRITE Wire.write
#define I2C_READ Wire.read
// Global Variables
int command = 0;       // This is the command char, in ascii form, sent from the serial port     
int i;
long previousMillis = 0;        // will store last time Temp was updated
int second, minute, hour, dayOfWeek, dayOfMonth, month, year;
byte test;
byte zero;
char nombrearchivo[13];



// initialize the library with the numbers of the interface pins

// 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) );
}

// 1) Sets the date and time on the ds1307
// 2) Starts the clock
// 3) Sets hour mode to 24 hour clock
// Assumes you're passing in valid numbers, Probably need to put in checks for valid numbers.

void setDateDs1307()               
{

   second = (byte) ((Serial.read() - 48) * 10 + (Serial.read() - 48)); // Use of (byte) type casting and ascii math to achieve result. 
   minute = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
   hour  = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
   dayOfWeek = (byte) (Serial.read() - 48);
   dayOfMonth = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
   month = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
   year= (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
   Wire.beginTransmission(DS1307_I2C_ADDRESS);
   I2C_WRITE(zero);
   I2C_WRITE(decToBcd(second) & 0x7f);    // 0 to bit 7 starts the clock
   I2C_WRITE(decToBcd(minute));
   I2C_WRITE(decToBcd(hour));      // If you want 12 hour am/pm you need to set
                                   // bit 6 (also need to change readDateDs1307)
   I2C_WRITE(decToBcd(dayOfWeek));
   I2C_WRITE(decToBcd(dayOfMonth));
   I2C_WRITE(decToBcd(month));
   I2C_WRITE(decToBcd(year));
   Wire.endTransmission();
}

// Gets the date and time from the ds1307 and prints result
void getDateDs1307()
{
  // Reset the register pointer
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  I2C_WRITE(zero);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
 

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

//  if (hour < 10)
//    Serial.print('0');
//  Serial.print(hour, DEC);
//  Serial.print(":");
//  if (minute < 10)
//    Serial.print("0");
//  Serial.print(minute, DEC);
//  Serial.print(":");
//  if (second < 10)
//    Serial.print("0");
//  Serial.print(second, DEC);
//  Serial.print(", ");
//  if (dayOfMonth < 10)
//    Serial.print('0');
//  Serial.print(dayOfMonth, DEC);
//  Serial.print(" ,");
//  Serial.print(month, DEC);
//  Serial.print(" 20");
//  if (year < 10)
//    Serial.print("0");
//  Serial.println(year);
   
  snprintf(nombrearchivo,13,"%02d%02d20%02d.csv", dayOfMonth, month, year); //merge together
  Serial.println(nombrearchivo);
   
}


void setup() {
 
  Wire.begin();
   
  zero=0x00;
   // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

}

void loop() {
     if (Serial.available()) {      // Look for char in serial que and process if found
      command = Serial.read();
      if (command == 84 || command == 116) {      //If command = "Tt" Set Date
       setDateDs1307();
       getDateDs1307();
       
       Serial.println(nombrearchivo);

      }
      else if (command == 82 || command == 114) {      //If command = "Rr" Read Date ... BBR
       getDateDs1307();
       Serial.println(" ");
      }
     
      Serial.print("Command: ");
      Serial.println(command);     // Echo command CHAR in ascii that was sent
      }
      command = 0;                 // reset command
      delay(100);
     
     
  }




dxw00d

Code: [Select]
   second = (byte) ((Serial.read() - 48) * 10 + (Serial.read() - 48)); // Use of (byte) type casting and ascii math to achieve result. 
   minute = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
   hour  = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
   dayOfWeek = (byte) (Serial.read() - 48);
   dayOfMonth = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
   month = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
   year= (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));


This strikes me as a bit risky. As you don't check whether all the required data is available, I would expect you to get a few -1 returns from Serial.read().

PaulS

Code: [Select]
   second = (byte) ((Serial.read() - 48) * 10 + (Serial.read() - 48));
Aside from the problem that dxw00d pointed out, something like this:
Code: [Select]
   second = (byte) ((Serial.read() - '0') * 10 + (Serial.read() - '0'));
is far easier to understand without the need to consult an ASCII table.

mrescude

All of the RTC code was used from this page http://combustory.com/wiki/index.php/RTC1307_-_Real_Time_Clock, and I've had no problems with it.
I understand that it could miss a Serial.read(), but I'll only set the date once. I'm just keeping that part of the code so I can try new date configurations, basically I'll just use the getDate function.

Thank you!

el_supremo

Code: [Select]
snprintf(nombrearchivo,13,"%02d%02d20%02d.csv", dayOfMonth, month, year);
That filename structure will not sort well when you have a lot of files. I'd suggest that you use YYYYMMDD instead so that the files will be sorted into natural order when you view them in Windows Explorer or use 'ls' or whatever on Linux.
Code: [Select]
snprintf(nombrearchivo,13,"20%02d%02d%02d.csv",year,month, dayOfMonth);

Pete

mrescude

You're right, I'll change that!

Thanks!

AMPS-N

can some tell me how did we assigned this address:#define DS1307_I2C_ADDRESS 0x68  . how we get this I2c address


Hello,

I have an RTC DS1307 attached to my Arduino Leonardo with the Ethernet/SD Shield. I want to use the hour, dayOfMonth, month and year variables to make a char array that look like this 16091012.txt (4pm in 09/10/2012--Today). This way i could create a file in the SD Card. I'm using

I tried to use strcpy and strcat, but the program stops responding before arriving to these instructions. when i delete them, the programs runs fine.
This also happens when using itoa function.

Code: [Select]



#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68  // This is the I2C address
// Arduino version compatibility Pre-Compiler Directives
#if defined(ARDUINO) && ARDUINO >= 100   // Arduino v1.0 and newer
 #define I2C_WRITE Wire.write
 #define I2C_READ Wire.read
#else                                   // Arduino Prior to v1.0
 #define I2C_WRITE Wire.send
 #define I2C_READ Wire.receive
#endif
// Global Variables
int command = 0;       // This is the command char, in ascii form, sent from the serial port    
int i;
long previousMillis = 0;        // will store last time Temp was updated
int second, minute, hour, dayOfWeek, dayOfMonth, month, year;
byte test;
byte zero;
char *nombrearchivo, *hora, *dia, *mes, *ano;


// initialize the library with the numbers of the interface pins

// 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) );
}

// 1) Sets the date and time on the ds1307
// 2) Starts the clock
// 3) Sets hour mode to 24 hour clock
// Assumes you're passing in valid numbers, Probably need to put in checks for valid numbers.

void setDateDs1307()                
{

  second = (byte) ((Serial.read() - 48) * 10 + (Serial.read() - 48)); // Use of (byte) type casting and ascii math to achieve result.  
  minute = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
  hour  = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
  dayOfWeek = (byte) (Serial.read() - 48);
  dayOfMonth = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
  month = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
  year= (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  I2C_WRITE(zero);
  I2C_WRITE(decToBcd(second) & 0x7f);    // 0 to bit 7 starts the clock
  I2C_WRITE(decToBcd(minute));
  I2C_WRITE(decToBcd(hour));      // If you want 12 hour am/pm you need to set
                                  // bit 6 (also need to change readDateDs1307)
  I2C_WRITE(decToBcd(dayOfWeek));
  I2C_WRITE(decToBcd(dayOfMonth));
  I2C_WRITE(decToBcd(month));
  I2C_WRITE(decToBcd(year));
  Wire.endTransmission();
}

// Gets the date and time from the ds1307 and prints result
void getDateDs1307()
{
 // Reset the register pointer
 Wire.beginTransmission(DS1307_I2C_ADDRESS);
 I2C_WRITE(zero);
 Wire.endTransmission();

 Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
 

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

 if (hour < 10)
   Serial.print('0');
 Serial.print(hour, DEC);
 Serial.print(":");
 if (minute < 10)
   Serial.print("0");
 Serial.print(minute, DEC);
 Serial.print(":");
 if (second < 10)
   Serial.print("0");
 Serial.print(second, DEC);
 Serial.print(", ");
 if (dayOfMonth < 10)
   Serial.print('0');
 Serial.print(dayOfMonth, DEC);
 Serial.print(" ,");
 Serial.print(month, DEC);
 Serial.print(" 20");
 if (year < 10)
   Serial.print("0");
 Serial.println(year);
 
 //#####Concatenatestrins into filename######

}


void setup() {
 
 Wire.begin();
 Serial.begin(57600);
 zero=0x00;
}

void loop() {
    if (Serial.available()) {      // Look for char in serial que and process if found
     command = Serial.read();
     if (command == 84 || command == 116) {      //If command = "Tt" Set Date
      //setDateDs1307();
      getDateDs1307();
      Serial.println(" ");
     }
     else if (command == 82 || command == 114) {      //If command = "Rr" Read Date ... BBR
      getDateDs1307();
      Serial.println(" ");
     }
     else if (command == 81 || command == 113) {      //If command = "Qq" RTC1307 Memory Functions
       delay(100);    
       if (Serial.available()) {
        command = Serial.read();
        if (command == 49) {        //If command = "1" RTC1307 Initialize Memory - All Data will be set to
                                      // 255 (0xff).  Therefore 255 or 0 will be an invalid value.  
          Wire.beginTransmission(DS1307_I2C_ADDRESS);   // 255 will be the init value and 0 will be considered
                                                         // an error that occurs when the RTC is in Battery mode.
          I2C_WRITE(0x08); // Set the register pointer to be just past the date/time registers.
          for (i = 1; i <= 24; i++) {
              I2C_WRITE(0Xff);
             delay(10);
          }  
          Wire.endTransmission();
          Wire.beginTransmission(DS1307_I2C_ADDRESS);  
          I2C_WRITE(0x21); // Set the register pointer to 33 for second half of registers. Only 32 writes per connection allowed.
          for (i = 1; i <= 33; i++) {
              I2C_WRITE(0Xff);
             delay(10);
          }  
          Wire.endTransmission();
          getDateDs1307();
          Serial.println(": RTC1307 Initialized Memory");
        }
        else if (command == 50) {      //If command = "2" RTC1307 Memory Dump
         getDateDs1307();
         Serial.println(": RTC 1307 Dump Begin");
         Wire.beginTransmission(DS1307_I2C_ADDRESS);
         I2C_WRITE(zero);
         Wire.endTransmission();
         Wire.requestFrom(DS1307_I2C_ADDRESS, 32);
         for (i = 0; i <= 31; i++) {  //Register 0-31 - only 32 registers allowed per I2C connection
            test = I2C_READ();
            Serial.print(i);
            Serial.print(": ");
            Serial.print(test, DEC);
            Serial.print(" : ");
            Serial.println(test, HEX);
         }
         Wire.beginTransmission(DS1307_I2C_ADDRESS);
         I2C_WRITE(0x20);
         Wire.endTransmission();
         Wire.requestFrom(DS1307_I2C_ADDRESS, 32);  
         for (i = 32; i <= 63; i++) {         //Register 32-63 - only 32 registers allowed per I2C connection
            test = I2C_READ();
            Serial.print(i);
            Serial.print(": ");
            Serial.print(test, DEC);
            Serial.print(" : ");
            Serial.println(test, HEX);
         }
         Serial.println(" RTC1307 Dump end");
        }
       }  
      }
     Serial.print("Command: ");
     Serial.println(command);     // Echo command CHAR in ascii that was sent
     }
     command = 0;                 // reset command
     delay(100);
   }



This is a code for RTC that i found. I want to use the function getDateDs1307(), this way i'll have the hour,dayOfMonth, month and year in integer variables.

What i need is like

char filedate[13]="16091012.txt"

Any ideas?

Thank you!

AMPS


Go Up