|
|
View previous topic :: View next topic |
Author |
Message |
Davsgr
Joined: 08 May 2010 Posts: 3
|
DS1307 problem I2C |
Posted: Sat May 08, 2010 8:49 am |
|
|
Hi
I use a 16F887.
This is my code for write and read the first address of the DS1307 :
Code: |
#use i2c (Master, sda=PIN_C4, SCL=PIN_C3)
int heure;
main()
{
heure=0;
i2c_start();
i2c_write(0xD0);
i2c_write(0x00);
i2c_write(0x26);
i2c_stop();
i2c_start();
i2c_write(0xd0);
i2c_write(0x00);
i2c_start();
i2c_write(0xd1);
heure = i2c_read(0);
i2c_stop();
}
|
Normally the variable "heure" should be 0x26 but it indicates 0xFF.
is the code wrong ?
Thanks for your Help and sorry for my english . |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
Davsgr
Joined: 08 May 2010 Posts: 3
|
|
Posted: Sat May 08, 2010 2:10 pm |
|
|
I have see this thread but I have find any solution.
I have 5v on my ds1307 I have a 3v battery I have pull-up resistors (10K) and i have a 32.768 KHz watch crystal.
I can see the signal on the SDA and SCL with the Oscilloscope.
I check again and again the connection... |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19498
|
|
Posted: Sat May 08, 2010 2:34 pm |
|
|
Double check your supply voltage, and Vbatt.
The Vcc supply, _must_ be at least Vbatt*1.25, or the chip will disable I2C. With a brand new lithium battery, it only takes the nominally 5v supply to be a little 'low', and I2C stops working. You'd still see SCL, and SDA from the PIC, but no actual reply.
Your pull-ups are rather high resistance. For 100KHz operation, with no series resistors, on 5v, 10K, limits the total bus capacitance to only about 75pF. Values like 4K7R, are generally 'safer'.
Best Wishes |
|
|
Davsgr
Joined: 08 May 2010 Posts: 3
|
|
Posted: Sat May 08, 2010 3:22 pm |
|
|
PIC are found any slave
I go testing with pull-ups 4K7
Thanks for your answer |
|
|
bright_krom
Joined: 11 May 2010 Posts: 3
|
Re: DS1307 problem I2C |
Posted: Tue May 11, 2010 5:35 am |
|
|
Code: |
// This driver is written for DS1307 RTC I2C interface.
//
// Function ds1307_init()
// ds1307_get_time()
// ds1307_set_time()
// ds1307_write_byte(adres, byte)
// ds1307_read_byte(adres) //return int8
//
// variables min, sec, hrs, mth, day, year, work_ds1307;
//
//
// Example: ...
// ...
// ds1307_init(); // this function made enable clock/osilator if clock is stopped.
// sec=0x00;
// min=0x10;
// hrs=0x18;
// ds1307_set_time(); // this function made clock is 18:10:00
//
// timer = ds1307_read_byte(0x15); //The value of ram address 0x15 is to timer for int8.
//
//
//
#define RTC_SDA PIN_C4
#define RTC_SCL PIN_C3
#use i2c(master, sda=RTC_SDA, scl=RTC_SCL)
#define Squarewave 0b00010010 //SQWE, located at address 0x07, 8.192Khz clock output at DS1307's 7. pin.
#define READ TRUE
#define WRITE FALSE
int min, sec, hrs, mth, day, year, work_ds1307;
void ds1307_open(int adres, int1 rw)
{
i2c_start();
i2c_write(0xD0);
i2c_write(adres);
if (rw) {i2c_start(); i2c_write(0xD1);}
}
void ds1307_close()
{
i2c_stop();
delay_us(2);
}
void ds1307_write_byte(int adres, int data)
{
ds1307_open(adres, WRITE);
i2c_write(data);
ds1307_close();
}
int ds1307_read_byte(int adres)
{
ds1307_open(adres, READ);
work_ds1307 = i2c_read(0);
ds1307_close();
return work_ds1307;
}
void ds1307_set_time()
{
ds1307_open(0x00, WRITE);
i2c_write(sec & 0x7F);
i2c_write(min);
i2c_write(hrs);
ds1307_close();
}
void ds1307_set_date()
{
ds1307_open(0x04, WRITE);
i2c_write(day);
i2c_write(mth);
i2c_write(year);
ds1307_close();
}
void ds1307_get_time()
{
ds1307_open(0x00, READ);
sec = i2c_read();
min = i2c_read();
hrs = i2c_read(0);
ds1307_close();
}
void ds1307_get_date()
{
ds1307_open(0x00, READ);
day = i2c_read();
mth = i2c_read();
year = i2c_read(0);
ds1307_close();
}
void ds1307_sqwe(int x)
{
ds1307_open(0x07, WRITE);
i2c_write(x);
ds1307_close();
}
int1 ds1307_init()
{
ds1307_get_time();
if (!sec & 0x80) return TRUE;
sec &= 0x7F; ds1307_write_byte(0x00, sec);
ds1307_sqwe(Squarewave);
return FALSE;
}
|
I hope you help. |
|
|
|
|
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
|