|
|
View previous topic :: View next topic |
Author |
Message |
simons
Joined: 15 Jan 2009 Posts: 18
|
Problems with DS1307 |
Posted: Thu Feb 12, 2009 4:24 pm |
|
|
Hello to all I have 2 devices on a i2c bus, one pcf8574 to drive a lcd and a ds1307 ..
I'm using this driver I found on this forum:
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(0x10); // 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
delay_us(1);
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_write(0x10);
i2c_stop();
}
void ds1307_get_date(BYTE &day, BYTE &mth, BYTE &year, BYTE &dow)
{
i2c_start();
i2c_write(0xD0);
delay_us(1);
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));
}
|
But every time I send the get_date function I get ALWAYS 45 for hours, 85 for minutes and 85 for seconds, also I get 45 for day, 25 for month and -91 for year...
this is the main function:
Code: |
ds1307_init();
ds1307_set_date_time(15,6,5,2,15,20,55);
while(1)
{
ds1307_get_time(hour, minutes,seconds);
delay_us(2);
ore=13;
printf(i2c_LcdSendChar,"\f%02d%02d%02d",hour, minutes,seconds);
delay_ms(1000);
ds1307_get_date(day,month,year,dow);
delay_us(2);
printf(i2c_LcdSendChar,"\f%02d%02d%02d",day,month,year);
}
|
help me please!!! this thing is driving me nuts!!!
I'm using a resistor partitor to have 3,45v on pin Vbat of ds1307 because i don't have a 3v lithium battery..
I also enabled the 1hz output with a 2.2K pullup on sqout pin with a led wired to ground but nothing happens.. the led is always off..
:( :( |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
simons
Joined: 15 Jan 2009 Posts: 18
|
|
Posted: Fri Feb 13, 2009 5:47 pm |
|
|
The voltage on Vbat pin is 3.45V set by 2 resistors because i don't have a battery...
I checked every thing and it doesn't work.. :( |
|
|
simons
Joined: 15 Jan 2009 Posts: 18
|
|
Posted: Fri Feb 13, 2009 6:14 pm |
|
|
I tried to enable the swqe , I have a 1k pullup on swout pin and a led wired with a resistor from the sqout pin to gnd... the led is still off..
I think the problem is with the crystal... but even the set_time function doesn't work... |
|
|
simons
Joined: 15 Jan 2009 Posts: 18
|
|
Posted: Wed Feb 18, 2009 5:55 am |
|
|
I have another question.. do you think that i've damaged the rtc by connecting the vbat to +5v?
I noticed another thing:
I tried to call the "get_time" function with the rtc disconnected and it also gives 45:85:85 ...
what am i doing wrong????? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
simons
Joined: 15 Jan 2009 Posts: 18
|
|
Posted: Wed Feb 18, 2009 1:58 pm |
|
|
I ran that code and the RTC is correctly detected at address D0 ... I have 2 i2c devices and both are detected correctly...
Now I'm getting another behaviour: I read the time every second, and now I get this:
45:85:00
45:85:01
45:85:00
45:85:03
45:85:85
and sometimes :45 or :25 an so on...
Am I missing something? the internal oscillator seems no to be working because the led connected to sqwout is not blinking... |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Feb 18, 2009 3:13 pm |
|
|
Quote: | Now i'm getting another behaviour: I read the time every second, and now I get this:
45:85:00
45:85:01
45:85:00
45:85:03
45:85:85 |
I think you're using a different program than what you posted. Because
look at your code below. It doesn't have any colons ':' in the printf
statements.
Quote: | while(1)
{
ds1307_get_time(hour, minutes,seconds);
delay_us(2);
ore=13;
printf(i2c_LcdSendChar,"\f%02d%02d%02d",hour, minutes,seconds); delay_ms(1000);
ds1307_get_date(day,month,year,dow);
delay_us(2);
printf(i2c_LcdSendChar,"\f%02d%02d%02d",day,month,year); } |
Post your true actual test program. Also post the #include statement
for the PIC, and the #fuses, and the #use delay() statement.
Post your compiler version. |
|
|
simons
Joined: 15 Jan 2009 Posts: 18
|
|
Posted: Wed Feb 18, 2009 3:28 pm |
|
|
The code i posted is an adaptation for my lcd.. i have a 1x6 characters lcd so for a simple test i was printing the time without the colons
these are my includes:
Code: | #include "lcd_new.h" //My own lcd library
#include "ONEWIRE.C" //one wire routines
#include "DS1820.C" //1-wire temp-sensor by dallas
#include "LCD_I2C.C" //my own I2C driver for an lcd
#include "DS1307.C" //The driver for ds1307 by dallas |
these are the fuses:
Code: | #include <16F876A.h>
#device adc=8
#use delay(clock=20000000)
#fuses NOWDT,HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8, ERRORS)
#define RTC_SDA PIN_C4
#define RTC_SCL PIN_C3
#use i2c(master, sda=RTC_SDA, scl=RTC_SCL, SLOW) |
the compiler version is 3.181 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Feb 18, 2009 3:42 pm |
|
|
I installed vs. 3.181. I don't have a 16F876A, so I used a 16F877A.
I modified the two printf lines to display on a terminal window instead
of an LCD. I removed the \f and put in \n\r as shown below.
Code: | printf("%02d%02d%02d ",hour, minutes,seconds);
delay_ms(1000);
ds1307_get_date(day,month,year,dow);
delay_us(2);
printf("%02d%02d%02d \n\r",day,month,year); |
Here's what I got:
Quote: |
152055 150605
152056 150605
152057 150605
152058 150605
152059 150605
152100 150605
152101 150605
152102 150605
152103 150605
152104 150605
152105 150605
152106 150605 |
That's hours, minutes, seconds and then month, day, year.
You can see the seconds change from 55, to 56, up to 00, and then
start counting 01, 02, 03, etc. It's working.
I have 4.7K pull-ups on the SDA and SCL lines. I have a 3.35v lithium
battery on the Vbat pin. The Vdd is at +5v. There is a 32.768 KHz
watch crystal on the xtal pins of the DS1307.
If it's not working for you, then it's likely there is a problem with the
hardware. If you've got another device on the same i2c bus, you
should remove it. Carefully check your connections. Make sure you
really have a watch crystal of the correct frequency on the DS1307.
Don't use a 32 MHz crystal, for example. |
|
|
simons
Joined: 15 Jan 2009 Posts: 18
|
|
Posted: Wed Feb 18, 2009 3:51 pm |
|
|
I bought the rtc and the crystals on ebay .. the auction said 32.768Khz watch crystals.. i have this code printed on it: KDS7B ..
i tried also disconnecting the other device on i2c bus..
The voltage on supply pin i s 5.04v and the vbat pin is 3.45V (set by 2 resistors, i don't have a battery right now)
I tried also to ground the vbat pin ,as described in the datasheet, when the backup supply is not needed.. but nothing changes..
should the rtc set and get time even if there's no crystal?? |
|
|
simons
Joined: 15 Jan 2009 Posts: 18
|
|
Posted: Wed Feb 18, 2009 4:09 pm |
|
|
Now i'm starting to think that the RTC is faulty.. now it isn't detected by the test program you posted before...
:( I'll try some others hardware configurations |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Feb 18, 2009 4:15 pm |
|
|
I have the DS1307 in a DIP socket so I just bent the two crystal pins
on the chip outwards so they hang over the side of the socket and don't
connect to the crystal. Then I ran the same program. Here's what I got:
Quote: |
152055 150605
152055 150605
152055 150605
152055 150605
152055 150605
152055 150605
152055 150605
152055 150605
|
The seconds are not increasing. They stay at "55". |
|
|
simons
Joined: 15 Jan 2009 Posts: 18
|
|
Posted: Wed Feb 18, 2009 4:18 pm |
|
|
Well.. now i think it's my IC that is damaged or faulty.. with or without the crystal it returns the same values and now it isn't detected by the test routine...
I ordered another ic.. I'll let you know!
thanks for help anyway!!! |
|
|
simons
Joined: 15 Jan 2009 Posts: 18
|
|
Posted: Tue Feb 24, 2009 11:28 am |
|
|
today i tested my new ds1307 and it's doing the same as before.... now when i try to set date time it returns 45:85:85.. when i read the seconds i get:
:00
:01
:01
:00
:03
:45
i really don't know!! now i have a lithium battery on pin Vbat of ds1307.. tested and it's 3.3V brand new. |
|
|
|
|
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
|