Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] DS1307 read function is not working

Status
Not open for further replies.

ADGAN

Full Member level 5
Full Member level 5
Joined
Oct 9, 2013
Messages
295
Helped
4
Reputation
8
Reaction score
4
Trophy points
18
Activity points
1,837
Hi! I'm using a DS1307 and the program is not executing from the read function onwards. I'm using 10K pull up resistors and this is written in MPLAB C18. This is the code:

Code:
void readDS1307(){
          StartI2C1();
          IdleI2C1();
          WriteI2C1(0xD0);
          IdleI2C1();
          WriteI2C1(0x00);
          IdleI2C1();
          RestartI2C1();
          IdleI2C1();
          WriteI2C(0xD1); 
          IdleI2C(); 
                                
        sec = ReadI2C1();
        AckI2C1();
        min = ReadI2C1();
        AckI2C1();
        hr = ReadI2C1();
        AckI2C1();
        week_day = ReadI2C1();
        AckI2C1();
        day = ReadI2C1();
        AckI2C1();
        mn = ReadI2C1();
        AckI2C1();
        year = ReadI2C1();
        NotAckI2C1();
        StopI2C1();

}

I have called this function in the main function as follows:
Code:
OpenI2C1(MASTER,SLEW_OFF);
Delay10KTCYx(25);
SSPADD = 99;
writeDS1307();
readDS1307();

How to resolve this problem?
 
Last edited:

I think the problem is with ReadI2C1()
 

Thanks for the reply. I'm using PIC18F45K22 with 4MHz crystal.
 

hello,


Does the ouput SQWOUT on DS1307 blink at 1hz .. to check if DS1307 init is OK ?
What about config bits settings ?
Hardware initialisation ?

here is my code (Pic18F26K22 at 10Mhz)
to drive a DS1307 and a I2C LCD 4x20 car on same bus..
i use 2,7K pull up reistors on SCL and ADA lines
Where do you init the first time the DS13027
in my application i test RA5 input to init or not init DS1307 values

Code:
void ds1307_get_time(void)
{
	I2CStart();
	writeI2C(DS1307_ADDR);			// slave addrs
	writeI2C(0x00);            // Start at REG 0 - Seconds
	I2CRestart();
	writeI2C(DS1307_ADDR+1);
	sec = bcd2bin(Read_I2C(0));
	min = bcd2bin(Read_I2C(0));
	hr  = bcd2bin(Read_I2C(1));
	I2CStop();
}

The result of DS1307 register are in BCD format..
do you use the DS1307 data in this format ?

and for I2C init SSPBUF must be cleared and no WCOL collision bit ON.

Code:
void main()
{

... etc ........

 // Initial the PIC18F26K22 Hardware I2C Master  uses RC3 & RC4
   CloseI2C();    
   addr=PCF8574_ADDR;   // 4E pour le LCD
   // module I2C a 100KHz
   OpenI2C(MASTER, SLEW_OFF);
   SSPADD = 24 ;  // at 10Mhz et  49 at 20MHz;
   IdleI2C();
   StartI2C();
   
   donnee = SSPBUF;        // clear buffer 
   do
   {
   status = WriteI2C( addr);    //write the address of slave
   if(status == -1)             //check if collision happened
   {
    donnee = SSPBUF;            //empty the buffer,
    SSPCON1bits.WCOL=0;     // clear the collision status bit
    }
    j++;
   }
   while((status!=0)&& (j<100));        //write untill successful communication


if (PORTAbits.RA5==0) DS1307_Init_Date_Heure(13,8,23,5,16,6);
	
 //   k=fprintf(_H_USART,"Lecture DS1307\r\n"); 
    ds1307_get_time();
    LCD_Erase_Line(3);   
    k=sprintf(txt,"Time is %02dH%02dM%02dS  ",hr,min,sec); 
    LCD_puts(txt);
   	Tempo(1000);
    ds1307_get_date();
    LCD_Erase_Line(4);  
    k=sprintf(txt,"Date is %02d/%02d/20%02d ",date,mth,year); 
    LCD_puts(txt);
 

Thanks for the reply. How do I check the SQWOUT output? No I didn't use it in BCD format, I converted it. I tried a MikroC example and it worked, so I'm guessing the DS1307 is fine. The problem I'm facing is the program does not execute beyond the read function. I'll try your code. Thanx

- - - Updated - - -

How do you use Read_I2C(0) or Read_I2C(1) in MPLAB ?
 

How do I check the SQWOUT output?

i added a led to check it DS1307 pin 7 SQWOUT ---- R=2,7K + led --+5V
on this output
writing 10H in control register at @07H make PIN7 1Hz ouput




How do you use Read_I2C(0) or Read_I2C(1) in MPLAB ?

my I2C functions..
Code:
void I2CStart(void)
{
	I2CIdle();
	SSPCON2bits.SEN = 1;
	while (SSPCON2bits.SEN);
}

void I2CRestart(void)
{
	I2CIdle();
	SSPCON2bits.RSEN = 1;
	while (SSPCON2bits.RSEN);
}

void I2CStop(void)
{
	I2CIdle();
	SSPCON2bits.PEN = 1;
	while (SSPCON2bits.PEN);
}

void writeI2C(unsigned char data)
{
	PIR1bits.SSP1IF = 0;
	SSPBUF = data;
	while(!PIR1bits.SSP1IF);

}

char Read_I2C(char ack)
{
	I2CIdle();
	SSP1CON2bits.RCEN = 1;
	while(SSP1CON2bits.RCEN);
	while(!SSP1STATbits.BF);
	I2CACK(ack);
	return SSPBUF;
}

void I2CIdle(void)
{
Tempo(100);
//	while((SSP1STAT & 0x04) | (SSPCON2 & 0x1F));
}

void I2CACK(unsigned char ackbit)
{
	
	if(ackbit)
	{
	SSP1CON2bits.ACKDT = 1;
	}
	else
	{
	SSP1CON2bits.ACKDT = 0;
	}

SSP1CON2bits.ACKEN = 1;
 while(SSP1CON2bits.ACKEN);
}
 
  • Like
Reactions: ADGAN

    ADGAN

    Points: 2
    Helpful Answer Positive Rating
That means you are not using the i2c.h header isn't it? Instead you are using your own.
 

yes,because "standard I2C " is not so compliant with every I2C devices !
with this,i can dialogue ok with DS 1307 and PCF8354 on the LCD.
If your design doesn't works with existing standard library,
you must play around it.

There is too much discussion about DS1307 to understand that
I2C is not 100% standard facing all different devices
and trade mark manufacturers.
Some time you have to write bit bang I2C!
it's my own opinion.. but you may keep doubt about.
 
  • Like
Reactions: ADGAN

    ADGAN

    Points: 2
    Helpful Answer Positive Rating
I even tried 4.7K but it didn't work. I tried a MikroC code with 10K and it worked. But in MPLAB the program doesn't execute beyond readI2C1().
 

I tried everything, but nothing works. I searched for this problem in the internet and many have faced this problem. If somebody know the solution pls post. I'm running out of time.
 

1. Remove Ic from socket or Remove Battery connection for movement connect it again , apply supply & then read RTC. if ur RTC read properly then u software data conversion
or Initialization problem
2. To check I2C u may use external EEPROM & check read write operation if data correct then ur I2C code okay.
3. Check Battery voltage monitoring bit condition of RTC
 

post all your code,with config bits..
and also tell what is config bits seen by pickit3.
i will check your version...
 

Thanks btw I'm using pickit 2 . I'll post it now.

- - - Updated - - -

This is my code. I have set the config bits in the config bit window of MPLAB. I tried your code but since it gave a compile error I removed it. I'll it try again. Pls let me know if I have made a mistake. Thank you.
View attachment AEBS - Copy.rar
 

hello,

Here is a program test for DS1307
It uses classic I2C MPLAB function and also mine .. to compare.

after few changes in your code , it's works Now
main key is to define the register
SSPADD = 24 ; // at 10Mhz or 49 at 20MHz

Results :

Lecture DS1307
DS1307 raw time 13/12/22 le 06 22H 58M 06S
DS1307 transformed time 13/12/22 le 06 22H 58M 06S

Lecture DS1307 with other subroutines
DS1307 date 13/12/22/ le 6
DS1307 time 22H58M07S

Lecture DS1307
DS1307 raw time 13/12/22 le 06 22H 58M 07S
DS1307 transformed time 13/12/22 le 06 22H 58M 07S
 

Attachments

  • Test_DS1307.zip
    15.9 KB · Views: 117
  • Like
Reactions: ADGAN

    ADGAN

    Points: 2
    Helpful Answer Positive Rating
Thank you so much. I'm using a 4MHz crystal with PLL enabled. The datasheet says "SCLx pin clock period = ((ADD<7:0> +1) *4)/Fosc" So the Fosc = 16MHz and SCL clock period is 4.0us, does that mean SSPADD = 15 ? But it should be between 24 and 49. To be honest I'm not familiar with calculating tcy, Fosc or SSPADD.

- - - Updated - - -

Thank you very much. I tested this in real hardware & it worked. But I have one more question to ask. When I switch it off & on again, seconds starts with zero. Is there a way to overcome this issue. I'm using a 3V battery.
 
Last edited:

SSPAD must be 39 for 16Mhz and I2C @ 100Khz

The last example i send DOSN'T WORK with your function
because use of pointer instead of values in the function
i tested by initialising all value to zero before to do read DS1307
and i get zero value..
it's my own function wich did the refresh of values..
so i mofified your function by using values instead of pointer..
and it is OK
 

Attachments

  • 18F26K22_Test_DS1307.zip
    98.7 KB · Views: 105
  • Like
Reactions: ADGAN

    ADGAN

    Points: 2
    Helpful Answer Positive Rating
I did those modifications. Thank you very much for the help.
 
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top