Wire.h Problem

Go To Last Post
13 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

Hello everyone.

 

I have a code written in C programming language.  github.com/kghunt/NWatch

I also want to get temperature and pressure data with the BMP280 sensor. However, the code does not allow this, it has created its own i2c library and used TWI.c. Because the Wire library is written in C ++, which creates a problem.

 

My code 

#include<Wire.h>

// BMP280 I2C address is 0x76(108)
#define Addr 0x76

void setup()
{
  // Initialise I2C communication as MASTER
  Wire.begin();
  // Initialise Serial communication, set baud rate = 9600
  Serial.begin(9600);
}

void loop()
{
  unsigned int b1[24];
  unsigned int data[8];
  for (int i = 0; i < 24; i++)
  {
    // Start I2C Transmission
    Wire.beginTransmission(Addr);
    // Select data register
    Wire.write((136 + i));
    // Stop I2C Transmission
    Wire.endTransmission();

    // Request 1 byte of data
    Wire.requestFrom(Addr, 1);

    // Read 1 byte of data
    if (Wire.available() == 1)
    {
      b1[i] = Wire.read();
    }
  }
  // Convert the data
  // temp coefficients
  unsigned int dig_T1 = (b1[0] & 0xFF) + ((b1[1] & 0xFF) * 256);
  int dig_T2 = b1[2] + (b1[3] * 256);
  int dig_T3 = b1[4] + (b1[5] * 256);

  // pressure coefficients
  unsigned int dig_P1 = (b1[6] & 0xFF) + ((b1[7] & 0xFF) * 256);
  int dig_P2 = b1[8] + (b1[9] * 256);
  int dig_P3 = b1[10] + (b1[11] * 256);
  int dig_P4 = b1[12] + (b1[13] * 256);
  int dig_P5 = b1[14] + (b1[15] * 256);
  int dig_P6 = b1[16] + (b1[17] * 256);
  int dig_P7 = b1[18] + (b1[19] * 256);
  int dig_P8 = b1[20] + (b1[21] * 256);
  int dig_P9 = b1[22] + (b1[23] * 256);

  // Start I2C Transmission
  Wire.beginTransmission(Addr);
  // Select control measurement register
  Wire.write(0xF4);
  // Normal mode, temp and pressure over sampling rate = 1
  Wire.write(0x27);
  // Stop I2C Transmission
  Wire.endTransmission();

  // Start I2C Transmission
  Wire.beginTransmission(Addr);
  // Select config register
  Wire.write(0xF5);
  // Stand_by time = 1000ms
  Wire.write(0xA0);
  // Stop I2C Transmission
  Wire.endTransmission();

  for (int i = 0; i < 8; i++)
  {
    // Start I2C Transmission
    Wire.beginTransmission(Addr);
    // Select data register
    Wire.write((247 + i));
    // Stop I2C Transmission
    Wire.endTransmission();

    // Request 1 byte of data
    Wire.requestFrom(Addr, 1);

    // Read 1 byte of data
    if (Wire.available() == 1)
    {
      data[i] = Wire.read();
    }
  }

  // Convert pressure and temperature data to 19-bits
  long adc_p = (((long)(data[0] & 0xFF) * 65536) + ((long)(data[1] & 0xFF) * 256) + (long)(data[2] & 0xF0)) / 16;
  long adc_t = (((long)(data[3] & 0xFF) * 65536) + ((long)(data[4] & 0xFF) * 256) + (long)(data[5] & 0xF0)) / 16;

  // Temperature offset calculations
  double var1 = (((double)adc_t) / 16384.0 - ((double)dig_T1) / 1024.0) * ((double)dig_T2);
  double var2 = ((((double)adc_t) / 131072.0 - ((double)dig_T1) / 8192.0) *
                 (((double)adc_t) / 131072.0 - ((double)dig_T1) / 8192.0)) * ((double)dig_T3);
  double t_fine = (long)(var1 + var2);
  double cTemp = (var1 + var2) / 5120.0;
  double fTemp = cTemp * 1.8 + 32;

  // Pressure offset calculations
  var1 = ((double)t_fine / 2.0) - 64000.0;
  var2 = var1 * var1 * ((double)dig_P6) / 32768.0;
  var2 = var2 + var1 * ((double)dig_P5) * 2.0;
  var2 = (var2 / 4.0) + (((double)dig_P4) * 65536.0);
  var1 = (((double) dig_P3) * var1 * var1 / 524288.0 + ((double) dig_P2) * var1) / 524288.0;
  var1 = (1.0 + var1 / 32768.0) * ((double)dig_P1);
  double p = 1048576.0 - (double)adc_p;
  p = (p - (var2 / 4096.0)) * 6250.0 / var1;
  var1 = ((double) dig_P9) * p * p / 2147483648.0;
  var2 = p * ((double) dig_P8) / 32768.0;
  double pressure = (p + (var1 + var2 + ((double)dig_P7)) / 16.0) / 100;

  // Output data to serial monitor
  Serial.print("Pressure : ");
  Serial.print(pressure);
  Serial.println(" hPa");
  Serial.print("Temperature in Celsius : ");
  Serial.print(cTemp);
  Serial.println(" C");
  Serial.print("Temperature in Fahrenheit : ");
  Serial.print(fTemp);
  Serial.println(" F");
  delay(1000);
}

I don't want to use Wire.h library. I worked datasheet a few days, but i couldn't handle it.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The code you reference has i2c.c. Convert the wire functions to those that i2c.c gives you.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

As you said yes, I worked on this issue for a few days. As far as I understand, I am not very good at this. I just couldn't succeed.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ahmetkcavusoglu wrote:
As far as I understand, I am not very good at this. I just couldn't succeed.

That's very defeatist. What investigation did you do? 

My first suggestion would be to read up on i2C/twi protocol. It's quite simple.

 

// Start I2C Transmission
  Wire.beginTransmission(Addr);
  // Select control measurement register
  Wire.write(0xF4);
  // Normal mode, temp and pressure over sampling rate = 1
  Wire.write(0x27);
  // Stop I2C Transmission
  Wire.endTransmission();
  
  
  becomes something like this:
  
  // Start I2C Transmission
  i2c_start();
  i2c_write(Addr); 
  // Select control measurement register
  i2c_write(0xF4); 
  // Normal mode, temp and pressure over sampling rate = 1
  i2c_write(0x27); 
  // Stop I2C Transmission
  i2c_stop(); 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:

ahmetkcavusoglu wrote:
As far as I understand, I am not very good at this. I just couldn't succeed.

That's very defeatist. What investigation did you do? 

My first suggestion would be to read up on i2C/twi protocol. It's quite simple.

 

// Start I2C Transmission
  Wire.beginTransmission(Addr);
  // Select control measurement register
  Wire.write(0xF4);
  // Normal mode, temp and pressure over sampling rate = 1
  Wire.write(0x27);
  // Stop I2C Transmission
  Wire.endTransmission();
  
  
  becomes something like this:
  
  // Start I2C Transmission
  i2c_start();
  i2c_write(Addr); 
  // Select control measurement register
  i2c_write(0xF4); 
  // Normal mode, temp and pressure over sampling rate = 1
  i2c_write(0x27); 
  // Stop I2C Transmission
  i2c_stop(); 

 

yeah it's quite simple but one of them was using twi.c other one was wire.cpp. it's difficult to change.

i want to see bmp280 with twi.h library

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ahmetkcavusoglu wrote:
it's difficult to change.

It shouldn't be.

 

The  bmp280 - like any other device - neither knows nor cares what library you use: all it sees are the activities on the wires of the physical I2C connection - START conditions, STOP conditions, Slave Address being sent, data, ACK/NACK, etc.

 

So what you need to do is to look at your libraries and understand how they achieve those things ...

 

Kartman wrote:
read up on i2C/twi protocol. It's quite simple 

Indeed. And it is very well-established and widely-used - so there is plenty of reference information available; eg,

 

The Official I2C Specification: https://www.nxp.com/docs/en/user-guide/UM10204.pdf

 

Others:

 

https://en.wikipedia.org/wiki/I2C

 

https://learn.sparkfun.com/tutorials/i2c/all

 

https://www.i2c-bus.org/

 

https://www.robot-electronics.co.uk/i2c-tutorial

 

https://www.digikey.co.uk/en/articles/why-the-inter-integrated-circuit-bus-makes-connecting-ics-so-easy

 

etc, etc, ...

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Wed. Sep 16, 2020 - 08:05 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm confused. You posted a link to the xwatch code. It has its own i2c/twi code in i2c.c. An example of its use is in ds3231.c

So what is this twi.h you mention?

 

 

I gave you a head start in using the functions in the project you linked to. So what is missing?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:

I'm confused. You posted a link to the xwatch code. It has its own i2c/twi code in i2c.c. An example of its use is in ds3231.c

So what is this twi.h you mention?

 

 

I gave you a head start in using the functions in the project you linked to. So what is missing?

 

i want to add bmp280 sensor, but i didn't understand watch code

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

ahmetkcavusoglu wrote:
i want to add bmp280 sensor, but i didn't understand watch code

 

You need to understand the watch code first before you even think about adding bmp280. You might want to think of a simpler project first before tackling a larger one.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:

ahmetkcavusoglu wrote:
i want to add bmp280 sensor, but i didn't understand watch code

 

You need to understand the watch code first before you even think about adding bmp280. You might want to think of a simpler project first before tackling a larger one.

You're absolutely right. I wrote it here so that maybe I can get help from someone. Actually I know I'm not good yet.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Oh, you want someone to do all the hard work on a project we have no interest in. Sorry, I thought you wanted assistance to do the task yourself. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

I didn't mean it that way, we are probably having trouble communicating as my native language is not English. I just wanted to be guided by me and you provided them. Thank you.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

A couple of things I saw, one, the code in i2c.c posted in your other thread, shows setting the I2C bus speed to 400kHz, not all I2C devices run that fast and in most cases that high of a speed is not needed.  Try setting the speed to 50kHz.

Also I2C bus needs pull up resistors, for 5v busses, 4k7 resistors should work well for 100kHz or less speed, for higher speed busses you may need stronger pullups.

Having a logic analyzer or DSO is a must have, so you can see if the buss is working as expected. A Logic Analyzer can be had at low cost, a DSO is usually a higher priced item, both are valuable to have when doing embedded development.

 

Jim

 

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"