#include "ds1302.h"


void DS1302_Write(uchar reg,uchar data){
	uchar i;
	Set_DS1302_DDRSDA();
	Clr_DS1302_RST();
	asm("nop");
	asm("nop");
	Clr_DS1302_SCLK();
	asm("nop");
	asm("nop");
	Set_DS1302_RST();
	asm("nop");
	asm("nop");
	for(i=8;i>0;i--)
		{ 
		if(reg&0x01) Set_DS1302_SDA();
	else Clr_DS1302_SDA();
		asm("nop");
		asm("nop");
		Set_DS1302_SCLK(); 
		asm("nop");
		asm("nop");
		Clr_DS1302_SCLK();
		asm("nop");
		asm("nop");
		reg>>=1;
		}
	for(i=8;i>0;i--)
		{ 
		if(data&0x01) Set_DS1302_SDA();
	else Clr_DS1302_SDA();
		asm("nop");
		asm("nop");
		Set_DS1302_SCLK();
		asm("nop");
		asm("nop");
		Clr_DS1302_SCLK();
		asm("nop");
		asm("nop");
		data>>=1;
		}
	Clr_DS1302_RST();
	asm("nop");
	asm("nop");
	Clr_DS1302_DDRSDA();
	}
//-----
uchar DS1302_Read(uchar reg){
	uchar data=0,i;
	reg+=1;
	Set_DS1302_DDRSDA();
	Clr_DS1302_RST();
	asm("nop");
	asm("nop");
	Clr_DS1302_SCLK();
	asm("nop");
	asm("nop");
	Set_DS1302_RST();
	asm("nop");
	asm("nop");
	for(i=8;i>0;i--)
	{
		if(reg&0x01) Set_DS1302_SDA();
	else Clr_DS1302_SDA();
		asm("nop");
		asm("nop");
		Set_DS1302_SCLK(); 
		asm("nop");
		asm("nop"); 
		Clr_DS1302_SCLK();
		asm("nop");
		asm("nop");
		reg>>=1;
	}
	Clr_DS1302_DDRSDA();
	for(i=8;i>0;i--)
	{ 
		data>>=1;
		if((PINA&(1<<DS1302_SDA))==(1<<DS1302_SDA)) data|=0x80;
		Set_DS1302_SCLK();
		asm("nop");
		asm("nop");
		Clr_DS1302_SCLK();
		asm("nop");
		asm("nop"); 
	}
	Clr_DS1302_RST();
	asm("nop");
	asm("nop");
	return(data);
	}
//-------------
uchar Check_DS1302(void){
	DS1302_Write(DS1302_CONTROL_Reg,0x80);
	if(DS1302_Read(DS1302_CONTROL_Reg)==0x80) return 1;
	return 0;
	}
//-----
void DS1302_Init(void){
	DS1302_Write(DS1302_CONTROL_Reg,0x00);		//zápis (WR) povolen
//	DS1302_Write(DS1302_SEC_Reg,0x80);
	DS1302_Write(DS1302_CHARGER_Reg,0xab);		//nabíjení-zap - přes 2 diody a 8kOhm
	DS1302_Write(DS1302_YEAR_Reg,0x11); 
	DS1302_Write(DS1302_MONTH_Reg,0x02);
	DS1302_Write(DS1302_DATE_Reg,0x26); //ČŐ
	DS1302_Write(DS1302_DAY_Reg,0x06); //ÖÜ
	DS1302_Write(DS1302_HR_Reg,0x18); //Ę±
	DS1302_Write(DS1302_MIN_Reg,0x40); //·Ö
	DS1302_Write(DS1302_SEC_Reg,0x00); //Ăë

//  	DS1302_Write(DS1302_CONTROL_Reg,0x80);
	}
//------------
void readtime(void){
	uchar data;
	data=DS1302_Read(DS1302_YEAR_Reg);
	year0=data&0x0f;
	year1=data>>4;
	data=DS1302_Read(DS1302_MONTH_Reg);
	month0=data&0x0f;
	month1=(data>>4)&0x01;
	data=DS1302_Read(DS1302_DATE_Reg);
	date0=data&0x0f;
	date1=(data>>4)&0x03;
	data=DS1302_Read(DS1302_DAY_Reg);
	day=data&0x07;
	data=DS1302_Read(DS1302_HR_Reg);
	hour0=data&0x0f;
	hour1=(data>>4)&0x03;
	data=DS1302_Read(DS1302_MIN_Reg);
	minute0=data&0x0f;
	minute1=(data>>4)&0x07;
	data=DS1302_Read(DS1302_SEC_Reg);
	second0=data&0x0f;
	second1=(data>>4)&0x07;
	} 
//------------------------------------
// nastavení data a času - po vstupu do fce se datum ovládá talčítky
//------------------------------------
void setdate(void){
		readtime();
		_delay_ms(300);
		datedisp();
		while(bit_is_set(PINTL,TLP)){
		 if(bit_is_clear(PINTL,TLH)){
		   if(date0<9)
			date0++;
			datedisp();
			_delay_ms(100);}
		 if(bit_is_clear(PINTL,TLD)){
		   if(date0>0)
			date0--;
			datedisp();
			_delay_ms(100);}
			}
		_delay_ms(500);
		while(bit_is_set(PINTL,TLP)){
		 if(bit_is_clear(PINTL,TLH)){
		   if(date1<3)
			date1++;
			datedisp();
			_delay_ms(100);}
		 if(bit_is_clear(PINTL,TLD)){
		   if(date1>0)
			date1--;
			datedisp();
			_delay_ms(100);}
			}

		_delay_ms(500);
		while(bit_is_set(PINTL,TLP)){
		 if(bit_is_clear(PINTL,TLH)){
		   if(month0<9)
			month0++;
			datedisp();
			_delay_ms(100);}
		 if(bit_is_clear(PINTL,TLD)){
		   if(month0>0)
			month0--;
			datedisp();
			_delay_ms(100);}
			}
		_delay_ms(500);
		while(bit_is_set(PINTL,TLP)){
		 if(bit_is_clear(PINTL,TLH)){
		   if(month1<1)
			month1++;
			datedisp();
			_delay_ms(100);}
		 if(bit_is_clear(PINTL,TLD)){
		   if(month1>0)
			month1--;
			datedisp();
			_delay_ms(100);}
			}
			_delay_ms(500);
		while(bit_is_set(PINTL,TLP)){
		 if(bit_is_clear(PINTL,TLH)){
		   if(year0<9)
			year0++;
			datedisp();
			_delay_ms(100);}
		 if(bit_is_clear(PINTL,TLD)){
		   if(year0>0)
			year0--;
			datedisp();
			_delay_ms(100);}
			}
			_delay_ms(500);
		while(bit_is_set(PINTL,TLP)){
		 if(bit_is_clear(PINTL,TLH)){
		   if(year1<9)
			year1++;
			datedisp();
			_delay_ms(100);}
		 if(bit_is_clear(PINTL,TLD)){
		   if(year1>0)
			year1--;
			datedisp();
			_delay_ms(100);}
			}
			_delay_ms(500);
		while(bit_is_set(PINTL,TLP)){
		 if(bit_is_clear(PINTL,TLH)){
		   if(hour0<9)
			hour0++;
			datedisp();
			_delay_ms(100);}
		 if(bit_is_clear(PINTL,TLD)){
		   if(hour0>0)
			hour0--;
			datedisp();
			_delay_ms(100);}
			}
			_delay_ms(500);
		while(bit_is_set(PINTL,TLP)){
		 if(bit_is_clear(PINTL,TLH)){
		   if(hour1<2)
			hour1++;
			datedisp();
			_delay_ms(100);}
		 if(bit_is_clear(PINTL,TLD)){
		   if(hour1>0)
			hour1--;
			datedisp();
			_delay_ms(100);}
			}
			_delay_ms(500);
		while(bit_is_set(PINTL,TLP)){
		 if(bit_is_clear(PINTL,TLH)){
		   if(minute0<9)
			minute0++;
			datedisp();
			_delay_ms(100);}
		 if(bit_is_clear(PINTL,TLD)){
		   if(minute0>0)
			minute0--;
			datedisp();
			_delay_ms(100);}
			}
		_delay_ms(500);
		while(bit_is_set(PINTL,TLP)){
		 if(bit_is_clear(PINTL,TLH)){
		   if(minute1<5)
			minute1++;
			datedisp();
			_delay_ms(100);}
		 if(bit_is_clear(PINTL,TLD)){
		   if(minute1>0)
			minute1--;
			datedisp();
			_delay_ms(100);}	
			}
			writedate();
			_delay_ms(500);

		}
//-------------------------------------
// zobrazit datum
//--------------------------------------
void datedisp(void){
	char text[16];
	lcd_clrscr();
	lcd_gotoxy(0,0);
	sprintf(text,"%d%d.%d%d.%d%d %d%d:%d%d--%d%d",date1,date0,month1,month0,year1,year0,hour1,hour0,minute1,minute0,second1,second0);
	lcd_puts(text);
		}
//--------------------------------------
//  zápis do RTC 
//--------------------------------------
void writedate(void){
	unsigned char pom;
	pom=(year1<<4)|year0;
DS1302_Write(DS1302_YEAR_Reg,pom); 
	pom=(month1<<4)|month0;
DS1302_Write(DS1302_MONTH_Reg,pom);
	pom=(date1<<4)|date0;
DS1302_Write(DS1302_DATE_Reg,pom); 
	pom=(hour1<<4)|hour0;
DS1302_Write(DS1302_HR_Reg,pom);
	pom=(minute1<<4)|minute0; 
DS1302_Write(DS1302_MIN_Reg,pom);
DS1302_Write(DS1302_SEC_Reg,0x00);
		}
//----------------------------------------
// fce vrací minuty
//---------------------------------------
char minuty(void){
return(10*minute1+minute0);
}

//----------------------------------------
// fce vrací hodiny
//---------------------------------------
char hodiny(void){
return(10*hour1+hour0);
}
//----------------------------------------
// fce vrací dny
//---------------------------------------
char dny(void){
return(10*date1+date0);
}
// fce vrací měsíce
//---------------------------------------
char mesice(void){
return(10*month1+month0);
}

