/*********************************************************************
 *
 *                Thermo Sensor
 *
 *********************************************************************
 * FileName:        temperature.c
 * Dependencies:    See INCLUDES section below
 * Processor:       PIC18
 * Compiler:        C18 2.30.01+
 * Company:         Microchip Technology, Inc.
 *
 * Software License Agreement
 *
 * The software supplied herewith by Microchip Technology Incorporated
 * (the 揅ompany? for its PICmicro?Microcontroller is intended and
 * supplied to you, the Company抯 customer, for use solely and
 * exclusively on Microchip PICmicro Microcontroller products. 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 揂S 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.
 *
 ********************************************************************/

/** I N C L U D E S **********************************************************/
#include "Compiler.h"
#include "GenericTypeDefs.h"
#include "HardwareProfile.h"
#include "temperature.h"

#if defined(__18CXX)
    #if defined(PICDEM_FS_USB)
        #include <spi.h>
    #else
        #include "SoftwareSPI.h"
    #endif
#endif

/** V A R I A B L E S ********************************************************/
#pragma udata
BOOL old_sw;
WORD_VAL temperature;       // Raw data format
char tempString[15];        // Buffer for storing data in ASCII format

/** P R I V A T E  P R O T O T Y P E S ***************************************/

/** D E C L A R A T I O N S **************************************************/
#pragma code
/******************************************************************************
 * Function:        void InitTempSensor(void)
 *
 * PreCondition:    None
 *
 * Input:           None
 *
 * Output:          None
 *
 * Side Effects:    None
 *
 * Overview:        Initializes SPI interface & chip select line
 *
 * Note:            None
 *****************************************************************************/
void InitTempSensor(void)
{
    #if defined(PICDEM_FS_USB)
    cs_temp_sensor = 1;
    tris_cs_temp_sensor = OUTPUT_PIN;
    OpenSPI(SPI_FOSC_64, MODE_11, SMPMID);
    
    // Initialize readable values - default to room temperature
    temperature.Val = 0x0C87;       // 25 degree celsius
    UpdateCelsiusASCII();
    
    #elif defined(LOW_PIN_COUNT_USB_DEVELOPMENT_KIT)
        //Initialize SW SPI interface to TC72
        ANSEL = 0;
        ANSELH = 0;
        LOpenSWSPI ();
        LSetCESWSPI();        //clear CS pin
        LWriteSWSPI( 0x80 );
        LWriteSWSPI( 0x00 );
        LClearCESWSPI();        //clear CS pin

 	#elif defined(__C30__) || defined(__C32__)
        #if defined(__C30__)
	   		AD1CHS = 0x4;           //MUXA uses AN4
    		AD1PCFGLbits.PCFG4 = 0;
            AD1CON3 = 0x0002;
            AD1CON2 = 0;
            AD1CON1 = 0x8000; // turn ADC ON
       #else
            AD1PCFGbits.PCFG14 = 0;
            AD1CON1 = 0x0000; // SAMP bit = 0 ends sampling ...
            // and starts converting
            AD1CHS = 0x000E0000; // Connect RB14/AN14 as CH0 input ..
            // in this example RB2/AN2 is the input
            AD1CSSL = 0;
            AD1CON3 = 0x0002; // Manual Sample, Tad = internal 6 TPB
            AD1CON2 = 0;
            AD1CON1SET = 0x8000; // turn ADC ON
        #endif
 	
    #endif    
}//end InitTempSensor

/******************************************************************************
 * Function:        void AcquireTemperature(void)
 *
 * PreCondition:    None
 *
 * Input:           None
 *
 * Output:          None
 *
 * Side Effects:    None
 *
 * Overview:        None
 *
 * Note:            None
 *****************************************************************************/
BOOL AcquireTemperature(void)
{
	float temp;
	int counter;
				
	//get ready to sample the A/D
	AD1CON1SET = 0x0002;          // start sampling ...
	for(temp=0;temp<1000;temp++); //Sample delay, conversion start automatically
	AD1CON1CLR = 0x0002;          // start Converting
	while (!(AD1CON1 & 0x0001));  // conversion done?

	temperature.Val = ADC1BUF0;

    return TRUE;
}

/******************************************************************************
 * Function:        void UpdateCelsiusASCII(void)
 *
 * PreCondition:    None
 *
 * Input:           None
 *
 * Output:          None
 *
 * Side Effects:    None
 *
 * Overview:        This routine converts data output to ASCII string
 *
 * Note:            None
 *****************************************************************************/
void UpdateCelsiusASCII(void)
{
    WORD_VAL temp;
        
    temp.Val = temperature.Val;          //发送字符串 
    tempString[0] = 'A';
    tempString[1] = 'D';
    tempString[2] = 'C';
    tempString[3] = 'V';
    tempString[4] = 'a';
    tempString[5] = 'l';
    tempString[6] = 'u';
    tempString[7] = 'e';
    tempString[8] = ':';

    tempString[9] = (temp.Val / 1000) + '0';   //发送AD值
    tempString[10] = (temp.Val % 1000)/100 + '0';
    tempString[11] = (temp.Val % 100)/10 + '0';
    tempString[12] = (temp.Val % 100)%10 + '0';
    
    tempString[13] = '\r';
    tempString[13] = '\n';
    tempString[14] = 0x00;  
}//end UpdateCelsiusASCII

/*******************************************************************************
 * Function:        BOOL SwitchIsPressed(void)
 *
 * PreCondition:    None
 *
 * Input:           None
 *
 * Output:          Returns TRUE if sw was pressed
 *                  Returns FALSE if sw was not pressed
 *
 * Side Effects:    None
 *
 * Overview:        This function is used to check sw switch.
 *
 * Note:            None
 ******************************************************************************/
BOOL SwitchIsPressed(void)
{
    if(sw != old_sw)
    {
        old_sw = sw;                  // Save new value
        if(sw == 0)                    // If pressed
            return TRUE;                // Was pressed
    }//end if
    return FALSE;                       // Was not pressed
}//end SwitchIsPressed

/** EOF temperature.c ********************************************************/
