Twig_RTC

I am using ATMEGA328 and have TWIG_RTC and TWIG SHIELD on my Arduino.

Try to run the sample code for TWIG_RTC but with no luck.

What I got is the code runs until Wire.endTransmission(); and the code stopped. Comment out the Wire.endTransmission(); line and code stopped at line Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

Any idea??

Hi Sir, how you know that the code runs until Wire.endTransmission()? Have you set the right baudrate? Before you read the time, you should set the time through the serial port first, have you done this?

This is how I know:

{
char len = 0;
len = strlen(cmd);
Wire.beginTransmission(0x35);
while(len–)
{
Wire.send(*(cmd++));
}
Serial.println(“Still able to see this line…”);
Wire.endTransmission(); // stop transmitting
Serial.println(“Never gets here…”);
}
baudrate checked! How do I set the time if the code won’t run?

Hi Sir,
Are you sure the adress here is right:Wire.beginTransmission(0x35)? Please put up all your code so I can check where the problem is. Thanks!

Thanks! Ok, try this:

[code]
#include “Wire.h”
#define DS1307_I2C_ADDRESS 0x68 // This is the I2C address
// 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
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
byte test;

// 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));
Serial.println(“check 3”);
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.send(0x00);
Wire.send(decToBcd(second)); // 0 to bit 7 starts the clock
Wire.send(decToBcd(minute));
Wire.send(decToBcd(hour)); // If you want 12 hour am/pm you need to set
// bit 6 (also need to change readDateDs1307)
Wire.send(decToBcd(dayOfWeek));
Wire.send(decToBcd(dayOfMonth));
Wire.send(decToBcd(month));
Wire.send(decToBcd(year));
Serial.println(“check 4”);
Wire.endTransmission();
Serial.println(“check 5”);//<---------------------------------------------------------See if you could get to this line
}

// Gets the date and time from the ds1307 and prints result
void getDateDs1307()
{
// Reset the register pointer
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.send(0x00);
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());

Serial.print(hour, DEC);
Serial.print(":");
Serial.print(minute, DEC);
Serial.print(":");
Serial.print(second, DEC);
Serial.print(" “);
Serial.print(month, DEC);
Serial.print(”/");
Serial.print(dayOfMonth, DEC);
Serial.print("/");
Serial.print(year, DEC);
Serial.print(" ");
}
void setup() {
Wire.begin();
Serial.begin(57600);
}
void loop() {
delay(2000);
/*T(00-59)(00-59)(00-23)(1-7)(01-31)(01-12)(00-99) - T(sec)(min)(hour)(dayOfWeek)(dayOfMonth)(month)(year) - T Sets the date of the RTC DS1307 Chip.
Example to set the time for 02-DEC-10 @ 19:57:11 for the 3 day of the week, send this command - T1157193021210
*/
Serial.println(“check 1”);
if (Serial.available())
{ // Look for char in serial que and process if found
command = Serial.read();
if (command == 84) { //If command = “T” Set Date
Serial.println(“check 2”);
setDateDs1307();
getDateDs1307();

   Serial.println(" ");
  }
  while(1)
  {
    getDateDs1307();
    delay(500);
  }

}
}[/code]

This is the sample code with few more debug lines.
Thanks for helping.

Hi Sir,
I have tried your code, and no problem. It can work. You should send the setting time command first. I don’t know if you have done this. For example:
To set the time for 02-DEC-10 @ 19:57:11 for the 3rd day of the week, send this command - T1157193021210 using serial monitor
If you have done this, then it may be the cause of the arduino board or the Twig RTC.

You are right! This is really strange. I change the computer and everything works! Why is that? I also tried download 0022 in the new folder but it’s not working so I guess is not the conflict in libraries. Any idea?

It may be the problem of the arduino environment. It is strange for me too.:slight_smile: