i2c temperature sensor

17 Nov 2011

Hello,

I have a beginner lvl question. I want to read from an tmp sensor

#include "mbed.h"

void setRegisterPointer( void );
void readRegister( void );

Serial pc(USBTX, USBRX);
I2C i2c(p28, p27);
int address = 0x48;
char data[2];

int main() {
    i2c.frequency(200000);

    while (1) {
        pc.printf("start\n");
        
        data[0] = 0x04;//pointeradress
        setRegisterPointer();
        pc.printf("registerpointer set to (%c)\n",data[0]);
        
        readRegister();
        pc.printf("register read: (%c%c)\n",data[0], data[1]);
        wait_ms(1000);
    }      
}

void setRegisterPointer( void){
    i2c.write(address,data, 1);
}

void readRegister( void ){
    i2c.write(address,data, 2);

}

but the line:

pc.printf("register read: (%c%c)\n",data[0], data[1]);

only give out the pointer address 0x48 and 0x00, but I expect values like 0x18, 0xXX (from the sensor). I don't see the problem :D

18 Nov 2011

In setRegisterPointer that should be i2c.read instead of i2c.write

18 Nov 2011

The sensor has multiple registers with an address pointer that is incremented automatically. The first byte (after the addressbyte) what it receives, is it's new address pointer value. Now if he gets a read command, it reads the value in the address shown by the address pointer. So I need to write, before each read, the desired register address (here 0x04).

Perform the functions "i2c.write" and "i2c.write" a complete i2c communication (start, read/write, acks, stop etc)? Or have I to keep an eye of compliance with the Protocol?

18 Nov 2011

blob blob,

you are right, typically you need to set the register address first. In your case that means write one byte (the register pointer). Your function setRegisterPointer() does that. Next, you need to read the value from that register, that is read one byte. Your readRegister(void) should do that. Instead is does another i2c.write() of 2 dummy bytes.

The i2c.read() and i2c.write perform a complete I2C interaction (start, address+ack, reads/writes+ack, stop).

I suggest you change your code:

#include "mbed.h"

char getRegister(char deviceAddress, char registerAddress) {
  char data[2];

  // load register pointer
  data[0] = registerAddress;

  // set register pointer
  i2c.write(deviceAddress, data, 1);

  // get register content
  i2c.read(deviceAddress, data, 1);

  return data[0];
}

void setRegister(char deviceAddress, char registerAddress, char value) {
  char data[2];

  // load register pointer
  data[0] = registerAddress;

  // load register value
  data[1] = value;

  // set register
  i2c.write(deviceAddress, data, 2);
}

Serial pc(USBTX, USBRX);
I2C i2c(p28, p27);
char deviceAddress = 0x48;
char registerAddress;
char register;

int main() {
    i2c.frequency(200000);

    while (1) {
        pc.printf("start\n");
        
        registerAddress = 0x04;
        register = getRegister(deviceAddress, registerAddress);
        pc.printf("register 0x%2d read: 0x%2d)\n", registerAddress, register);
        wait_ms(1000);
    }      
}

18 Nov 2011

In my post above I meant to say that in the readregister function you should use i2c.read.

19 Nov 2011

thank you clark, I'll try this.

08 Oct 2013

did that code clark posted work?

10 Nov 2013

1. We want to convert an analog sinusoid with a range of -3.3V to +3.3V to an 8-bit digital signal with a sample rate of 8000Hz. Answer the following questions: - What is the step size in volts? - What is the worst-case quantisation error? - How long is the gap between adjacent samples, in seconds?

2. Using the LEDs on the mbed board, produce the following patterns: ⁃ Flash each LED from left to right, then from right to left, without any lights being on at the same time. Each LED should be turned on for 500 milliseconds, then when the LED switches off, the next LED should light up simultaneously. This process should occur ten times. ⁃ Turn the LEDs on for 200 milliseconds in a random order. Each time an LED is switched off, the next LED should light up. Run this process indefinitely.

3. Read temperature data into the mbed and use the LEDs to give a visual indication of the heat data (you should set the range of each LED, based on the observed temperature fluctuation in the room). Make a note of your design decisions. 1. 2.

can anyone help me for the code please .