/*****************************************************************************
 *
 * Main entry into the application.
 *
 *****************************************************************************
 * FileName:        main.c
 * Dependencies:    
 * Processor:       PIC18F8xxx or PIC18F6xxx
 * 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 timer function
 * 
 *
 *
 * Author               Date        Comment
 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * Ross Fosler			08/13/04	...	
 * Gaurang Kavaiya		12/6/04		Modified for PICDEM HPC Explorer.
 *									Added RTC support.
 *****************************************************************************/



#include	<P18cxxx.H>
#include	<stdlib.h>
#include	"TYPES.H"
#include	"IO.H"
#include	"SRALLOC.H"
#include	"strings.h"
#include	"timer.h"
#include	"adcmain.h"
#include	"menu.h"
#include	"ledmain.h"
#include	"rb0main.h"
#include	"I2Cmain.h"
#include	"TC74main.h"
#include	"bootmain.h"
#include	"vectors.h"

void main(void)
{
	UCHAR uRxCmd;

	// Query for direct boot entry
	if (!PORTBbits.RB0)
	{
		_asm  
			clrf STKPTR, 0
			goto BootVector
		_endasm
	}
	

	SRAMInitHeap();			// Setup the dynamic heap
	
	TimerInit();

	IOOpen(0x2B);			// Open communications

	MainMenu_Update();		// Generate the main menu

	LED_Init();				// Setup and update LEDs
	LED_update();

	ADC_Init();				// Setup analog

	RB0_Init();				// PortB setup

	I2C_Init(0x7F);			// Setup I2C

	TC74_Init();			// Setup temperature sensor stuff
	
	RTC_Init();				//Setup RTC

	while(1)
	{
		// Process I/O events
		IOProcessEvents();

		// Process I2C events
		I2C_ProcessEvents();

		// Timer synchronized event
		if (TimerIsOverflowEvent())
		{
			ADC_Update();
			RTC_Update();
			RB0main();
			TC74_ProcessEvents();
		}

		// Get any data from the input stream
		uRxCmd = getc();
		switch (uRxCmd)
		{
			case '1':
				LATDbits.LATD0 = ~LATDbits.LATD0;
				LED_update();
				break;
			case '2':
				LATDbits.LATD1 = ~LATDbits.LATD1;
				LED_update();
				break;
			case '3':
				LATDbits.LATD2 = ~LATDbits.LATD2;
				LED_update();
				break;
			case '4':
				LATDbits.LATD3 = ~LATDbits.LATD3;
				LED_update();
				break;
			case '5':
				LATDbits.LATD4 = ~LATDbits.LATD4;
				LED_update();
				break;
			case '6':
				LATDbits.LATD5 = ~LATDbits.LATD5;
				LED_update();
				break;
			case '7':
				LATDbits.LATD6 = ~LATDbits.LATD6;
				LED_update();
				break;
			case '8':
				LATDbits.LATD7 = ~LATDbits.LATD7;
				LED_update();
				break;
			case 'b':
				Boot_Main();
				break;

		}
	}
}
