Craig Evans / Mbed 2 deprecated 2645_TMP102

Dependencies:   mbed

main.cpp

Committer:
eencae
Date:
2016-02-15
Revision:
1:63221e5c0606
Parent:
0:bbdc7a18d706
Child:
2:e3e801b31933

File content as of revision 1:63221e5c0606:

/*

2645_TMP102

Sample code from ELEC2645 Week 17 Lab

Demonstrates how to read the temperature from an I2C sensor

(c) Craig A. Evans, University of Leeds, Feb 2016

*/

#include "mbed.h"

// addresses for ADD0 connected to GND
#define TMP102_ADD      0x48
#define TMP102_R_ADD    0x91
#define TMP102_W_ADD    0x90

// register addresses
#define TEMP_REG    0x00
#define CONFIG_REG  0x01
#define TLOW_REG   0x02
#define THIGH_REG    0x03

// I2C peripheral for temperature sensor
I2C tmp102(I2C_SDA,I2C_SCL);
// UART connection for PC
Serial pc(USBTX,USBRX);

// K64F on-board LEDs
DigitalOut r_led(LED_RED);
DigitalOut g_led(LED_GREEN);
DigitalOut b_led(LED_BLUE);
// K64F on-board switches
InterruptIn sw2(SW2);
InterruptIn sw3(SW3);

// error function hangs flashing an LED
void error();
// setup serial port
void init_serial();
// set-up the on-board LEDs and switches
void init_K64F();
// initialisation function for temperature sensor
void init_TMP102();
// function to read temperature from sensor (in degrees C)
float get_temperature();

int main()
{
    // initialise the board, serial port and sensor
    init_K64F();
    init_serial();
    init_TMP102();

    while (1) {

        // read temperature and print over serial port
        float T = get_temperature();
        pc.printf("T = %f K\n",T);
        // small delay - 1s to match the update rate of the sensor (1 Hz)
        wait(1.0);

    }

}

void init_serial()
{
    // set to highest baud - ensure terminal software matches
    pc.baud(115200);
}

void init_K64F()
{
    // on-board LEDs are active-low, so set pin high to turn them off.
    r_led = 1;
    g_led = 1;
    b_led = 1;

    // since the on-board switches have external pull-ups, we should disable the internal pull-down
    // resistors that are enabled by default using InterruptIn
    sw2.mode(PullNone);
    sw3.mode(PullNone);

}

void error()
{
    while(1) {  // if error, hang while flashing error message
        r_led = 0;
        wait(0.2);
        r_led = 1;
        wait(0.2);
    }
}

void init_TMP102()
{
    tmp102.frequency(400000); // set bus speed to 400 kHz

    int ack;  // used to store acknowledgement bit
    char config_data[2];  // array for data
    char reg = CONFIG_REG;  // register address

    //////// Read current status of configuration register ///////

    ack = tmp102.write(TMP102_W_ADD,&reg,1);  // send the slave write address and the configuration register address
    if (ack)
        error();  // if we don't receive acknowledgement, flash error message

    ack = tmp102.read(TMP102_R_ADD,config_data,2);  // read default 2 bytes from configuration register and store in array
    if (ack)
        error();  // if we don't receive acknowledgement, flash error message

    ///////// Configure the register //////////

    // set conversion rate to 1 Hz - CR1 and CR2 are in the second data byte
    config_data[1] |= (1 << 6);    // set bit 6
    config_data[1] &= ~(1 << 7);    // clear bit 7

    //////// Send the configured register value to the slave config register ////////////

    // create data packet
    char data_packet[3] = {reg,config_data[0],config_data[1]};

    ack = tmp102.write(TMP102_W_ADD,data_packet,3);  // send the data packet to the slave write address
    if (ack)
        error();  // if we don't receive acknowledgement, flash error message

}

float get_temperature()
{
    int ack;  // used to store acknowledgement bit
    char data[2];  // array for data
    char reg = TEMP_REG;  // temperature register address

    ack = tmp102.write(TMP102_W_ADD,&reg,1);  // send temperature register address
    if (ack)
        error();  // if we don't receive acknowledgement, flash error message

    ack = tmp102.read(TMP102_R_ADD,data,2);  // read 2 bytes from temperature register and store in array
    if (ack)
        error();  // if we don't receive acknowledgement, flash error message

    int temperature = (data[0] << 4) | (data[1] >> 4);

    return temperature*0.0625;
}