#include <avr/io.h>
#include "i2c.h"
#include "ds1307.h"

// *******************************************************************
// FCE: komunikace s obvodem realneho casu (RTC) po sbernici I2C (TWI)
// INC: "ds1307.h", "i2c.h" 
// DAT: 19.2.2012
// VER: 1.5 
// *******************************************************************

// cteni hodnot z obvodu
uint8_t DS1307Read(uint8_t address,uint8_t *data)
{
	uint8_t res;	//result
	
	// Start
	I2CStart();
	
	// SLA+W
	res=I2CWriteByte(0b11010000);	// DS1307 adresa + W bit
	 
	// Error
	if(!res)	return FALSE;
	
	// zaslani adresy do registru >navazani komunikace
	res=I2CWriteByte(address);
	
	// Error
	if(!res)	return FALSE;
	
	// opakovani startu
	I2CStart();
	
	// SLA + R
	res=I2CWriteByte(0b11010001);	//DS1307 Adresa + R bit
	
	// Error
	if(!res)	return FALSE;
	
	// zaslani hodnoty do registru + NACK
	res=I2CReadByte(data,0);
	
	// Error
	if(!res)	return FALSE;
	
	// STOP
	I2CStop();
	
	return TRUE;
}


// zapis hodnoty do obvodu
uint8_t DS1307Write(uint8_t address,uint8_t data)
{
	uint8_t res;	//vysledek
	
	// Start
	I2CStart();
	
	// SLA+W
	res=I2CWriteByte(0b11010000);
	
	// Error
	if(!res)	return FALSE;
	
	// zaslani adresy
	res=I2CWriteByte(address);
	
	//Error
	if(!res)	return FALSE;
	
	// zaslani hodnoty
	res=I2CWriteByte(data);
	
	// Error
	if(!res)	return FALSE;
	
	// STOP
	I2CStop();
	
	return TRUE;
}
	
// inicializace obvodu - nastaveni hodnot
void TimeInit()
{

	int temp;

	temp=0b00000000;			// nastaveni vterin + CH bit
	DS1307Write(0x00,temp);
	
	temp=0b01000101;			// nastaveni minut 
	DS1307Write(0x01,temp);

	temp=0b00100000;			// nastaveni hodin, mod 12/24h
	DS1307Write(0x02,temp);

	temp=0b00000100;			// nastaveni dne v tydnu; 1=PONDELI ... 7=NEDELE
	DS1307Write(0x03,temp);	

	temp=0b00011001;			// nastaveni datumu; 1 .. 31
	DS1307Write(0x04,temp);	

	temp=0b00000011;			// nastaveni mesice; 1 .. 12
	DS1307Write(0x05,temp);	

	temp=0b00010010;			// nastaveni roku
	DS1307Write(0x06,temp);	


	#define CH 7				

	DS1307Read(0x00,&temp);
	
	temp&=(~(1<<CH));			// vymazani "CH" bitu v registru 00h RTC
	DS1307Write(0x00,temp);
		
	temp=0b10010000;			// nastaveni "SQWE" vystupu - blikani zelene LED 1Hz
	DS1307Write(0x07,temp);	
		
	temp = 0;

}

// cteni casu a data
void TimeRead()
{

	uint8_t data;
		
	DS1307Read(0x00,&data);		// cteni hodin
	Time[8]='\0';
	Time[7]=48+(data & 0b00001111);
	Time[6]=48+((data & 0b01110000)>>4);
	Time[5]=':';
	
	DS1307Read(0x01,&data);

	Time[4]=48+(data & 0b00001111);
	Time[3]=48+((data & 0b01110000)>>4);
	Time[2]=':';
	
	DS1307Read(0x02,&data);
	
	Time[1]=48+(data & 0b00001111);
	Time[0]=48+((data & 0b00110000)>>4);



	DS1307Read(0x06,&data);		// cteni datumu

	Date[7]=48+(data & 0b00001111);
	Date[6]=48+((data & 0b11110000)>>4);
	Date[5]='/';

	DS1307Read(0x05,&data);		

	Date[4]=48+(data & 0b00001111);
	Date[3]=48+((data & 0b00010000)>>4);
	Date[2]='/';

	DS1307Read(0x04,&data);		

	Date[1]=48+(data & 0b00001111);
	Date[0]=48+((data & 0b00110000)>>4); 

}





