CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Problem with ds1307

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
planmas



Joined: 18 Aug 2011
Posts: 8

View user's profile Send private message

Problem with ds1307
PostPosted: Thu Sep 01, 2011 11:08 am     Reply with quote

Hello,

The driver works like a charm Very Happy but i still have one problem.
http://www.ccsinfo.com/forum/viewtopic.php?t=23255

RTC's 3.0V battery, should work like a backup battery in case of powering down the main circuit, right?

So, i have my rtc in a circuit with a PIC (obviously) and the positive pole of the battery connected to VBAT and the negative pole connected to GND (GND is common to the entire circuit). If i leave the connection like this, the values of my rtc are not correct, and are always the same, if i connect VBAT to GND it works well, also works well if i switch the poles of the battery, meaning, VBAT to negative pole, GND to positive pole, but when i shut down the main power source the rtc does not keeps on counting, it resets it self.

In resume:

Battery poles connected right (like the datasheet of DS1307 suggests): RTC does not count.

Battery poles connected inverted: RTC count while the main power source is connected.

Does anyone has any idea why this happens?

Best Wishes

Planmas
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Sep 01, 2011 2:10 pm     Reply with quote

You connected the battery backwards. You probably blew out the Vbat
circuit inside the ds1307. Never connect the power backwards on any IC.
planmas



Joined: 18 Aug 2011
Posts: 8

View user's profile Send private message

PostPosted: Fri Sep 02, 2011 7:34 am     Reply with quote

Ok, that might have happened...

New DS1307, new battery, same result...

The output is:

45-25--1 85 45:85:85

I have made some search in this forum about this problem, but have found no solution.

My main.c is: (don't mind the comments)

Code:


#include <18F26K20.h>

#fuses HS, NOWDT,NOPROTECT, NOLVP

#use delay(clock=20000000)
#use rs232(uart1,baud=9600,stream=PC,errors)

#include <ds1307.c>

void main()
{
  BYTE dia,mes,ano,dow,hora,min,seg;
 
  ds1307_init();
 
  // Set date for -> 15 June 2005 Tuesday
  // Set time for -> 15:20:55
 
  ds1307_set_date_time(31,12,9,2,23,59,55);
 
  while(1)
  {
    delay_ms(1000);
   
    ds1307_get_date(dia,mes,ano,dow);
    ds1307_get_time(hora,min,seg);
   
    fprintf(PC,"%02d-%02d-%02d   %02d  %02d:%02d:%02d",dia,mes,ano,dow,hora,min,seg);
    fprintf(PC,"\r\n");

  }
}



and my ds1307.c is:

Code:


////////////////////////////////////////////////////////////////////////////////
///                               DS1307.C                                   ///
///                     Driver for Real Time Clock                           ///
///                                                                          ///
/// ds1307_init() - Enable oscillator without clearing the seconds register -///
///                 used when PIC loses power and DS1307 run from 3V BAT     ///
///               - Disable squarewave output                                ///
///                                                                          ///
/// ds1307_set_date_time(day,mth,year,dow,hour,min,sec)  Set the date/time   ///
///                                                                          ///
/// ds1307_get_date(day,mth,year,dow)               Get the date             ///
///                                                                          ///
/// ds1307_get_time(hr,min,sec)                     Get the time             ///
///                                                                          ///
////////////////////////////////////////////////////////////////////////////////

#define RTC_SDA  PIN_C4
#define RTC_SCL  PIN_C3

#use i2c(master, sda=RTC_SDA, scl=RTC_SCL)

BYTE bin2bcd(BYTE binary_value);
BYTE bcd2bin(BYTE bcd_value);

void ds1307_init(void)
{
   BYTE seconds = 0;

   i2c_start();
   i2c_write(0xD0);      // WR to RTC
   i2c_write(0x00);      // REG 0
   i2c_start();
   i2c_write(0xD1);      // RD from RTC
   seconds = bcd2bin(i2c_read(0)); // Read current "seconds" in DS1307
   i2c_stop();
   seconds &= 0x7F;

   delay_us(3);

   i2c_start();
   i2c_write(0xD0);      // WR to RTC
   i2c_write(0x00);      // REG 0
   i2c_write(bin2bcd(seconds));     // Start oscillator with current "seconds value
   i2c_start();
   i2c_write(0xD0);      // WR to RTC
   i2c_write(0x07);      // Control Register
   i2c_write(0x80);     // Disable squarewave output pin
   i2c_stop();

}

void ds1307_set_date_time(BYTE day, BYTE mth, BYTE year, BYTE dow, BYTE hr, BYTE min, BYTE sec)
{
  sec &= 0x7F;
  hr &= 0x3F;

  i2c_start();
  i2c_write(0xD0);            // I2C write address
  i2c_write(0x00);            // Start at REG 0 - Seconds
  i2c_write(bin2bcd(sec));      // REG 0
  i2c_write(bin2bcd(min));      // REG 1
  i2c_write(bin2bcd(hr));      // REG 2
  i2c_write(bin2bcd(dow));      // REG 3
  i2c_write(bin2bcd(day));      // REG 4
  i2c_write(bin2bcd(mth));      // REG 5
  i2c_write(bin2bcd(year));      // REG 6
  i2c_write(0x80);            // REG 7 - Disable squarewave output pin
  i2c_stop();
}

void ds1307_get_date(BYTE &day, BYTE &mth, BYTE &year, BYTE &dow)
{
  i2c_start();
  i2c_write(0xD0);
  i2c_write(0x03);            // Start at REG 3 - Day of week
  i2c_start();
  i2c_write(0xD1);
  dow  = bcd2bin(i2c_read() & 0x7f);   // REG 3
  day  = bcd2bin(i2c_read() & 0x3f);   // REG 4
  mth  = bcd2bin(i2c_read() & 0x1f);   // REG 5
  year = bcd2bin(i2c_read(0));            // REG 6
  i2c_stop();
}

void ds1307_get_time(BYTE &hr, BYTE &min, BYTE &sec)
{
  i2c_start();
  i2c_write(0xD0);
  i2c_write(0x00);            // Start at REG 0 - Seconds
  i2c_start();
  i2c_write(0xD1);
  sec = bcd2bin(i2c_read() & 0x7f);
  min = bcd2bin(i2c_read() & 0x7f);
  hr  = bcd2bin(i2c_read(0) & 0x3f);
  i2c_stop();

}

BYTE bin2bcd(BYTE binary_value)
{
  BYTE temp;
  BYTE retval;

  temp = binary_value;
  retval = 0;

  while(1)
  {
    // Get the tens digit by doing multiple subtraction
    // of 10 from the binary value.
    if(temp >= 10)
    {
      temp -= 10;
      retval += 0x10;
    }
    else // Get the ones digit by adding the remainder.
    {
      retval += temp;
      break;
    }
  }

  return(retval);
}


// Input range - 00 to 99.
BYTE bcd2bin(BYTE bcd_value)
{
  BYTE temp;

  temp = bcd_value;
  // Shifting upper digit right by 1 is same as multiplying by 8.
  temp >>= 1;
  // Isolate the bits for the upper digit.
  temp &= 0x78;

  // Now return: (Tens * 8) + (Tens * 2) + Ones

  return(temp + (temp >> 2) + (bcd_value & 0x0f));
}




I don't know where more to pickup...

Thanks



EDIT: By the way, I have the PIC and RTC powered with 3.3V on VCC pin.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Fri Sep 02, 2011 8:41 am     Reply with quote

Quote:
By the way, I have the PIC and RTC powered with 3.3V on VCC pin

Did you read the datasheet? DS1307 minimal supply voltage is specified with 4.5 V.
asmallri



Joined: 12 Aug 2004
Posts: 1635
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Fri Sep 02, 2011 9:36 am     Reply with quote

FvM wrote:
Quote:
By the way, I have the PIC and RTC powered with 3.3V on VCC pin

Did you read the datasheet? DS1307 minimal supply voltage is specified with 4.5 V.


I tested this configuration a few years ago and the DS1307 will not work with a VCC 3.3 volts.
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
deltatech



Joined: 22 Apr 2006
Posts: 87

View user's profile Send private message

PostPosted: Fri Sep 02, 2011 5:16 pm     Reply with quote

Hi Just to let you I have used DS1307 but this IC is horrendously inaccurate minimum 8 seconds plus per day
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Sat Sep 03, 2011 2:08 am     Reply with quote

The DS1307, is as accurate as the time source you use to feed it. No better, no worse.
Get a good crystal, make sure the PCB layout is designed to minimise stray capacitance, and pick-up from other signal sources, and if necessary trim the crystal (hint this is the difference between something like a digital Omega watch, and a cheap Japanese unit - they actually take the time to adjust the crystal frequency.....).
Now, 'watch' crystals in watches, give better accuracy than they do in general, because worn on the wrist, the temperature is better controlled. The standard watch crystal, will normally be offered with +/- 20ppm accuracies, and if maintained at around 30C, will give accuracies of perhaps 0.5 seconds/day. However stick the same unit into a bit of kit that experiences temperatures down close to freezing, or up to say 40C, and accuracies _will_ plummet. You also have to be careful when sourcing watch crystals, as many are deliberately made to run fast rather than slow, with the standard 'setup' proceedure on the unit being to measure how many extra counts it produces in a day, and then store this as a calibration constant to 'zero' the clock.
Using crystals in a reasonably temperature controlled environment (control booth for a pumping system), trimming the crystal at installation (using the master clock from Rugby in the UK), I have seen the DS1307, manage better than a minute a year.

Best Wishes
Douglas Kennedy



Joined: 07 Sep 2003
Posts: 755
Location: Florida

View user's profile Send private message AIM Address

PostPosted: Sat Sep 03, 2011 8:48 am     Reply with quote

The ds1307 spec sheet has the battery VBAT specified as Typical 3v max 3.5v. This I hope means that with an interruption of power at 5v the DS1307 would keep time via the battery until power at 5v was restored. While on battery all the DS1307 would do is keep time and the ram alive.
deltatech



Joined: 22 Apr 2006
Posts: 87

View user's profile Send private message

PostPosted: Sun Sep 04, 2011 8:43 am     Reply with quote

I followed PCB layout guidelines to the letter. I used a 20ppm crystal, I used decoupling caps for the power supply but still the clock was running 8 seconds too fast per day.

Tuning each crystal requires frequency counters and it is time consuming and not practical for every PCB.

In the end I had to install MSF receivers to synch the clocks, there was no other option. It was dumped after the first PCB batch.

It is best to buy an RTC ic with a tuned internal crystal the cost is not that much difference as DS1307 is overpriced anyway.

DS1307 belongs to the steam age.
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Sun Sep 04, 2011 9:38 am     Reply with quote

You say '20ppm' crystals, but not what loading caps they were designed for?. The point about the crystals for the DS1307, is that they should be designed for a Cl of 12.5pF. If you then follow their recommended board layout for the crystal connections (which has short lines, and a surrounding protection ring, then a ground plane outside this), the loading is very close to spec, and should do significantly better than 8 seconds per day. That your results were this bad, suggests either crystals designed for a different Cl, or PCB layout that didn't follow the recommendations.

Best Wishes
planmas



Joined: 18 Aug 2011
Posts: 8

View user's profile Send private message

PostPosted: Wed Sep 21, 2011 7:24 am     Reply with quote

Hi

I'm using now DS1338 (the driver for the 1307 works for this one) with 3.3V and its working well, but i have detected a problem.

To power the entire circuit I have a dual analog power supply. I set the voltage to 5V and then this 5V are regulated to power the rest of the circuit to 3.3V. So far its good.

But if I turn off the power supply with the voltage level to 5V, the RTC no longer works properly, it keeps sending wrong values.

If I lower the voltage to 0 and then turn off the power it works well the next time i turn the power on.

Am I toasting the RTC? Is the power supply with a transient behavior?

Ideas?


Thank you

planmas
temtronic



Joined: 01 Jul 2010
Posts: 9243
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Sep 21, 2011 12:05 pm     Reply with quote

If you're running the PIC at 5 volts and the RTC at 3.3, then you should have logic level translators between the two devices.
OR
You'll have to use an L version PIC( Low voltage capable).

I doubt that a 5volt PIC will correctly read/write to the RTC without proper level shifting.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group