Fleury's Library with DS1307

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, I've gone through page after page of posts on this forum dealing with the DS1307 and I think I've incorporated everything I see but still no dice. I'm using a mega644 on an STK500 to interface to the DS1307 on a futurlec DS1307 dev board. I've got PC0 hooked to SCL and PC1 hooked to SDA. GND and Vcc are connected to the power pins. All four connections are done through the PortC header of the STK500.

I have an LCD screen attached to PORTB to see what's going on.

Here's the code:

#define F_CPU 8000000UL  // 8 MHz

#include 
#include 
#include    //see note below about restrictions
#include "lcd.h"
#include "i2cmaster.h"

#define DS1307	0b11010000

char  buffer[10];

int main(void)
{
	unsigned char ret;	
	
	//Init Everything
	lcd_init(LCD_DISP_ON);  	// initialize display, cursor off
	i2c_init();

	//Let's see if we detect the DS1307
	lcd_puts("Check: ");
	if (i2c_start(DS1307+I2C_WRITE))
	{
		i2c_stop();
		lcd_puts("No device\n");
	}
	else
	{
		i2c_stop();
		lcd_puts("Device Found\n");
	}
	

	//Now let's get the seconds
	lcd_puts("Asking seconds..");

	if (i2c_start(DS1307+I2C_WRITE)) lcd_puts("fail1");     // set device address and write mode
	if (i2c_write(0x00)) lcd_puts("fail2");
	if (i2c_write(0x00)) lcd_puts("fail3");
	i2c_stop();
	
	if (i2c_start(DS1307+I2C_READ)) lcd_puts("fail4");       // set device address and read mode
	ret = i2c_readNak();                    // read one byte
    i2c_stop();
	
	lcd_puts("\nSeconds: ");
	
	utoa(ret,buffer,10);
	lcd_puts(buffer);

	

	for(;;)
	{
	}

}

On my lcd screen I get:

Quote:
Check: Device Found
Asking seconds..fail4

That would seem to indicate that I can't start the read command. I have no idea why that would be. Interestingly, if I remove the Vcc connection so the dev board has to run on its on battery backup (but I still have the ground connected), I do read 3.3V on the battery pin but I can't get past the check part of the program so my terminal reads:

Quote:
Check:

Wonder why that would make a difference.

Anyhow, any tips welcome.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Your code looks absolutely fine. AFIK, the I2C i/f shuts down when you are on backup battery. (it just maintains the time).

But since it is ACKing the I2C_WRITE address, the I2C_READ address should get ACKed too. I can only suggest that you check that 'I2C_READ' #define has not been corrupted.

I have run your program successfully on a real DS1307.
n.b. you should display the Seconds value in hex not decimal.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks very much for testing my code. Allows me to concentrate my investigation in other areas. I'll report back what the problem was whenever I get that "aha!"

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think JTAG uses portC, did you disable jtag?

Mike

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The RTC needs to get started the fist time used by zeroing the CH bit.

Quote:
Please note that the initial power-on state of all registers is not defined. Therefore, it is important to
enable the oscillator (CH bit = 0) during initial configuration.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Finally got a couple more chips in from digikey. My program worked instantly on the replacement chip. Thanks!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Chips are generally fine. It is normally my wiring or software that is the problem.

As both I and you have discovered, your code was fine.

I would try the original DS1307 one more time, before consigning to the dustbin.

David.