Thread: Reading temperature from the sensor

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    92

    Reading temperature from the sensor

    Hello everyone,

    Problem is not with the logic (atleast I hope so) but with code not being compiled,
    it might be a wrong forum to post a program for temperature sensor TMP102 using microcontroller but I am stuck with my compiler since it can't compile because of the errors.


    Basically it is using I2C to communicate with the sensor reading two bytes from the sensor in the following format:

    high_byte = 0b nnnn nnnn
    low_byte = 0b nnnn **** , * - not used bit, and n = [0,1] bit

    and shifting it to get 12 bit data, and when that 12 bit data is multiplied by 0.0625 we get temperature in Celsius.

    Code:
    /*
     * i2c.c
     *
     * Created: 12/26/2016 12:26:20 PM
     * Author : fairenough
     * ATmega 2560
     */ 
    #define F_CPU 16000000UL
    
    #include <avr/io.h>
    
    #define TMP102_ADDRESS_W            0b10010000
    #define TMP102_ADDRESS_R            0b10010001
    #define TMP102_TEMP_REGISTER        0b00000000
    #define TMP102_CONFIG_REGISTER        0b00000001
    #define TMP102_TLOW_REGISTER        0b00000010
    #define TMP102_THIGH_REGISTER        0b00000011
    #define TMP102_CONFIG_DATA_BYTE_1    0x60            //Normal mode operation
    #define TMP102_CONFIG_DATA_BYTE_2    0XA0            //Normal mode operation
    
    unsigned char tempHighByte;
    unsigned char tempLowByte;
    float temp;
    
    void i2c_init();
    void i2c_start();
    void i2c_stop()
    void i2c_send (unsigned char data);
    unsigned char i2c_read_ack();
    unsigned char i2c_read_noAck();
    
    
    void i2c_init()
    {
        TWSR = 0x00;            //set prescaler bits to zero
        TWBR = 72;                // BR = (F_CPU) / (16 + 2*TWBR * 1) = 100kHz
        TWCR |= 1 << TWEN;        //enable TWI module
    }
    
    void i2c_start()
    {
        TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN);
        while (!(TWCR & (1 <<TWINT)));
    }
    
    void i2c_stop()
    {
        TWCR |= (1 << TWINT) | (1 << TWEN) | (1 << TWSTO);
        while (!(TWCR & (1 << TWSTO)));
    }
    void i2c_send (unsigned char data)
    {
        TWDR = data;
        TWCR |= (1 << TWINT) | (1 << TWEN);
        while (!(TWCR & (1 << TWINT)));
    }
    
    unsigned char i2c_read_ack()
    {
        TWCR |= (1 << TWINT) | (1 << TWEN) | (1 << TWEA);
        while (!(TWCR & (1 << TWINT)));
        return TWDR;
    }
    
    unsigned char i2c_read_noAck()
    {
        TWCR |= (1 << TWINT) | (1 << TWEN);
        while (!(TWCR & (1 << TWINT)));
        return TWDR;
    }
    
    
    int main(void)
    {
        i2c_init();
        
        while (1) 
        {
            i2c_start();
            i2c_send(TMP102_ADDRESS_W);
            i2c_send(TMP102_CONFIG_REGISTER);
            i2c_send(TMP102_CONFIG_DATA_BYTE_1);
            i2c_send(TMP102_CONFIG_DATA_BYTE_2);
            i2c_stop();
            
            i2c_start();
            i2c_send(TMP102_ADDRESS_R);
            tempHighByte = i2c_read_ack();
            tempLowByte = i2c_read_noAck();
            i2c_stop();
            
            temp = 0.0625 * ( (tempHighByte << 4) | (tempLowByte >>4) );
        }
    }
    Compiler error:
    https://s28.postimg.org/6imq2r0p9/Capture.jpgfree screen capture

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Please cut and paste the complete error messages, all of them exactly as they appear in your development environment.

    By the way you seem to be missing at least one semicolon (line 27).


    Jim

  3. #3
    Registered User
    Join Date
    Dec 2015
    Posts
    92
    Quote Originally Posted by jimblumberg View Post
    Please cut and paste the complete error messages, all of them exactly as they appear in your development environment.

    By the way you seem to be missing at least one semicolon (line 27).


    Jim
    I couldn't paste it, atleast I do not know the way to do it (ATMEL Studio which is based on the MS Visual Studio)

    Here is the picture that I posted in the first post:
    https://s28.postimg.org/6imq2r0p9/Capture.jpg

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Did you fix the issue I pointed out in my last response and recompile?

    Jim

  5. #5
    Registered User
    Join Date
    Dec 2015
    Posts
    92
    Yeah Jim I put the semicolon and it compiled now.
    I have one more question, it might be offtopic, what is the most efficient algorithm to convert floating data type to a string for the serial communication (variable temp is floating type which is sent back to the PC from MCU).

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I'd probably go with something like sprintf().

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sensor Values reading wrong
    By kevin306 in forum C Programming
    Replies: 4
    Last Post: 05-26-2014, 04:07 AM
  2. Replies: 2
    Last Post: 12-04-2012, 07:28 AM
  3. Gas sensor using msp 430.
    By lowchernhwee in forum C Programming
    Replies: 2
    Last Post: 08-02-2012, 04:36 AM
  4. How to get RSSI value, send to sensor, sensor receive package, repackage it?
    By techissue2008 in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-04-2009, 10:13 AM

Tags for this Thread