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
*/
}