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