Example #1
0
/*****************************************************************************
* Function Name: ProcessTemperature()
******************************************************************************
* Summary:
* Measures the current temperature. 
*
* Parameters:
* None
*
* Return:
* uint32 - Measured temperature
*
* Theory:
* This function measures the reference voltage, thermistor voltage and 
* offset voltage. 

* Channel 0 - Reference voltage
* Channel 1 - Thermistor voltage
* Channel 2 - Offset voltage
* 
* This function removes offset via correlated double sampling and then
* measures the temperature.
*
* Side Effects:
* None
*
* Note:
*
*****************************************************************************/
int32 ProcessTemperature (void)
{
    /* Local variables used for temperature measurement */
    static int32 temperature = 0;
    #if (SIMULATE_TEMPERATURE_SENSOR)
        
    int32 thermistorResistance = 0;
    int16 referenceVoltage = 0;
    int16 thermistorVoltage = 0;
    int16 offsetVoltage = 0;
    
    /* Check if measurement interval has expired */
    if(SystemFlag & MEASUREMENT_INTERVAL)
    {
        /* Disconnect all the pins from Amux input */
        AMuxSeq_DisconnectAll();
        
        /* Measure reference voltage - Channel 0 */
        referenceVoltage = MeasureSensorVoltage();
        /* Measure thermistor voltage - Channel 0 */
        thermistorVoltage = MeasureSensorVoltage();
        /* Measure offset voltage - Channel 0 */
        offsetVoltage = MeasureSensorVoltage();
        
        /* Measure thermistor resistance and remove offset by using correlated 
        *  double sampling. */
        thermistorResistance = Thermistor_GetResistance((referenceVoltage - offsetVoltage), (thermistorVoltage - offsetVoltage));
        
        /* Compute temperature and remove decimal places */
        temperature = Thermistor_GetTemperature(thermistorResistance);
        temperature = temperature / 100;
    }
    #else
    /* Check if measurement interval has expired */
    if (SystemFlag & MEASUREMENT_INTERVAL)
    {
        temperature++;
        if(temperature > 100)
        {
            temperature = 0;
        }
    }
    #endif
    
    /* Return measured temperature */
    return (temperature);
}
/*******************************************************************************
* Function Name: void processADC( void )
********************************************************************************
*
* Summary:
*  This function calculates all values from the ADC each time a scan completes.
*  The variable adcState is set to PROCESS by the ADC ISR each time a scan finishes.
*  Once processing is done, the adcState variable is set to DONE so that the
*  next scan can start.
*
* The values calculated are: Ambient light, temperature, and POT voltage.
*******************************************************************************/
void processADC(void)
{
    int16   alsCurrent; /* Variable to store ALS current */
    int16   illum16; /* Illuminance as a 16 bit value (lux) */
    int16   thermistorResistance; /* Variables for temperature calculation */
    int16   temp16; /* Temperature expressed as a 16 bit integer in 1/100th of a degree */

    /* Process ADC results that were captured in the interrupt */
    if(adcState == PROCESS)
    {
        /* ALS */
        /* Calculate the photodiode current */
		alsCurrent = (adcResults[ALS] * ALS_CURRENT_SCALE_FACTOR_NUMERATOR)/ALS_CURRENT_SCALE_FACTOR_DENOMINATOR; 
		
		/* If the calculated current is negative, limit it to zero */
		if(alsCurrent < 0)
		{
			alsCurrent = 0;
		}
		
		/* Calculate the light illuminance */
        illum16 = (alsCurrent * ALS_LIGHT_SCALE_FACTOR_NUMERATOR)/ALS_LIGHT_SCALE_FACTOR_DENOMINATOR;
		LocData.illuminance = (float32)(illum16);
        
        /* Thermistor */
        /* Calculate thermistor resistance */
        thermistorResistance = Thermistor_GetResistance(adcResults[THERM_REF], adcResults[THERM]);           
                       
        /* Calculate temperature in degree Celsius using the Component API */
        temp16 = Thermistor_GetTemperature(thermistorResistance);
        /* Convert tempearture to a float */
        LocData.temperature = ((float32)(temp16))/100.0;
        
        /* POT */
        LocData.potVal = ADC_CountsTo_Volts(POT, adcResults[POT]);
        
        adcState = DONE;
    }
}