
	
        /*----------------------------------------*/
	
	sbit	RTC_SCLK = 0x95;	//1380 SCLK PIN	
	sbit	RTC_IO   = 0x96;	//1380 I/O  PIN	
	sbit    RTC_REST = 0X97;	//1380 REST PIN

	unsigned char	xdata	rtc_data[8]; //BUFFER TO MAINTAIN INDIVIDUAL REGISTERS
										 // OF 1380 

	//This function writes data passed by rdata to 1380
	void writeRtc(unsigned char rdata)
	{								  
		unsigned char i,temp=1;

		for(i=0;i<8;i++)		//writing 8 bits serially	
		{
			RTC_SCLK=0;			//Input to SCLK is falling edge - rising edge sequence				
			if(rdata & temp)	//check for particular bit to send as 1 / 0
				RTC_IO = 1;		//taking bit by bit data starting from LSB on port pin
			else
				RTC_IO = 0;				
			
			RTC_SCLK=1;			//Input to SCLK is falling edge - rising edge sequence				
			temp <<= 1;			//Left Shifting for sending next bit
		}
	}

	//-------------------------------------

	//This function enables write mode for 1380 and sends the data to write into 1380
	void writeRtcData(unsigned char rvar,unsigned char reg)
	{	
		RTC_REST=1;			//REST = 1 starts data transfer	

		reg &= 0xfe;		//R/W = 0 for writing mode enable
		writeRtc(reg);		//write command to RTC
		
		writeRtc(rvar);		//writing data to the location decided by address by address byte in command

		RTC_SCLK=0;		
		RTC_REST=0;			//REST = 0 terminates data transfer
	}
	
	/*****************************************************************************/
	// SETS THE DATE AS PER THE PASSED PARAMETERS BY UPDATING THE INTERNAL RTC REGISTERS
	/*****************************************************************************/
	/*---------------------------------------------------------------------*/
	void setDate(unsigned char dt,unsigned char mn,unsigned char year,unsigned char wk)
	{		//date			month		year		week day
	
		writeRtcData(0x00,0x8e);	//WP=0 register data can be written in
		writeRtcData(dt,0x86);		//write date
		writeRtcData(mn,0x88);		//write month
		writeRtcData(wk,0x8a);		//write day
		writeRtcData(year,0x8c);	//write year	
		writeRtcData(0x80,0x8e);	//WP=1 register data can not be written in
	}

	/*****************************************************************************/
	// SETS THE TIME AS PER THE PASSED PARAMETERS BY UPDATING THE INTERNAL RTC REGISTERS
	/*****************************************************************************/
	/*---------------------------------------------------------------------*/
	void setTime(unsigned char h,unsigned char m,unsigned char s)
	{        //	hours		minutes		seconds
		//	(24 hour format)

		writeRtcData(0x00,0x8e);	//WP=0 register data can be written in
		writeRtcData(s,0x80);		//write seconds	
		writeRtcData(m,0x82);		//write minutes
		writeRtcData(h,0x84);		//write hours
		writeRtcData(0x80,0x8e);	//WP=1 register data can not be written in
	}

	/*****************************************************************************/
	//This function extracts all the 8 registers of 1380 into the rtc_data[] temporary
	//buffer, so that we can access the individual array elements of this buffer
	//whenever we need latest RTC parameters, which can be called by using the functions
	//mentioned below this function.
	/*****************************************************************************/
	void updateRealTime(void)
	{
		unsigned char i,j,k,temp;

		RTC_REST = 1;			//REST = 1 starts data transfer	

		writeRtc(0xbf);			//command for burst mode reading	
		
		for(i=0;i<8;i++)		//8 register loop
		{
			rtc_data[i]=0;		//first clear the register.
			temp=0x01;
			for (j=0;j<8;j++)	//for 8 bits = 1 byte
			{
				RTC_IO=1;		//making line as input
				RTC_SCLK=0;		//Input to SCLK is falling edge - rising edge sequence				

				if(RTC_IO)		//if 1 then save as 1 else kept as 0
					rtc_data[i] |= temp;				

				RTC_SCLK=1;		//Input to SCLK is falling edge - rising edge sequence				

				temp<<=1;		//Left Shifting for reading next bit
			}			
			k = rtc_data[i]>>4;	//Generating the 8-bit value			
			rtc_data[i] = (unsigned char)((k*10)+(rtc_data[i] & 0x0f));
		}
		RTC_SCLK=0;				//REST = 0 terminates data transfer
	}

	/*****************************************************************************/
	// These functions are called when we require the specified RTC parameter
	// in our operations of the system
	// Example : unsigned char cur_min; cur_min = getMins();
	/*****************************************************************************/
	unsigned char getSecs(void) {return rtc_data[0];}	
	unsigned char getMins(void) {return rtc_data[1];}
	unsigned char getHours(void){return rtc_data[2];}
	unsigned char getDate(void) {return rtc_data[3];}
	unsigned char getMonth(void){return rtc_data[4];}
	unsigned char getYear(void) {return rtc_data[6];}
	unsigned char getDay(void)  {return rtc_data[5]-1;}


	//Main code...
	/*****************************************************************************/
	//The rtc_data[] temporary buffer is updated forever in the superloop and
	//the date and time setting, grabbing the required RTC parameters are done
	//thro special user programming at system runtime.
	/*****************************************************************************/
	void main(void)
	{
		//Initialize hardware devices
		//Initialize RTOS
		//Create Tasks
		//Load initial values and parameters
		//start Interrupts and Serial Baud

		RTC_SCLK=0;		
		RTC_REST=0;	
		RTC_IO = 0;

		while(1)// SuperLoop that Runs Forever...
		{
			--
			--
			--
			--
			--
			updateRealTime();
		}
	}


	