Go Down

Topic: [RISOLTO]MLX90614 with wire.h libraries (Read 1 time) previous topic - next topic

F4LK0

Jan 09, 2014, 10:25 pm Last Edit: Jan 21, 2014, 02:35 pm by F4LK0 Reason: 1
I  try to read temp value from MLX90614 using Arduino DUE. but I have a big problem. I receive always max value (0x7FFF).
I used this code

Code: [Select]

#define MLX90614_ADDR_1 0x5A
#define MLX90614_TOBJ1_RAM_ADDR 0x07
#define MLX90614_TOBJ2_RAM_ADDR 0x08
#define MLX90614_ENVIR_RAM_ADDR 0x06

void setup() {
 Serial.begin(9600);
 Wire.begin();
}

void loop() {
 uint16_t data = 0;
 uint8_t PEC = 0;
 //Inizio comunicazione
 Wire.beginTransmission(MLX90614_ADDR_1);
 //dico di leggere dall'area della RAM del sensore
 Wire.write(MLX90614_TOBJ1_RAM_ADDR);
 //fine comunicazione senza rilascio del canale
 Wire.endTransmission(false);
 //richiesta di invi odati dallo slave senza rilascio del canale
 Wire.requestFrom(MLX90614_ADDR_1, 3, true);
 //attendo finche ci sono dati sul canale    
 while(Wire.available() < 3);
 //leggo il primo byte
 data = Wire.read();
 //leggo il secondo byte
 data |= (Wire.read() & 0x7F) << 8;
 //leggo il PEC
 PEC = Wire.read(); // PEC
 //fine transazione e chiusura del canale
 //Wire.endTransmission();
 
 delay(200);
 
 float celsius = data / 50 - 273.15; //temperatura in °C
 float kelvin  = data / 50; //temperatura in Kelvin
 
 //visualizzo nel debug il valore il valore
 Serial.print(data);
 Serial.print(" - ");
 Serial.print(PEC);
 Serial.print("\n °C = ");
 Serial.print(celsius);
 Serial.print("\n K = ");
 Serial.print(kelvin);
 Serial.print("\n");
}


Somebody can Help me to resolve my problem?

I've a similar problem with the same chip, but only with Arduino DUE, not with Arduino UNO. Look my message http://forum.arduino.cc/index.php?PHPSESSID=8tgp4v58dat11h17bsi42cta13&topic=209555.0 and watch the differences in the 2 attached pictures, the i2c START of Arduino DUE are bad.

F4LK0

hi,
i know your problem. I'm Simone and I wrote in this blog http://skyduino.wordpress.com/2013/11/02/arduino-capteur-de-temperature-sans-contact-mlx90614/.

F4LK0

somebody resolved the problem?

thanks

F4LK0

in this post I found the solution:

http://forum.arduino.cc/index.php/topic,137607.msg1101421.html#msg1101421

this is code

Code: [Select]
#include <Arduino.h>
#include <include/twi.h>

#define ADDR      0x5A

//EEPROM 32x16
#define TO_MAX    0x00
#define TO_MIN    0x01
#define PWM_CTRL  0x02

//RAM 32x16
#define RAW_IR_1  0x04
#define RAW_IR_2  0x05
#define TA        0x06
#define TOBJ_1    0x07
#define TOBJ_2    0x08

#define SYNC_PIN  2

static const uint32_t TWI_CLOCK = 100000;
static const uint32_t RECV_TIMEOUT = 100000;
static const uint32_t XMIT_TIMEOUT = 100000;

Twi *pTwi = WIRE_INTERFACE;

static void Wire_Init(void) {
  pmc_enable_periph_clk(WIRE_INTERFACE_ID);
  PIO_Configure(
  g_APinDescription[PIN_WIRE_SDA].pPort,
  g_APinDescription[PIN_WIRE_SDA].ulPinType,
  g_APinDescription[PIN_WIRE_SDA].ulPin,
  g_APinDescription[PIN_WIRE_SDA].ulPinConfiguration);
  PIO_Configure(
  g_APinDescription[PIN_WIRE_SCL].pPort,
  g_APinDescription[PIN_WIRE_SCL].ulPinType,
  g_APinDescription[PIN_WIRE_SCL].ulPin,
  g_APinDescription[PIN_WIRE_SCL].ulPinConfiguration);

  NVIC_DisableIRQ(TWI1_IRQn);
  NVIC_ClearPendingIRQ(TWI1_IRQn);
  NVIC_SetPriority(TWI1_IRQn, 0);
  NVIC_EnableIRQ(TWI1_IRQn);
}

static void Wire1_Init(void) {
  pmc_enable_periph_clk(WIRE1_INTERFACE_ID);
PIO_Configure(
g_APinDescription[PIN_WIRE1_SDA].pPort,
g_APinDescription[PIN_WIRE1_SDA].ulPinType,
g_APinDescription[PIN_WIRE1_SDA].ulPin,
g_APinDescription[PIN_WIRE1_SDA].ulPinConfiguration);
PIO_Configure(
g_APinDescription[PIN_WIRE1_SCL].pPort,
g_APinDescription[PIN_WIRE1_SCL].ulPinType,
g_APinDescription[PIN_WIRE1_SCL].ulPin,
g_APinDescription[PIN_WIRE1_SCL].ulPinConfiguration);

NVIC_DisableIRQ(TWI0_IRQn);
NVIC_ClearPendingIRQ(TWI0_IRQn);
NVIC_SetPriority(TWI0_IRQn, 0);
NVIC_EnableIRQ(TWI0_IRQn);
}

void setup() {
  Serial.begin(9600);

  pinMode(SYNC_PIN, OUTPUT);
  digitalWrite(SYNC_PIN, LOW);

  Wire_Init();
  // Disable PDC channel
  pTwi->TWI_PTCR = UART_PTCR_RXTDIS | UART_PTCR_TXTDIS;
  TWI_ConfigureMaster(pTwi, TWI_CLOCK, VARIANT_MCK);
}

void loop() {
  uint16_t tempUK;
  float tempK;
  uint8_t hB, lB, pec;

  digitalWrite(SYNC_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(SYNC_PIN, LOW);

  TWI_StartRead(pTwi, ADDR, TOBJ_1, 1);

  lB = readByte();
  hB = readByte();
 
  //last read
  TWI_SendSTOPCondition(pTwi);
  pec = readByte();
 
  while (!TWI_TransferComplete(pTwi))
    ;
  //TWI_WaitTransferComplete(pTwi, RECV_TIMEOUT);

  tempUK = (hB << 8) | lB;
  if(tempUK & (1 << 16)) {
    Serial.print("Error !");
    Serial.println(tempK);
  }
  else {
    tempK = ((float)tempUK * 2) / 100 ;
    Serial.print("Temp UK: ");
    Serial.print(tempUK);
    Serial.print(" C: ");
    Serial.println(tempK - 273.15);
  }
  Serial.print(hB, HEX);
  Serial.print(" : ");
  Serial.print(lB, HEX);
  Serial.print(" : ");
  Serial.println(pec, HEX);
 
  delay(2000);
}

uint8_t readByte() {
  //TWI_WaitByteReceived(pTwi, RECV_TIMEOUT);
  while (!TWI_ByteReceived(pTwi))
    ;
  return TWI_ReadByte(pTwi);
}

static inline bool TWI_WaitTransferComplete(Twi *_twi, uint32_t _timeout) {
  while (!TWI_TransferComplete(_twi)) {
    if (TWI_FailedAcknowledge(_twi))
      return false;
    if (--_timeout == 0)
      return false;
  }
  return true;
}

static inline bool TWI_WaitByteReceived(Twi *_twi, uint32_t _timeout) {
  while (!TWI_ByteReceived(_twi)) {
    if (TWI_FailedAcknowledge(_twi))
      return false;
    if (--_timeout == 0)
      return false;
  }
  return true;
}

static inline bool TWI_FailedAcknowledge(Twi *pTwi) {
  return pTwi->TWI_SR & TWI_SR_NACK;
}



and jmparatte tested the signal.....

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy