Pages: 1 [2]   Go Down
Author Topic: power outage recovery using DS1307 Real Time Clock  (Read 4400 times)
0 Members and 1 Guest are viewing this topic.
Space Coast Florida
Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The example code uses noise from an unused analog pin seed the random() function. I think that will be good enough. My application does not require TRUE random numbers. The repeating pattern would really be good enough.
Logged

Space Coast Florida
Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Now enter maximum weirdness .
I took the older code and turned it into a couple of functions. I swear it worked last night very late. At work today this code froze at the uploading point and made  my serial programming port unreachable. I determined that the ReadRTCRAM() function is to blame. Does this make any sense to anybody
Code:
/*
* Q(1-2) - (Q1) Memory Load  (Q2) RTC - Memory Read  
 
 */
#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68  // This is the I2C address


// Global Variables
long randNumber;
int command = 0;       // This is the command char, in ascii form, sent from the serial port    
byte zero;
int magnum1 = 90;      // dummy var
int magnum2 = 9;      //  dummy var

int  state   = 96;     // State machine variable
int  counter = 255;      // Master counter variable

void setup() {
   randomSeed(4);
  Wire.begin();           // join i2c bus
  Serial.begin(57600);
  zero=0x00;             // not sure what this is for
}

void loop() {
  if (Serial.available()) {      // Look for char in serial que and process if found
    command = Serial.read();

    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"  load data to RAM
         WriteRTCRAM();
        }
        else if (command == 50) {      //If command = "2" Read data from RAM
          ReadRTCRAM();
        }
      }  
    }
  }
  command = 0;                 // reset command
  delay(100);
}
//********************************
void WriteRTCRAM(){
    randNumber = random(250);
  Serial.println(randNumber);  

 Wire.beginTransmission(DS1307_I2C_ADDRESS);   // 255 will be the init value and 0 will be considered
          Wire.write(32);                   // Set the register pointer to before data location
          Wire.write(randNumber);              // Write magic number before storing data
          Wire.write(state);                // Store State variable
          Wire.write(counter >> 8);         // writes the high byte of counter
          Wire.write(counter & 0xFF);       // writes the low byte of counter
          Wire.write(randNumber);              // Write magic number after storing data
          Wire.endTransmission();

          Serial.println("Data Stored");
}
//********************************
void ReadRTCRAM(){
Serial.println("Begin Read");
          Wire.beginTransmission(DS1307_I2C_ADDRESS);
          Wire.write(zero);   // not sure what this is for
          Wire.endTransmission();

          Wire.beginTransmission(DS1307_I2C_ADDRESS);
          Wire.write(0x20);         // not sure what this is doing
          Wire.endTransmission();
          Wire.requestFrom(DS1307_I2C_ADDRESS, 32);  // Set pointer to the first byte of data

          magnum1 = (Wire.read());  //magic number written before data write
          state = (Wire.read());    // Read state data
          counter = (Wire.read() << 8) + Wire.read(); // Read first byte shift left read second byte
          magnum2 = (Wire.read());  // Read Magic number written after data write
          Serial.print(" Magic Number = ");
          Serial.println(magnum1);
          Serial.print(" State = ");
          Serial.println(state);
          Serial.print(" Counter = ");
          Serial.println(counter);
          Serial.print(" Magic Number = ");
          Serial.println(magnum2);
          Serial.println(" RTC1307 Dump end");
          if (magnum1 = magnum2){
          Serial.print (" Write Error!!!");
          }
}
this is just too strange
« Last Edit: January 21, 2013, 10:00:43 pm by urthlight » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 652
Posts: 50861
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    if (command == 81 || command == 113) {      //If command = "Qq" RTC1307 Memory Functions
Why not make it easy to read this code?
Code:
    if (command == 'Q' || command == 'q') {      // No comment needed; its obvious what to type

Code:
          magnum1 = (Wire.read());  //magic number written before data write
          state = (Wire.read());    // Read state data
          counter = (Wire.read() << 8) + Wire.read(); // Read first byte shift left read second byte
          magnum2 = (Wire.read());  // Read Magic number written after data write
(What's) (with) (all) (the) (parentheses) (?)

Code:
          if (magnum1 = magnum2){
Yeah, right. An assignment in the middle of an if statement...

Code:
          Wire.write(zero);   // not sure what this is for
          Wire.write(0x20);         // not sure what this is doing
Don't you think you should figure these out?
Logged

Space Coast Florida
Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 Insert Quote
Code:

    if (command == 81 || command == 113) {      //If command = "Qq" RTC1307 Memory Functions
Quote
Why not make it easy to read this code?
Ok np but this is just temp code to get it running



  
Code:
         magnum1 = (Wire.read());  //magic number written before data write
          state = (Wire.read());    // Read state data
          counter = (Wire.read() << 8) + Wire.read(); // Read first byte shift left read second byte
          magnum2 = (Wire.read());  // Read Magic number written after data write
Quote
(What's) (with) (all) (the) (parentheses) (?)

Ok fixed

Code:
         if (magnum1 = magnum2){
Quote
Yeah, right. An assignment in the middle of an if statement...
Ah this is supposed to be
Code:
[code]
          if (magnum1 != magnum2){
[/code]

Code:

          Wire.write(zero);   // not sure what this is for
          Wire.write(0x20);         // not sure what this is doing
Quote
Don't you think you should figure these out?

Well being new to  the TWI I dont fully understand but I did find
    Issue 527 : Stream-based Wire library doesn't accept zero
and the fix  which I used.

and still after all of this I still cannot get this to upload unless I REM out the If statement.

« Last Edit: January 22, 2013, 12:11:51 pm by urthlight » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 652
Posts: 50861
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
and still after all of this I still cannot get this to compile unless I REM out the If statement.
Time to post new code, and the error messages.
Logged

Space Coast Florida
Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
/*
* Q(1-2) - (Q1) Memory Load  (Q2) RTC - Memory Read 
 
 */
#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68  // This is the I2C address


// Global Variables
long randNumber;
int command = 0;       // This is the command char, in ascii form, sent from the serial port     
byte zero;
int  state   = 96;     // State machine variable
int  counter = 255;      // Master counter variable
int magnum1 = 90;      // dummy var
int magnum2 = 9;      //  dummy var

void setup() {
   randomSeed(analogRead(0));
  Wire.begin();           // join i2c bus
  Serial.begin(57600);
  zero=0x00;             // not sure what this is for
}

void loop() {
  if (Serial.available()) {      // Look for char in serial que and process if found
    command = Serial.read();

    if (command == 'Q' || command == 'q') {   
      delay(100);     
      if (Serial.available()) {
        command = Serial.read();
        if (command == 49) {                //If command = "1"  load data to RAM
         WriteRTCRAM();
        }
        else if (command == 50) {      //If command = "2" Read data from RAM
          ReadRTCRAM();
        }
      } 
    }
  }
  command = 0;                 // reset command
  delay(100);
}
//********************************
void WriteRTCRAM(){
    randNumber = random(250);
  Serial.println(randNumber); 

 Wire.beginTransmission(DS1307_I2C_ADDRESS);   // 255 will be the init value and 0 will be considered
          Wire.write(32);                   // Set the register pointer to before data location
          Wire.write(randNumber);              // Write magic number before storing data
          Wire.write(state);                // Store State variable
          Wire.write(counter >> 8);         // writes the high byte of counter
          Wire.write(counter & 0xFF);       // writes the low byte of counter
          Wire.write(randNumber);              // Write magic number after storing data
          Wire.endTransmission();

          Serial.println("Data Stored");
}

void ReadRTCRAM(){
Serial.println("Begin Read");
          Wire.beginTransmission(DS1307_I2C_ADDRESS);
          Wire.write(zero);   // not sure what this is for
          Wire.endTransmission();

          Wire.beginTransmission(DS1307_I2C_ADDRESS);
          Wire.write(0x20);         // not sure what this is doing
          Wire.endTransmission();
          Wire.requestFrom(DS1307_I2C_ADDRESS, 32);  // Set pointer to the first byte of data

          magnum1 = Wire.read();  //magic number written before data write
          state = Wire.read();    // Read state data
          counter = Wire.read() << 8 + Wire.read(); // Read first byte shift left read second byte
          magnum2 = Wire.read();  // Read Magic number written after data write
          Serial.print(" Magic Number = ");
          Serial.println(magnum1);
          Serial.print(" State = ");
          Serial.println(state);
          Serial.print(" Counter = ");
          Serial.println(counter);
          Serial.print(" Magic Number = ");
          Serial.println(magnum2);
          Serial.println(" RTC1307 Dump end");
          if (magnum1 != magnum2){
          Serial.print (" Write Error!!!");
          }
}

This compiles but will not upload

Code:
/*
* Q(1-2) - (Q1) Memory Load  (Q2) RTC - Memory Read 
 
 */
#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68  // This is the I2C address


// Global Variables
long randNumber;
int command = 0;       // This is the command char, in ascii form, sent from the serial port     
byte zero;
int  state   = 96;     // State machine variable
int  counter = 255;      // Master counter variable
int magnum1 = 90;      // dummy var
int magnum2 = 9;      //  dummy var

void setup() {
   randomSeed(analogRead(0));
  Wire.begin();           // join i2c bus
  Serial.begin(57600);
  zero=0x00;             // not sure what this is for
}

void loop() {
  if (Serial.available()) {      // Look for char in serial que and process if found
    command = Serial.read();

    if (command == 'Q' || command == 'q') {   
      delay(100);     
      if (Serial.available()) {
        command = Serial.read();
        if (command == 49) {                //If command = "1"  load data to RAM
         WriteRTCRAM();
        }
        else if (command == 50) {      //If command = "2" Read data from RAM
          ReadRTCRAM();
        }
      } 
    }
  }
  command = 0;                 // reset command
  delay(100);
}
//********************************
void WriteRTCRAM(){
    randNumber = random(250);
  Serial.println(randNumber); 

 Wire.beginTransmission(DS1307_I2C_ADDRESS);   // 255 will be the init value and 0 will be considered
          Wire.write(32);                   // Set the register pointer to before data location
          Wire.write(randNumber);              // Write magic number before storing data
          Wire.write(state);                // Store State variable
          Wire.write(counter >> 8);         // writes the high byte of counter
          Wire.write(counter & 0xFF);       // writes the low byte of counter
          Wire.write(randNumber);              // Write magic number after storing data
          Wire.endTransmission();

          Serial.println("Data Stored");
}

void ReadRTCRAM(){
Serial.println("Begin Read");
          Wire.beginTransmission(DS1307_I2C_ADDRESS);
          Wire.write(zero);   // not sure what this is for
          Wire.endTransmission();

          Wire.beginTransmission(DS1307_I2C_ADDRESS);
          Wire.write(0x20);         // not sure what this is doing
          Wire.endTransmission();
          Wire.requestFrom(DS1307_I2C_ADDRESS, 32);  // Set pointer to the first byte of data

          magnum1 = Wire.read();  //magic number written before data write
          state = Wire.read();    // Read state data
          counter = Wire.read() << 8 + Wire.read(); // Read first byte shift left read second byte
          magnum2 = Wire.read();  // Read Magic number written after data write
          Serial.print(" Magic Number = ");
          Serial.println(magnum1);
          Serial.print(" State = ");
          Serial.println(state);
          Serial.print(" Counter = ");
          Serial.println(counter);
          Serial.print(" Magic Number = ");
          Serial.println(magnum2);
          Serial.println(" RTC1307 Dump end");
         // if (magnum1 != magnum2){
        //  Serial.print (" Write Error!!!");
         // }
}

This compiles and uploads

No error code green progress bar fills up to the last 3 white spaces and holds, to the left it says uploading.
Eventually I get a time out message. Waiting now so I can quote it.
Logged

Space Coast Florida
Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_getsync(): timeout communicating with programmer
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_getsync(): timeout communicating with programmer
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_getsync(): timeout communicating with programmer
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_getsync(): timeout communicating with programmer
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_getsync(): timeout communicating with programmer
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_getsync(): timeout communicating with programmer
avrdude: stk500v2_command(): failed miserably to execute command 0x13
avrdude: stk500v2_paged_write: write command failed
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_getsync(): timeout communicating with programmer
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_getsync(): timeout communicating with programmer
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_getsync(): timeout communicating with programmer
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_getsync(): timeout communicating with programmer
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 652
Posts: 50861
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Does commenting out just the Serial.print() statement make a difference? Does uncommenting that one, and commenting another make a difference?
Logged

Space Coast Florida
Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Strangely yes commenting out the serial print statement did work.
????
Any Idea as to why this should act so?
« Last Edit: January 22, 2013, 12:44:36 pm by urthlight » Logged

Space Coast Florida
Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello???
Logged

Space Coast Florida
Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

"crickets"
Logged

Space Coast Florida
Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

As it turns out !!! is the culprit.
Quote
Actually all arduino mega1280 and mega2560 boards both past and present suffer from the !!! bootloader monitor 'bug'. Doesn't matter what version board or version of IDE, the only fix is to upgrade the bootloader to one that has fixes that and other shortcomings of that bootloader.

Lefty
 
 
 
 
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 27418
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

PaulS, you could not have just mentioned that???

//  Serial.print (" Write Error!!!");
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 652
Posts: 50861
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
PaulS, you could not have just mentioned that???
I guess I could have,,, if I'''d read the whole statement,,, and seen that the statement contained data that was likely to cause problems...

The initial post said that commenting out three statements was required,,, to make the code upload. I was interested in finding out which of the three was the culprit...

As you can see from my reply, three punctuation marks in a row contribute nothing to the information. I don't understand why people feel it necessary to send more data than is necessary.
Logged

Space Coast Florida
Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I don't understand why people feel it necessary to send more data than is necessary.
What is not necessary?
Logged

Pages: 1 [2]   Go Up
Jump to: