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 RTC and 24LC256 EEPROM on the same i2c bus problem

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



Joined: 23 Jul 2006
Posts: 2

View user's profile Send private message

DS1307 RTC and 24LC256 EEPROM on the same i2c bus problem
PostPosted: Mon Jul 24, 2006 2:13 am     Reply with quote

Hello!!

I use DS1307 and 24LC256 on the same bus and it have some problem.

When i use DS1307 alone its work well

But when i connect 24LC256 on the bus the time i get from DS1307 always be 45:85:85

any suggestion?

my pic is 16f877 using PIN_C3 as SCL and PIN_C4 as SDL.
I have 10k resistor pull up to +5V on both pins.(try change this to 1k but same thing)
birumher



Joined: 28 Apr 2004
Posts: 10

View user's profile Send private message Send e-mail

PostPosted: Mon Jul 24, 2006 2:37 am     Reply with quote

I use DS1307 and 24LC32 on the same bus and it works fine.
Here is my circuit


and here is the code.


Code:


#if defined(__PCM__)
#include <16F877>
#fuses XT,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)

#elif defined(__PCH__)
#include <18F452>
#fuses XT,NOWDT,NOPROTECT,NOLVP,NOPUT,NOBROWNOUT
#use delay(clock=4000000)
#endif

#define RTC_SDA  PIN_C4
#define RTC_SCL  PIN_C3
#define EEPROM_SDA  PIN_C4
#define EEPROM_SCL  PIN_C3

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

#include <lcd>
#include "ds1307.c"
#include <2432>

void main()
{
    BYTE data;
    BYTE data2;
    long int addr;
    init_ext_eeprom();
    lcd_init();
    lcd_gotoxy(1,1);

    data = 12;
    data2 = 12;
    addr=5;
    // write 12 to 5. byte
    write_ext_eeprom(addr, data);
    printf(lcd_putc,"write ad.\%02Lu data \%02u", addr, data);
    delay_ms(40);

    while (1)
    {
    lcd_gotoxy(1,2);

    // read 5. byte
    data2 = read_ext_eeprom(addr);
    lcd_gotoxy(1,2);
    printf(lcd_putc,"read  ad.\%02Lu data \%02u", addr, data2);
    delay_ms(1000);
    }
   
}


you have to replace #include <2432> with #include <24256>


and here is DS1307.C

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, FORCE_HW)

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));
}
Highwind



Joined: 23 Jul 2006
Posts: 2

View user's profile Send private message

PostPosted: Mon Jul 24, 2006 3:29 am     Reply with quote

this is my code

Code:
#include <16F877>
#device *=16
#use delay(clock=4000000)
#fuses XT,NOWDT,NOLVP
#use rs232(baud=19200,parity=N,xmit=PIN_C6,rcv=PIN_C7)

#byte port_a = 0x05
#byte port_d = 0x08

#include <lcds>
#include <24256>
#include <ds1307>


  BYTE sec;
  BYTE min;
  BYTE hrs;
  BYTE day;
  BYTE month;
  BYTE yr;
  BYTE dow;

#int_RDA
RDA_isr() {



}



void main() {
   int data;
   
   port_b_pullups(TRUE);
   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_CLOCK_DIV_2);
   setup_spi(FALSE);
   setup_psp(PSP_DISABLED);
   setup_counters(RTCC_INTERNAL,RTCC_DIV_2);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_ccp1(CCP_OFF);
   setup_ccp2(CCP_OFF);
   enable_interrupts(INT_RDA);
   enable_interrupts(global);
   
   set_tris_a(0x00);
   set_tris_d(0x00);
   
   port_a = 0x00;
   port_d = 0x00;
   data = 0x00;
   
   delay_ms(100);
   
   ds1307_init();
   lcd_init();
   init_ext_eeprom();
   
   lcd_putc("\f Test I2C \n");
   //write_ext_eeprom(0x0005,0x32);
   ds1307_set_date_time(23,7,6,1,18,47,00);
   
     
   while(1)
   {
   ds1307_get_time(hrs,min,sec);
   lcd_putc("\f Time:");
   lcd_putc((hrs/10) + 0x30);
   lcd_putc((hrs%10) + 0x30);
   lcd_putc(':');
   lcd_putc((min/10) + 0x30);
   lcd_putc((min%10) + 0x30);
   lcd_putc(':');
   lcd_putc((sec/10) + 0x30);
   lcd_putc((sec%10) + 0x30);
   lcd_putc("  \n");
   
   delay_ms(1000);
   //data = read_ext_eeprom(0x0005);
   //lcd_putc("data = ");
   //lcd_putc((data/10)+0x30);
   //lcd_putc((data%10)+0x30);
   }

}


and I use DS1307.c the same as your

DS1307 alone is fine but when 24LC256 is connect it keep reading 45:85:85

if i have //write_ext_eeprom(0x0005,0x32); program will hang and LCD keep showing "Test I2C"
birumher



Joined: 28 Apr 2004
Posts: 10

View user's profile Send private message Send e-mail

PostPosted: Mon Jul 24, 2006 7:21 am     Reply with quote

in 24256.c pins are
Code:

#ifndef EEPROM_SDA

#define EEPROM_SDA  PIN_B1
#define EEPROM_SCL  PIN_B0

#endif


you have to change it...

And in your circuit A0, A1, A2 of 24256 must be grounded. Check it.

And did you try only 24LC256 without DS1307 in the circuit?
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