Hi. I am using the tiva launchpad to read the temperature from the MLX90614 using I2c. For some reason I am only getting 255 back no matter what register I read from. The slave address is 0x5A and the register I want to read from is 0x07. I am not sure why it does not work or how to fix it.
This is the sensor datasheet and it says it needs a repeated start.
This is my code.
#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "inc/hw_gpio.h"
#include "inc/hw_ints.h"
#include "inc/hw_uart.h"
#include "inc/hw_sysctl.h"
#include "inc/hw_i2c.h"
#include "driverlib/fpu.h"
#include "driverlib/systick.h"
#include "driverlib/sysctl.h"
#include "driverlib/pin_map.h"
#include "driverlib/rom_map.h"
#include "driverlib/debug.h"
#include "driverlib/interrupt.h"
#include "driverlib/gpio.h"
#include "driverlib/uart.h"
#include "driverlib/rom.h"
#include "driverlib/i2c.h"
#ifdef DEBUG
void__error__(char *pcFilename, unsigned long ulLine) {}
#endif
void i2c_setup(void)
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
// Open-Drain operation
//
GPIOPadConfigSet(GPIO_PORTB_BASE, GPIO_PIN_2, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_OD);
GPIOPadConfigSet(GPIO_PORTB_BASE, GPIO_PIN_3, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_OD);
//
GPIODirModeSet(GPIO_PORTB_BASE, GPIO_PIN_3, GPIO_DIR_MODE_HW);
GPIODirModeSet(GPIO_PORTB_BASE, GPIO_PIN_2, GPIO_DIR_MODE_HW);
GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2);
GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3);
//GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_2 | GPIO_PIN_3);
GPIOPinConfigure(GPIO_PB2_I2C0SCL);
GPIOPinConfigure(GPIO_PB3_I2C0SDA);
I2CMasterEnable(I2C0_MASTER_BASE);
I2CMasterInitExpClk(I2C0_MASTER_BASE, SysCtlClockGet(), false);
SysCtlDelay(100);
}
void i2c_send_read(char addre, unsigned long data,unsigned long *receive)
{
unsigned long err;
unsigned long temp[2];
// Write the address first with direction as transmit to write the address for read
I2CMasterSlaveAddrSet(I2C0_MASTER_BASE,addre,false);
I2CMasterDataPut(I2C0_MASTER_BASE,data);
I2CMasterControl(I2C0_MASTER_BASE,I2C_MASTER_CMD_BURST_SEND_START);
while(I2CMasterBusy(I2C0_MASTER_BASE));
err = I2CMasterErr(I2C_MASTER_ERR_ADDR_ACK);
// Repeated Start to receive can be used to get data
I2CMasterSlaveAddrSet(I2C0_MASTER_BASE,addre,true);
I2CMasterControl(I2C0_MASTER_BASE,I2C_MASTER_CMD_BURST_RECEIVE_START);
while(I2CMasterBusy(I2C0_MASTER_BASE));
err = I2CMasterErr(I2C_MASTER_ERR_ADDR_ACK);
temp[0]=I2CMasterDataGet(I2C0_MASTER_BASE);
I2CMasterControl(I2C0_MASTER_BASE,I2C_MASTER_CMD_BURST_RECEIVE_CONT);
while(I2CMasterBusy(I2C0_MASTER_BASE));
temp[1]=I2CMasterDataGet(I2C0_MASTER_BASE);
I2CMasterControl(I2C0_MASTER_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH);
while(I2CMasterBusy(I2C0_MASTER_BASE));
// convert 10 place to ASCII
receive[1] = (temp[0]-((temp[0] / 10)*10)) + 0x30; // Convert 1's place to ASCII
}
int main(void)
{
// set the control clock to 8MHZ
SysCtlClockSet(SYSCTL_SYSDIV_1|SYSCTL_USE_PLL|SYSCTL_OSC_MAIN|SYSCTL_XTAL_8MHZ);
i2c_setup();
unsigned long tempData=0;
while(1)
{
i2c_send_read(0x5A,0x07,&tempData);
SysCtlDelay(SysCtlClockGet() / 10 / 3);
}
}