This example shows how to read out the LM75 temperature sensor. The code is using the I2C library.

Datasheet: http://www.nxp.com/documents/data_sheet/LM75A.pdf

READ OUT TEMPERATURE

#include "lpc17xx.h"
#include "lpc_types.h"
#include "i2c.h"
#include "timer.h"

int main (void)
{
    int byte1;
    int byte2;
    int var;
    float output;

    TimerInit(0,500);                                                             //Initialize timer 0

    int portused = 1;                                                             //Declare I2C channel one as portused
    I2C1Init(0);                                                                     //Initialize I2C channel 1; port 0.0 and 0.1

    while(1)
    {
        I2CWriteLength[portused] = 2;                                   //Change write length to 2
        I2CMasterBuffer[portused][0] = 0b10011110;             //Copy address + write bit to masterbuffer
        I2CMasterBuffer[portused][1] = 0b00000000;            //Copy to 0 masterbuffer (Select temperature pointer register in sensor)
        I2CEngine(portused);                                                 //Run I2C data transmission

        I2CWriteLength[portused] = 1;                                   //Change writelength to 1
        I2CMasterBuffer[portused][0] = 0b10011111;             //Copy address + read byte to masterbuffer
        I2CEngine(portused);                                                 //Run I2C data transmission

        byte1 = I2CSlaveBuffer[1][0];                                     //Read out byte 1 from slave buffer
        byte2 = I2CSlaveBuffer[1][1];                                     //Read out byte 2 from slave buffer

        var = (byte2 |= (byte1 << 8));                                     //Data-proccessing step 1 (Transform byte 1 and 2 to one integer)
        output = (0.125 * (var >> 5));                                     //Data-proccessing step 2 + multyplying with 0.125 to get an actual temperature

        delayMs(0,500);                                                         //Delay 500ms
    }
}


This picture is a illustation of the data processing. 


Copyright 2011. Joomla 1.7 templates - Joomla template maker. The Simplecortex is developed by BRC-Electronics