Content originally posted in LPCWare by researchinnovation on Thu Apr 19 04:54:05 MST 2012
Hope this code may help you...Just give a try.. .:)
#include "mcu_regs.h"
#include "type.h"
#include "gpio.h"
#include "temp.h"
/******************************************************************************
* Defines and typedefs
*****************************************************************************/
/*
* Time-Select Pin Configuration. Selected by Jumper J26 on the base board
*/
#define TEMP_TS1 0
#define TEMP_TS0 0
/*
* Pin 0.2 or pin 1.5 can be used as input source for the temp sensor
* Selected by jumper J25.
*/
//#define TEMP_USE_P0_2
#if TEMP_TS1 == 0 && TEMP_TS0 == 0
#define TEMP_SCALAR_DIV10 1
#define NUM_HALF_PERIODS 340
#elif TEMP_TS1 == 0 && TEMP_TS0 == 1
#define TEMP_SCALAR_DIV10 4
#define NUM_HALF_PERIODS 100
#elif TEMP_TS1 == 1 && TEMP_TS0 == 0
#define TEMP_SCALAR_DIV10 16
#define NUM_HALF_PERIODS 32
#elif TEMP_TS1 == 1 && TEMP_TS0 == 1
#define TEMP_SCALAR_DIV10 64
#define NUM_HALF_PERIODS 10
#endif
#define P0_2_STATE ((LPC_GPIO0->DATA & (0x1 << 2)) != 0)
#define P1_5_STATE ((LPC_GPIO1->DATA & (0x1 << 5)) != 0)
#ifdef TEMP_USE_P0_2
#define GET_TEMP_STATE P0_2_STATE
#else
#define GET_TEMP_STATE P1_5_STATE
#endif
/******************************************************************************
* External global variables
*****************************************************************************/
/******************************************************************************
* Local variables
*****************************************************************************/
static uint32_t (*getTicks)(void) = NULL;
/******************************************************************************
* Local Functions
*****************************************************************************/
/******************************************************************************
* Public Functions
*****************************************************************************/
/******************************************************************************
*
* Description:
* Initialize Temp Sensor driver
*
* Params:
* [in] getMsTicks - callback function for retrieving number of elapsed ticks
* in milliseconds
*
*****************************************************************************/
void temp_init (uint32_t (*getMsTicks)(void))
{
#ifdef TEMP_USE_P0_2
GPIOSetDir( PORT0, 2, 0 );
#else
GPIOSetDir( PORT1, 5, 0 );
#endif
getTicks = getMsTicks;
}
/******************************************************************************
*
* Description:
* Read temperature
*
* Returns:
* 10 x T(c), i.e. 10 times the temperature in Celcius. Example:
* if the temperature is 22.4 degrees the returned value is 224.
*
*****************************************************************************/
int32_t temp_read (void)
{
uint8_t state = 0;
uint32_t t1 = 0;
uint32_t t2 = 0;
int i = 0;
/*
* T(C) = ( period (us) / scalar ) - 273.15 K
*
* 10T(C) = (period (us) / scalar_div10) - 2731 K
*/
state = GET_TEMP_STATE;
/* get next state change before measuring time */
while(GET_TEMP_STATE == state);
state = !state;
t1 = getTicks();
for (i = 0; i < NUM_HALF_PERIODS; i++) {
while(GET_TEMP_STATE == state);
state = !state;
}
t2 = getTicks();
if (t2 > t1) {
t2 = t2-t1;
}
else {
t2 = (0xFFFFFFFF - t1 + 1) + t2;
}
return ( (2*1000*t2) / (NUM_HALF_PERIODS*TEMP_SCALAR_DIV10) - 2731 );
}