lpcware

Trouble in understanding function inside library

Discussion created by lpcware Employee on Jun 15, 2016
Latest reply on Jun 15, 2016 by lpcware
Content originally posted in LPCWare by mngeowcy on Sat Apr 04 09:02:33 MST 2015
Hi ,

I'm doing a school project where I am required to use a temperature sensor to printf out the current temperature detected by the sensor. The problem is that the function takes too long , causing an inevitable delay. I wanted to try to edit the function inside the library but I'm not able to understand the code. Was hoping to get some help here.

The function in the library is as follows :


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 );
}



These are the definitions :


#define TEMP_TS1 0
#define TEMP_TS0 0

#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_6_STATE ((GPIO_ReadValue(0) & (1 << 6)) != 0)
#define P0_2_STATE ((GPIO_ReadValue(0) & (1 << 2)) != 0)


#ifdef TEMP_USE_P0_6
#define    GET_TEMP_STATE P0_6_STATE
#else
#define    GET_TEMP_STATE P0_2_STATE
#endif



Note that the library does contain other functions as well , so not all the defined terms will be used.

I have also looked at the datasheet ( http://datasheets.maximintegrated.com/en/ds/MAX6576-MAX6577.pdf ) of the temperature sensor , the general descriptions of how it works is :


Quote:


Figure 3 shows a quick-look application circuit for the
MAX6576 using a universal counter measuring period.
TS1 and TS0 are both tied to ground to select a scalar
multiplier of 10μs/°K. The MAX6576 converts the ambient
temperature into a square wave with a period that is
10 times the absolute temperature of the device in μs.
At room temperature, the universal counter will display
approximately 2980μs




I understand the code up to the line "   state = GET_TEMP_STATE; " , I'm lost after that point.

Thank you and sorry for the trouble.

Outcomes