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 support@ccsinfo.com

DS1307 problem I2C

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



Joined: 08 May 2010
Posts: 3

View user's profile Send private message

DS1307 problem I2C
PostPosted: Sat May 08, 2010 8:49 am     Reply with quote

Hi Smile

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 . Embarassed
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat May 08, 2010 1:32 pm     Reply with quote

See this thread for ideas on trouble-shooting DS1307 problems:
http://www.ccsinfo.com/forum/viewtopic.php?t=37774
Davsgr



Joined: 08 May 2010
Posts: 3

View user's profile Send private message

PostPosted: Sat May 08, 2010 2:10 pm     Reply with quote

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... Rolling Eyes
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat May 08, 2010 2:27 pm     Reply with quote

Try this program to see if the PIC can find the ds1307 on the i2c bus.
It will scan the i2c bus and report any slaves that are found:
http://www.ccsinfo.com/forum/viewtopic.php?t=37650&start=6
Ttelmah



Joined: 11 Mar 2010
Posts: 19348

View user's profile Send private message

PostPosted: Sat May 08, 2010 2:34 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sat May 08, 2010 3:22 pm     Reply with quote



PIC are found any slave Confused

I go testing with pull-ups 4K7

Thanks for your answer Very Happy
bright_krom



Joined: 11 May 2010
Posts: 3

View user's profile Send private message

Re: DS1307 problem I2C
PostPosted: Tue May 11, 2010 5:35 am     Reply with quote

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.
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