• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Working DS1307 project with ATmega

Thread starter #1
Hello,

Does anyone have a 100% working code for initialising, setting date/time and then reading date/time from DS1307 (and storing date/time in variables) written in C using hardware TWI interface at ATmega (most preferably atmega8).

The problem is i want to use DS1307 RTC and before ill start writing things by myself (because always when i check a device with libraries written by someone, and i read them and understand i start writing them from scratch by myself to know them better) i want to make sure i connected RTC in good way and its working (im gonna display it on LCD) and so when ill start writing my own program ill know that if its not working its my code fault and not RTC connection problem.

I tried to to search for it in internet but all i find are I2C libraries but i want something specific for DS1307 since im just starting it and im afraid that when using those I2C general libraries ill mess something up with combination of writing, initialising etc and again i wont know if its not working because of code or because of hardware.
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
#2
I've got them in C for pic .... Any good..!!! The Pic code would be VERY easy to convert to Atmel....

My I2C code is for both EEPROM's and RTC DS1307...


If I had the AVR simulator I'd convert them for you.
 
Thread starter #3
Ok, it seems ill write one quickier. This is what i wrote so far, im using DS1307 datasheet and ATmega8 datasheet for help (TWI hardware interface).

This is what i wrote so far, functions to send byte to DS1307
Code:
void ds1307_init()
{
	/*
	SCL Clock frequency
	SCLf=FCPU/(16+2*TWBR*(4^TWPS))
	*/
	TWBR = 2;
	TWSR |=((1<<TWPS1)|(1<<TWPS0));

	/*
	Enable the TWI Module
	*/
	TWCR|=(1<<TWEN);
} 

void ds1307_start();
{
	/*
	Send start condition
	Wait for ACK
	*/
	TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
	while(!(TWCR & (1<<TWINT)));
}

void ds1307_stop();
{
	TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
}

void ds1307_writebyte(uint8_t byte);
{
	/*
	check if start sucesfully, if not start error
	*/
	if((TWSR & 0xF8) != START) ERROR(); 
	/*
	Load byte to TWDR (adress) and
	Start transmission of adress
	*/
	TWDR = byte;
	TWCR = (1<<TWINT) | (1<<TWEN);
	/*
	Wait for ACK
	*/
	while(!(TWCR & (1<<TWINT)));
	/*
	check status
	*/
	if((TWSR & 0xF8) !=MT_SLA_ACK) ERROR();
}

void ds1307_sendbyte(uint8_t address, uint8_t data)
{
	/*
	Send start condition
	DS1307 in receiver mode
	Address()DS receiver
	1101000()0
	*/
	ds1307_start();
	ds1307_writbyte(11010000);
	/*
	Send address of desired register
	Send data to desired register
	Send stop condition
	*/
	ds1307_writbyte(address);
	ds1307_writbyte(data);
	ds1307_stop();
}
void ERROR()
{
	/*
	Will write error MSG on LCD
	*/
}
Im using code from ATmega8 datasheet but there are few things that i dont understand, what are those START and MT_SLA_ACK? Are those supposed to be variables or i should replace them with appropriate constants? I read those are statuses but statuses of what?

Also any advices, something can be done better or changed?
 
Thread starter #5
Two attachements *.c and *.h. I put those files together to make a ready to go DS1307 library (but can be used with other TWI devices too).

include *.h and attach *.c to your project

The comments in *.c are for ATmega8A

functions startings with TWI are general TWI functions that can be used with any other TWI device. Functions starting with DS1307 are just for this RTC clock.

Initialise TWI with TWI_init(), unlock device with DS1307_lock() (might be needed if device is new), DS1307_Set() sets time and date (look at function arguments).

The actual function that reads time from RTC is DS1307_conv(). It will read date and time from clock and convert and save it into strings timeBuf, dateBuf, dayBuf that can be displayed on LCD.

if you #define NoSecBuffer 1 then additional string noSecBuf will be avaliable (only minutes and seconds)
if you #define DirectTime 1 then you will be able to access directly numerical values of hours, seconds and minutes
 

Attachments

Latest threads

EE World Online Articles

Loading

 
Top