/*****************************************************************************
 *
 * Real time clock and display.
 *
 *****************************************************************************
 * FileName:        RTC.c
 * Dependencies:    
 * Processor:       PIC18F
 * Compiler:       	C18 02.30.00 or higher
 * Linker:          MPLINK 03.20.01 or higher
 * Company:         Microchip Technology Incorporated
 *
 * Software License Agreement
 *
 * The software supplied herewith by Microchip Technology Incorporated
 * (the "Company") is intended and supplied to you, the Company's
 * customer, for use solely and exclusively with products manufactured
 * by the Company. 
 *
 * The software is owned by the Company and/or its supplier, and is 
 * protected under applicable copyright laws. All rights are reserved. 
 * Any use in violation of the foregoing restrictions may subject the 
 * user to criminal sanctions under applicable laws, as well as to 
 * civil liability for the breach of the terms and conditions of this 
 * license.
 *
 * THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTIES, 
 * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED 
 * TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 
 * PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT, 
 * IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR 
 * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
 *
 *
 * This is a simple implementation of Real time clock functionality
 * 
 *
 *
 * Author               Date        Comment
 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * Gaurang Kavaiya		12/06/04	Initial Version
 * 
 *****************************************************************************/

#include	<P18cxxx.H>
#include	<stdlib.h>
#include	"TYPES.H"
#include	"IO.H"
#include	"strings.h"


UCHAR	uSec, uOldSec;
UCHAR	uMin;
UCHAR	uHour;


void RTC_Init(void)
{
	T1CON = 0x0f;
	
//	PIE1bits.TMR1IE = 1;
	TMR1H = 0x80;
	TMR1L = 0x0;

	uSec = uMin = uHour = 0;
}




void RTC_Update(void)
{
	UCHAR pValStr[10];

	// Scan interrupt flag
	if (PIR1bits.TMR1IF)
	{

	PIR1bits.TMR1IF = 0;
	_asm
	bsf	TMR1H,7,0
	_endasm
	uSec++;
	if (uSec> 59)
		{
			uSec = 0;
			uMin++;
			if (uMin> 59)
				{
					uMin =0;
					uHour++;
					
					if(uHour > 23)
						uHour =0;
				}
		}

			// If it changed the update
			if (uOldSec != uSec)
 			{
				uOldSec = uSec;

				// Write the value
/*				putrs(MsgTimeValPos);
				itoa(uHour, (char*)(pValStr+1));
				if(pValStr[2] == 0) {pValStr[2] = pValStr[1]; pValStr[1] = '0' ;}
				pValStr[3] = ':';

				itoa(uMin, (char*)(pValStr+4));
				if(pValStr[5] == 0) {pValStr[5]=pValStr[4]; pValStr[4] = '0' ;}
				pValStr[6] = ':';
				
				itoa(uSec, (char*)(pValStr+7));
				pValStr[0] = 5;
				puts(pValStr);*/


				putrs(MsgTimeValPos);
				itoa(uMin, (char*)(pValStr+1));
				if(pValStr[2] == 0) {pValStr[2] = pValStr[1]; pValStr[1] = '0' ;}
				pValStr[3] = ':';

				itoa(uSec, (char*)(pValStr+4));
				if(pValStr[5] == 0) {pValStr[5]=pValStr[4]; pValStr[4] = '0' ;}
				pValStr[6] = 0;
				
				pValStr[0] = 5;
				puts(pValStr);
				
				

			}

	
	}
}
