SparkFun Forums 

Where electronics enthusiasts find answers.

Your source for all things Atmel.
#12546
I am currently in the process of building a digital clock using the Olimex AVR-MT-129 board with a DS1307 connected to it. The AVR-MT128 is working great and has a lot of functionality. Got most of my code working for a menu system for setting the clock but when i hookup the DS1307 to the AVR i get nothing. I am wondering if anyone else has tried to do this and has had any luck and if anyone might know what i am doing wrong. I am including a code snippet and the wiring that i have done. Thanks for any help.
Garry


DS1307 AVRmega128
Pin 5 SDA to PD1
Pin 6 SCL to PD0
Pin 7 SQW to PE2
Pin 8 to +5
Pin 4 to GND
Pin 1 and 2 got to a 32.768kHZ crystal

Code:
#include <mega128.h>
#include <delay.h>
#include <stdio.h>

// I2C Bus functions
#asm
.equ __i2c_port=0x12 ;PORTD
.equ __sda_bit=1
.equ __scl_bit=0
#endasm
#include <i2c.h>

// DS1307 Real Time Clock functions
#include <ds1307.h>

// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0x15 ;PORTC
#endasm
#include <lcd.h>

// Declare your global variables here
unsigned char date,month,year,hour,minute,second;
char lcd_buffer[33];

void main(void)
---
snip
---
// I2C Bus initialization
i2c_init();

// DS1307 Real Time Clock initialization
// Square wave output on pin SQW/OUT: On
// Square wave frequency: 1Hz
rtc_init(0,1,0);

// LCD module initialization
lcd_init(16);
rtc_set_date(31,12,02);
lcd_clear();
rtc_set_time(23,59,58);
lcd_clear();

while (1)
{
// Place your code here
rtc_get_date(&date,&month,&year);
sprintf(lcd_buffer,"%2u-%2u-200%u",date,month,year);
lcd_gotoxy(0,0);
lcd_puts(lcd_buffer);
delay_ms(500);
rtc_get_time(&hour,&minute,&second);
sprintf(lcd_buffer,"%2u:%2u:%2u",hour,minute,second);
lcd_gotoxy(0,1);
lcd_puts(lcd_buffer);
delay_ms(500);
lcd_clear();
};
}
By transcendentnb2
#12575
I've never used that code, but check to make sure you have pull-up resistors on your clock and data lines. You never mentioned it and it's a fundamental I2C/TWI requirement.
By Sumit_Dave
#18329
1) which IDE are u using ?
2) can you send me the mega128.h and delay.h as i wanted to run demo code.
By stevech
#18346
Among the winAVR files for OPEX, a scheduler OS based on date/times rather than only delay() as in RTOSes - is code for a DS1307.
That code doesn't rely on libraries - - so maybe it'll help.

At the link, below, you can get the OPEX source code and look at the DS1307 source file.

And I included that file from the OPEX files as an attachment here.

These are old versions - newer exist.

http://www.avrfreaks.net/index.php?modu ... tem_id=230

http://www.atmanecl.com
http://www.atmanecl.com/EnglishSite/opex.htm (lower left)

and if you ever get involved with the new ZX24a, here's DS1307 code for ZBasic's VM is at
http://www.zbasic.net/forum/viewtopic.php?t=215