1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Micro controller Programming

Discussion in 'Microcontrollers' started by M. Tamilarasan, Nov 6, 2013.

  1. M. Tamilarasan

    M. Tamilarasan Member

    Joined:
    Nov 6, 2013
    Messages:
    53
    Likes:
    0
    Code (c):

    #include <Wire.h>                       // For some strange reasons, Wire.h must be included here
    #include <DS1307new.h>

    // *********************************************
    // DEFINE
    // *********************************************

    // *********************************************
    // VARIABLES
    // *********************************************
    uint16_t startAddr = 0x0000;            // Start address to store in the NV-RAM
    uint16_t lastAddr;                      // new address for storing in NV-RAM
    uint16_t TimeIsSet = 0xaa55;            // Helper that time must not set again

    // *********************************************
    // SETUP
    // *********************************************
    void setup()
    {
      pinMode(2, INPUT);                    // Test of the SQW pin, D2 = INPUT
      digitalWrite(2, HIGH);                // Test of the SQW pin, D2 = Pullup on

      Serial.begin(9600);

    /*
       PLEASE NOTICE: WE HAVE MADE AN ADDRESS SHIFT FOR THE NV-RAM!!!
                      NV-RAM ADDRESS 0x08 HAS TO ADDRESSED WITH ADDRESS 0x00=0
                      TO AVOID OVERWRITING THE CLOCK REGISTERS IN CASE OF
                      ERRORS IN YOUR CODE. SO THE LAST ADDRESS IS 0x38=56!
    */

      RTC.setRAM(0, (uint8_t *)&startAddr, sizeof(uint16_t));// Store startAddr in NV-RAM address 0x08

    /*
       Uncomment the next 2 lines if you want to SET the clock
       Comment them out if the clock is set.
       DON'T ASK ME WHY: YOU MUST UPLOAD THE CODE TWICE TO LET HIM WORK
       AFTER SETTING THE CLOCK ONCE.
    */

    //  TimeIsSet = 0xffff;
    //  RTC.setRAM(54, (uint8_t *)&TimeIsSet, sizeof(uint16_t));

    /*
      Control the clock.
      Clock will only be set if NV-RAM Address does not contain 0xaa.
      DS1307 should have a battery backup.
    */

      RTC.getRAM(54, (uint8_t *)&TimeIsSet, sizeof(uint16_t));
      if (TimeIsSet != 0xaa55)
      {
        RTC.stopClock();
         
        RTC.fillByYMD(2011,4,8);
        RTC.fillByHMS(22,7,0);
     
        RTC.setTime();
        TimeIsSet = 0xaa55;
        RTC.setRAM(54, (uint8_t *)&TimeIsSet, sizeof(uint16_t));
        RTC.startClock();
      }
      else
      {
        RTC.getTime();
      }

    /*
       Control Register for SQW pin which can be used as an interrupt.
    */

      RTC.ctrl = 0x00;                      // 0x00=disable SQW pin, 0x10=1Hz,
                                            // 0x11=4096Hz, 0x12=8192Hz, 0x13=32768Hz
      RTC.setCTRL();

      Serial.println("DS1307 Testsketch");
      Serial.println("Format is \"hh:mm:ss dd-mm-yyyy DDD\"");

      uint8_t MESZ;

      MESZ = RTC.isMEZSummerTime();
      Serial.print("MEZ=0, MESZ=1 : ");
      Serial.println(MESZ, DEC);  
      Serial.println();
    }

    // *********************************************
    // MAIN (LOOP)
    // *********************************************
    void loop()
    {
      RTC.getTime();
      if (RTC.hour < 10)                    // correct hour if necessary
      {
        Serial.print("0");
        Serial.print(RTC.hour, DEC);
      }
      else
      {
        Serial.print(RTC.hour, DEC);
      }
      Serial.print(":");
      if (RTC.minute < 10)                  // correct minute if necessary
      {
        Serial.print("0");
        Serial.print(RTC.minute, DEC);
      }
      else
      {
        Serial.print(RTC.minute, DEC);
      }
      Serial.print(":");
      if (RTC.second < 10)                  // correct second if necessary
      {
        Serial.print("0");
        Serial.print(RTC.second, DEC);
      }
      else
      {
        Serial.print(RTC.second, DEC);
      }
      Serial.print(" ");
      if (RTC.day < 10)                    // correct date if necessary
      {
        Serial.print("0");
        Serial.print(RTC.day, DEC);
      }
      else
      {
        Serial.print(RTC.day, DEC);
      }
      Serial.print("-");
      if (RTC.month < 10)                   // correct month if necessary
      {
        Serial.print("0");
        Serial.print(RTC.month, DEC);
      }
      else
      {
        Serial.print(RTC.month, DEC);
      }
      Serial.print("-");
      Serial.print(RTC.year, DEC);          // Year need not to be changed
      Serial.print(" ");
      switch (RTC.dow)                      // Friendly printout the weekday
      {
        case 1:
          Serial.print("MON");
          break;
        case 2:
          Serial.print("TUE");
          break;
        case 3:
          Serial.print("WED");
          break;
        case 4:
          Serial.print("THU");
          break;
        case 5:
          Serial.print("FRI");
          break;
        case 6:
          Serial.print("SAT");
          break;
        case 7:
          Serial.print("SUN");
          break;
      }
      Serial.print(" seconds since 1.1.2000:");
      Serial.print(RTC.time2000, DEC);
      uint8_t MESZ = RTC.isMEZSummerTime();
      Serial.print(" MEZ=0, MESZ=1 : ");
      Serial.print(MESZ, DEC);
      Serial.print(" - Address in NV-RAM is: ");
      RTC.getRAM(0, (uint8_t *)&lastAddr, sizeof(uint16_t));
      Serial.print(lastAddr, HEX);
      lastAddr = lastAddr + 1;              // we want to use it as addresscounter for example
      RTC.setRAM(0, (uint8_t *)&lastAddr, sizeof(uint16_t));
      RTC.getRAM(54, (uint8_t *)&TimeIsSet, sizeof(uint16_t));
      if (TimeIsSet == 0xaa55)              // check if the clock was set or not
      {
        Serial.println(" - Clock was set!");
      }
      else
      {
        Serial.println(" - Clock was NOT set!");
      }  
      delay(1000);                          // wait a second
    }
     
     
  2. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE
    When it starts... Is this the date and time!!
    Code (c):

    RTC.fillByYMD(2011,4,8);
    RTC.fillByHMS(22,7,0);
     
     
  3. M. Tamilarasan

    M. Tamilarasan Member

    Joined:
    Nov 6, 2013
    Messages:
    53
    Likes:
    0
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE

    Then the ram location 54 isn't being updated.

    Like I said before Adafruits RTClib seems to be a better choice...

    You need to recompile the code without this code

    Code (c):

    RTC.getRAM(54, (uint8_t *)&TimeIsSet, sizeof(uint16_t));
      if (TimeIsSet != 0xaa55)
      {
        RTC.stopClock();
         
        RTC.fillByYMD(2011,4,8);
        RTC.fillByHMS(22,7,0);
     
        RTC.setTime();
        TimeIsSet = 0xaa55;
        RTC.setRAM(54, (uint8_t *)&TimeIsSet, sizeof(uint16_t));
        RTC.startClock();
      }
      else
      {
        RTC.getTime();
      }
     
    Then it will be okay...
     
  6. M. Tamilarasan

    M. Tamilarasan Member

    Joined:
    Nov 6, 2013
    Messages:
    53
    Likes:
    0
    without this line i tried now but no result...i'm not good with programming so please help to solve this issue
     
  7. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE
    Code (c):

    #include <Wire.h>                       // For some strange reasons, Wire.h must be included here
    #include <DS1307new.h>

    // *********************************************
    // DEFINE
    // *********************************************

    // *********************************************
    // VARIABLES
    // *********************************************
    uint16_t startAddr = 0x0000;            // Start address to store in the NV-RAM
    uint16_t lastAddr;                      // new address for storing in NV-RAM
    uint16_t TimeIsSet = 0xaa55;            // Helper that time must not set again

    // *********************************************
    // SETUP
    // *********************************************
    void setup()
    {
      pinMode(2, INPUT);                    // Test of the SQW pin, D2 = INPUT
      digitalWrite(2, HIGH);                // Test of the SQW pin, D2 = Pullup on

      Serial.begin(9600);

    /*
       PLEASE NOTICE: WE HAVE MADE AN ADDRESS SHIFT FOR THE NV-RAM!!!
                      NV-RAM ADDRESS 0x08 HAS TO ADDRESSED WITH ADDRESS 0x00=0
                      TO AVOID OVERWRITING THE CLOCK REGISTERS IN CASE OF
                      ERRORS IN YOUR CODE. SO THE LAST ADDRESS IS 0x38=56!
    */

      RTC.setRAM(0, (uint8_t *)&startAddr, sizeof(uint16_t));// Store startAddr in NV-RAM address 0x08

    /*
       Uncomment the next 2 lines if you want to SET the clock
       Comment them out if the clock is set.
       DON'T ASK ME WHY: YOU MUST UPLOAD THE CODE TWICE TO LET HIM WORK
       AFTER SETTING THE CLOCK ONCE.
    */

    //  TimeIsSet = 0xffff;
    //  RTC.setRAM(54, (uint8_t *)&TimeIsSet, sizeof(uint16_t));

    /*
      Control the clock.
      Clock will only be set if NV-RAM Address does not contain 0xaa.
      DS1307 should have a battery backup.
    */

        RTC.getTime(); // <---- Just this line
     

    /*
       Control Register for SQW pin which can be used as an interrupt.
    */

      RTC.ctrl = 0x00;                      // 0x00=disable SQW pin, 0x10=1Hz,
                                            // 0x11=4096Hz, 0x12=8192Hz, 0x13=32768Hz
      RTC.setCTRL();

      Serial.println("DS1307 Testsketch");
      Serial.println("Format is \"hh:mm:ss dd-mm-yyyy DDD\"");

      uint8_t MESZ;

      MESZ = RTC.isMEZSummerTime();
      Serial.print("MEZ=0, MESZ=1 : ");
      Serial.println(MESZ, DEC);  
      Serial.println();
    }

    // *********************************************
    // MAIN (LOOP)
    // *********************************************
    void loop()
    {
      RTC.getTime();
      if (RTC.hour < 10)                    // correct hour if necessary
      {
        Serial.print("0");
        Serial.print(RTC.hour, DEC);
      }
      else
      {
        Serial.print(RTC.hour, DEC);
      }
      Serial.print(":");
      if (RTC.minute < 10)                  // correct minute if necessary
      {
        Serial.print("0");
        Serial.print(RTC.minute, DEC);
      }
      else
      {
        Serial.print(RTC.minute, DEC);
      }
      Serial.print(":");
      if (RTC.second < 10)                  // correct second if necessary
      {
        Serial.print("0");
        Serial.print(RTC.second, DEC);
      }
      else
      {
        Serial.print(RTC.second, DEC);
      }
      Serial.print(" ");
      if (RTC.day < 10)                    // correct date if necessary
      {
        Serial.print("0");
        Serial.print(RTC.day, DEC);
      }
      else
      {
        Serial.print(RTC.day, DEC);
      }
      Serial.print("-");
      if (RTC.month < 10)                   // correct month if necessary
      {
        Serial.print("0");
        Serial.print(RTC.month, DEC);
      }
      else
      {
        Serial.print(RTC.month, DEC);
      }
      Serial.print("-");
      Serial.print(RTC.year, DEC);          // Year need not to be changed
      Serial.print(" ");
      switch (RTC.dow)                      // Friendly printout the weekday
      {
        case 1:
          Serial.print("MON");
          break;
        case 2:
          Serial.print("TUE");
          break;
        case 3:
          Serial.print("WED");
          break;
        case 4:
          Serial.print("THU");
          break;
        case 5:
          Serial.print("FRI");
          break;
        case 6:
          Serial.print("SAT");
          break;
        case 7:
          Serial.print("SUN");
          break;
      }
      Serial.print(" seconds since 1.1.2000:");
      Serial.print(RTC.time2000, DEC);
      uint8_t MESZ = RTC.isMEZSummerTime();
      Serial.print(" MEZ=0, MESZ=1 : ");
      Serial.print(MESZ, DEC);
      Serial.print(" - Address in NV-RAM is: ");
      RTC.getRAM(0, (uint8_t *)&lastAddr, sizeof(uint16_t));
      Serial.print(lastAddr, HEX);
      lastAddr = lastAddr + 1;              // we want to use it as addresscounter for example
      RTC.setRAM(0, (uint8_t *)&lastAddr, sizeof(uint16_t));
      RTC.getRAM(54, (uint8_t *)&TimeIsSet, sizeof(uint16_t));
      if (TimeIsSet == 0xaa55)              // check if the clock was set or not
      {
        Serial.println(" - Clock was set!");
      }
      else
      {
        Serial.println(" - Clock was NOT set!");
      }  
      delay(1000);                          // wait a second
    }
     
     
  8. M. Tamilarasan

    M. Tamilarasan Member

    Joined:
    Nov 6, 2013
    Messages:
    53
    Likes:
    0
    upload_2014-1-9_15-18-27.png
    Result for this coding....
     
  9. M. Tamilarasan

    M. Tamilarasan Member

    Joined:
    Nov 6, 2013
    Messages:
    53
    Likes:
    0
    Time ans Date not set..
     
  10. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE
  11. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE
    It doesn't look like the clock is running atall in you post!!!

    Is the seconds counting... I can't see the picture properly..
     
  12. M. Tamilarasan

    M. Tamilarasan Member

    Joined:
    Nov 6, 2013
    Messages:
    53
    Likes:
    0
    no response.....Please help with apt coding...i'm using RTC & EEPROM board from NSK Electronics
     
  13. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE
    Show me how you have it connected.... Do me a schematic of the connections to the arduino...
     
  14. M. Tamilarasan

    M. Tamilarasan Member

    Joined:
    Nov 6, 2013
    Messages:
    53
    Likes:
    0
    connected using I2c protocol
    RTC Board Arduino Uno
    SDA - SDA
    SCL - SCL
    5V - 5V
    GND - GND
     
  15. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE
    Analogue pins 4 and 5 on the Arduino?
     
  16. M. Tamilarasan

    M. Tamilarasan Member

    Joined:
    Nov 6, 2013
    Messages:
    53
    Likes:
    0
    ya correct..
     
  17. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE
    I should be working then... Has it worked before that last code mod... Or does it always show 0.0.0.0. for the time..
     
  18. M. Tamilarasan

    M. Tamilarasan Member

    Joined:
    Nov 6, 2013
    Messages:
    53
    Likes:
    0
    before modifying the code-once the clock sets the time starts to run but when the external power turned off the clock reset to 0.0.0
    After the modification of code the clock displays 0.0.0 only...
     
  19. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    ONLINE
    Are you sure the external battery is giving +3v on pin 3 of the DS1307... It seems as if there is no backup then...
     
  20. M. Tamilarasan

    M. Tamilarasan Member

    Joined:
    Nov 6, 2013
    Messages:
    53
    Likes:
    0
    ya checked battery power coming to DS1307 pin 3
     
  21. M. Tamilarasan

    M. Tamilarasan Member

    Joined:
    Nov 6, 2013
    Messages:
    53
    Likes:
    0
    Thanks!!! Rogers for your support...I Solved the RTC problem...The problem was due to the frequent interrupt in power going to 3rd pin of DS1307.
     

Share This Page