Go Down

Topic: If you have an arduino mega 2560 and a ds1307 could you test some code? (Read 2585 times) previous topic - next topic

urthlight

I have some weird code that I would like someone else to run to see if you get the same results.
Code: [Select]
/*
* 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: [Select]
/*
* 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.


Quote
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

Krodal

The Arduino Mega is know for having issues with uploading.
It depends on the version of the Arduino software and the versions on the board.
I read once that 3 certain bytes in the binary sketch could be recognized as something special, but I can't find it anymore.

Are you using Arduino 1.0.3 ?
Can you try version 1.5.1r2 ? On the main site, select "download software" and follow the link "If you have the new Due Board click here".

This is the normal trouble shooting: http://arduino.cc/en/Guide/Troubleshooting
This is a very long thread about it: http://arduino.cc/forum/index.php?topic=83079.0


Riva


The Arduino Mega is know for having issues with uploading.
It depends on the version of the Arduino software and the versions on the board.
I read once that 3 certain bytes in the binary sketch could be recognized as something special, but I can't find it anymore.

I think the 3 bytes are !!!

urthlight

Riva you are correct. Go figure that I would stumble upon the three bytes that will hang it. Thanks for the information. You sir, rock!!!

retrolefty

Quote
The Arduino Mega is know for having issues with uploading.
It depends on the version of the Arduino software and the versions on the board.


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

urthlight

Cool, Mystery solved.
This is my first serious project in the arduino arena.  I am past the issue that brought me here.Thanks guys. I really needed an answer.I dont like a mystery, and the Serial.print statement stopping the compilation had me befuddled. I use "!!!" as a visual flag all of the time. Who woulda thunk it would bite me like this. But this only makes me wonder about other "easter eggs" might be in the mega. I am building a comercial product.
Quote

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.

Is there a list of the other shorcomings That I need to watch out for. And I will look into upgrading the bootloader.
Also.
Is there some way to set the runtime clock other than waiting 49 days for the clock to turn over. I need to certify that the rollover will be uneventfull.


just for giggles.



Go Up