DS1307 can not read and write time in arduino.

Discussion in 'Embedded Systems and Microcontrollers' started by dreamtale, Dec 2, 2014.

  1. dreamtale

    Thread Starter New Member

    Oct 30, 2014
    24
    1
    hello everyone
    In mid-November, I have designed a shield based on arduino.I have wrote two posts.http://forum.allaboutcircuits.com/t...-back-integrates-a-variety-of-sensors.103659/ and http://forum.allaboutcircuits.com/threads/arduino-shield-with-magnetometer-clock-and-oled.103193/ I have to debug a week.However, it does not work.The OLED display and bluetooth is ok.
    Here is my sch,
    20141202175414.jpg
    Here is my pcb
    IMG_0492.JPG
    Code (Text):
    1. void setup() {
    2.   Wire.begin();
    3.   Serial.begin(57600);
    4. }
    5.  
    6. void loop() {
    7.   if (Serial.available()) {  // Look for char in serial que and process if found
    8.     command = Serial.read();
    9.     if (command == 84) {      //If command = "T" Set Date
    10.       setDateDs1307();
    11.       getDateDs1307();
    12.       Serial.println(" ");
    13.     }
    14.     else if (command == 81) {  //If command = "Q" RTC1307 Memory Functions
    15.       delay(100);  
    16.       if (Serial.available()) {
    17.         command = Serial.read();
    18.      
    19.         // If command = "1" RTC1307 Initialize Memory - All Data will be set to 255 (0xff).
    20.         // Therefore 255 or 0 will be an invalid value.
    21.         if (command == 49) {
    22.        
    23.           // 255 will be the init value and 0 will be cosidered an error that
    24.           // occurs when the RTC is in Battery mode.
    25.           Wire.beginTransmission(clockAddress);
    26.        
    27.           // Set the register pointer to be just past the date/time registers.
    28.           Wire.write(byte(0x08));
    29.           for (int i = 1; i <= 27; i++) {
    30.             Wire.write(byte(0xff));
    31.             delay(100);
    32.           }
    33.           Wire.endTransmission();
    34.           getDateDs1307();
    35.           Serial.println(": RTC1307 Initialized Memory");
    36.         }
    37.         else if (command == 50) {      //If command = "2" RTC1307 Memory Dump
    38.           getDateDs1307();
    39.           Serial.println(": RTC 1307 Dump Begin");
    40.           Wire.beginTransmission(clockAddress);
    41.           Wire.write(byte(0x00));
    42.           Wire.endTransmission();
    43.           Wire.requestFrom(clockAddress, 64);
    44.           for (int i = 1; i <= 64; i++) {
    45.             test = Wire.read();
    46.             Serial.print(i);
    47.             Serial.print(":");
    48.             Serial.println(test, DEC);
    49.           }
    50.           Serial.println(" RTC1307 Dump end");
    51.         }
    52.       }
    53.     }
    54.     Serial.print("Command: ");
    55.     Serial.println(command);  // Echo command CHAR in ascii that was sent
    56.   }
    57.  
    58.   command = 0;  // reset command                
    59.   delay(100);
    60. }
    Time is always parked in the 45:85:85,I know this is the wrong data.I use two leonardo to test.Unfortunately, I still did not get the correct data.Then,I suspect that the IIC on my leonardo is broken.
    I've used other IIC devices, I put GY-30 connected to leonardo, and the code I‘ve been used,it can't work.I borrowed a Duemilanove from others.The GY-30 can work,and the DS1307 in my shield doesn't work.Now I have no idea.anyone can help me,and anyone can upload code about DS1307 base on arduino,In order to confirm the problem in my code or on my PCB.
    Thanks
    dreamtale
     
  2. Papabravo

    Expert

    Feb 24, 2006
    10,177
    1,799
    The problem is obvious to even a casual observer. SDA must be bi-directional on a bit by bit basis so that the ACK pulse from the device can get back to the master. Your use of a FET as a level translator has borked the protocol.
     
  3. dreamtale

    Thread Starter New Member

    Oct 30, 2014
    24
    1
    Thanks,I don't think so,I do not konw why you said that FET as a level translator borked the protocol.but the FET also use in bluttooth,The bluetooth is OK.Can you tell me clear?
     
  4. Papabravo

    Expert

    Feb 24, 2006
    10,177
    1,799
    What makes you think I2C and Bluetooth are related? Do you understand that the SDA line (Net A4) needs to pass signals in both directions during a transfer so that the slave device can ACK (acknowledge) the transmisstions of the master. Any circuit element like a FET or an FPGA that does not allow signals to propagete in both directions will bork the I2C protocol. But hey if you're so smart why are you asking me?
     
  5. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,395
    1,607
    Now gentlemen, let's, play nice. :p

    The level translators look similar to a Sparkfun design listed as "bi directional." I believe there exists a Phillips app note describing similar circuits. They do require very low threshold devices, so careful if you substuited for that Ap2306

    I say "similar" as Sparkfun's schematic has no series resistor in the gate. Beyond slowing things down I don't see the need for that part.

    dreamtale: I2C has no minimum speed. That means you do not need anything beyond a voltmeter (or a pair of LEDs) to debug the interface. So write some simple I2C code where you can watch the transactions bit by bit to see what is going back and fourth.

    The first critical bit of information is the ACK bit from slave to master after the first byte containing the slave address. If that is not there, nothing else following will work.
     
  6. dreamtale

    Thread Starter New Member

    Oct 30, 2014
    24
    1
    well,About some of the features mos tube, I did not fully understand. Ignore other reasons, the level is between RTC and arduino compatible. I will tear down AP2306, and the pull-up resistor change for 4.7K. I'll test it as soon as possible.however,Thanks.
     
  7. dreamtale

    Thread Starter New Member

    Oct 30, 2014
    24
    1
    Yeah,Thanks,If there are no FET, DS1307 to work properly.
     
  8. dreamtale

    Thread Starter New Member

    Oct 30, 2014
    24
    1
    Well,If there are no FET, DS1307 can work properly.I'm sure I made a mistake should not be
     
  9. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,395
    1,607
    What is the part number of the actual FET you used?
     
  10. dreamtale

    Thread Starter New Member

    Oct 30, 2014
    24
    1
    It's AP2306.
     
Loading...