This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

I2C code conversion help needed

Hi,

I need to read data from MLX90615. It is a temperature sensor which follows I2C to read data. I got a sample code for this which is given below.

float receiveTemperature(int Temperature_type) {
	int dev = (0x5B << 1);                               // 5B is the device address
	char dataLow = 0;                              // initially setting data low as zero
    char dataHigh = 0;                             // initially setting data high as zero
	char pec = 0;
	float celcius = 0.0;

	
	
	
	start(dev | I2C_WRITE);                        // start by sending device address
	write(Temperature_type);                       // choosing ambient or object temperature
	// read
	restart(dev | I2C_READ);                       // restart 
	dataLow = read(false);                         // reading lower bytes
	dataHigh = read(false);                        // reading higher bytes
	pec = read(true);                              // reading packet error code
	stop();

	// This combines high and low bytes together and processes temperature, MSB is a error bit and is ignored for temps
	double resolutionFactor = 0.02;                // resolution factor of the MLX90614
	double temperature = 0x0000; 
	int frac;                                      // data past the decimal point

	
	temperature = (double)(((dataHigh & 0x007F) << 8) | dataLow); // removes error bit of high byte by shifting and adds the low byte 
	temperature= (temperature * resolutionFactor) - 0.01;         // multiplying temperature value with resolutionFactor 
    celcius = (float)(temperature- 273.15);

	return celcius;                                               // return the value of temperature in celcius
		
}

In this, how can I change the API to nRF TWI drivers? I know the question is so stupid. But as a new comer the nRF APIs seems to be tougher for me.

Parents
  • First off, Nordic devices dont have a I2C module, instead they have a TWI (Two Wire Interface) module, which is not exactly the same as I2C, but close enough that most often you can ignore the differences.

    When using the peripherals (TWI module) you need to decide if you wish to use the nRF SDK and which version. Then you also need to choose if you want to use the HAL (Hadware Abstraction Layer) or the Library. The HAL gives you more control over the module and is very straight forward, while the library implements more functionalities, such as queuing, but requires a bit more of getting used to.

    If you wish to use the SDK, you may find I2C/TWI examples in folder /examples/peripheral/. There are examples for HAL and for Library.

    Furthermore, the InfoCenter is a very good source of documentation.

Reply
  • First off, Nordic devices dont have a I2C module, instead they have a TWI (Two Wire Interface) module, which is not exactly the same as I2C, but close enough that most often you can ignore the differences.

    When using the peripherals (TWI module) you need to decide if you wish to use the nRF SDK and which version. Then you also need to choose if you want to use the HAL (Hadware Abstraction Layer) or the Library. The HAL gives you more control over the module and is very straight forward, while the library implements more functionalities, such as queuing, but requires a bit more of getting used to.

    If you wish to use the SDK, you may find I2C/TWI examples in folder /examples/peripheral/. There are examples for HAL and for Library.

    Furthermore, the InfoCenter is a very good source of documentation.

Children
Related