/*****************************************************************************
 *
 * Temperature calculations and display.
 *
 *****************************************************************************
 * FileName:        TC74main.c
 * Dependencies:    
 * Processor:       PIC18F with CAN
 * 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 used within the DeviceNet Stack for
 * demonstration.
 * 
 *
 *
 * Author               Date        Comment
 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * Ross Fosler			08/13/04	...	
 * 
 *****************************************************************************/

#include	<P18cxxx.H>
#include	<stdlib.h>
#include	"TYPES.H"
#include	"IO.H"
#include	"strings.h"
#include	"I2Cmain.h"


#define	TC74_TEMP_COMP	3
//#define	TC74Addr	0b10100000
#define	TC74Addr	0b10011010

typedef struct _TC74FLAGS
{
	unsigned s1:1;
	unsigned s2:1;
	unsigned s3:1;
}TC74FLAGS;

TC74FLAGS	bTC74flags;
UCHAR		uTC74AvgCnt;
INT 		uAvgTC74;
INT 		uOldTC74;
UCHAR		uTC74RdCnt;
INT			uTC74TempK;
INT			uTC74TempF;
INT			uTC74TempR;



void TC74_Init(void)
{
	bTC74flags.s1 = 0;
	bTC74flags.s2 = 0;

	uTC74AvgCnt = 32;
	uTC74RdCnt = 8;
	uOldTC74 = ~uAvgTC74;
}



void TC74_ProcessEvents(void)
{
	UCHAR _uTempStr[6];
	
	// Setup the sensor
	if (!bTC74flags.s1)
	{
		if (!I2C_IsBusy())
		{
			// Setup the sensor
//			I2C_WriteCmdAndData(0b10011010, 0x01, 0x00);
			I2C_WriteCmdAndData(TC74Addr, 0x01, 0x00);
			bTC74flags.s1 = 1;
		}
	}
//	else 

	// Set control to the temperature register
	if (!bTC74flags.s2)
	{
		if (!I2C_IsBusy())
		{
//			I2C_WriteCmd(0b10011010, 0x00);
			I2C_WriteCmd(TC74Addr, 0x00);
			bTC74flags.s2 = 1;
		}
	}

	// Read the register
	else
	{
		// Only read every 16th itteration
		if (uTC74RdCnt)
		{
			uTC74RdCnt--;
		}
		else
		{
			uTC74RdCnt = 8;

			if (uTC74AvgCnt)
			{
				if (I2C_IsReady())
				{
					uTC74AvgCnt--;
					uAvgTC74 = uAvgTC74 + (char)I2C_Read() + TC74_TEMP_COMP;
					I2C_ClrReady();
				}
			}
			else
			{
				// Take the average and add the compensation
				uAvgTC74 = (uAvgTC74 / 32);
				if ((uAvgTC74 % 32) > 16) uAvgTC74++;


				// If it changed then update
				if (uOldTC74 != uAvgTC74)
				{
					uOldTC74 = uAvgTC74;

					// Display temp in C
					_uTempStr[1] = _uTempStr[2] = _uTempStr[3] = _uTempStr[4] = _uTempStr[5] = ' ';
					itoa(uAvgTC74, (char*)(_uTempStr + 1));
					putrs(MsgTempCPos);
					_uTempStr[0] = 5;
					puts(_uTempStr);

					// Display temp in F
					_uTempStr[1] = _uTempStr[2] = _uTempStr[3] = _uTempStr[4] = _uTempStr[5] = ' ';
					uTC74TempF = ((uAvgTC74 * 8) / 10) + 32 + uAvgTC74;
					if (((uAvgTC74 * 8) % 10) > 5) uTC74TempF++;
					itoa(uTC74TempF, (char*)(_uTempStr + 1));
					putrs(MsgTempFPos);
					_uTempStr[0] = 5;
					puts(_uTempStr);

					// Display temp in K
					_uTempStr[1] = _uTempStr[2] = _uTempStr[3] = _uTempStr[4] = _uTempStr[5] = ' ';
					uTC74TempK = uAvgTC74 + 273;
					itoa(uTC74TempK, (char*)(_uTempStr + 1));
					putrs(MsgTempKPos);
					_uTempStr[0] = 5;
					puts(_uTempStr);

					// Display temp in R
					_uTempStr[1] = _uTempStr[2] = _uTempStr[3] = _uTempStr[4] = _uTempStr[5] = ' ';
					uTC74TempR = ((uAvgTC74 * 8) / 10) + 492 + uAvgTC74;
					if (((uAvgTC74 * 8) % 10) > 5) uTC74TempR++;
					itoa(uTC74TempR, (char*)(_uTempStr + 1));
					putrs(MsgTempRPos);
					_uTempStr[0] = 5;
					puts(_uTempStr);
				}

				uTC74AvgCnt = 32;
				uAvgTC74 = 0;
			}

			// Request to read data from the temp sensor
			if (!I2C_IsBusy())
			{
				I2C_ReadData(TC74Addr);
			}
		}
	}
}
