Embedded system Fun Blog
























































Find out all the best information, libraries and circuit about the latest Embedded systems.
Showing posts with label I2C. Show all posts
Showing posts with label I2C. Show all posts

Sunday, 6 January 2013

Exemple code I2C bmp085 pressure sensor

.from: https://github.com/codebendercc/arduino-files/blob/master/extra-libraries/ArduSat-I2Csensors/bmp085.cpp


X=========================X bmp085.h X=========================X


/*
bmp085.h
libary for using the I2C bmp085 pressure sensor

(c) Written by Jeroen Cappaert for NanoSatisfi, August 2012
*/


#ifndef bmp085_h
#define bmp085_h

#include <Arduino.h>

#define BAR_ADDR 0x77 // Barometric pressure sensor I2C address


class bmp085
{
 public:
   //constructor
    bmp085();
    //functions
    void configBaro();
    int bmp085ReadInt(int device, byte address);
    unsigned int baroReadUT();
    unsigned long baroReadUP();
    float getTempFromBaro(unsigned int ut);
    long getPressFromBaro(unsigned long up);
    void getBmpData(float bmp[]);
    //variables
    int ac1;
    int ac2;
    int ac3;
    unsigned int ac4;
    unsigned int ac5;
    unsigned int ac6;
    int b1;
    int b2;
    int mb;
    int mc;
    int md;
    int barOSS;
    long b5;
  private:
};


#endif


X=========================X bmp085.cpp X=========================X

/*
bmp085.cpp
libary for using the I2C bmp085 pressure sensor

(c) Written by Jeroen Cappaert for NanoSatisfi, August 2012
*/

#include <Arduino.h>
#include "bmp085.h"
#include <Wire.h>


//Constructor
bmp085::bmp085()
{
  barOSS = 2;
}

//configure barometer
void bmp085::configBaro()
{
  ac1 = bmp085ReadInt(BAR_ADDR, 0xAA);
  ac2 = bmp085ReadInt(BAR_ADDR, 0xAC);
  ac3 = bmp085ReadInt(BAR_ADDR, 0xAE);
  ac4 = bmp085ReadInt(BAR_ADDR, 0xB0);
  ac5 = bmp085ReadInt(BAR_ADDR, 0xB2);
  ac6 = bmp085ReadInt(BAR_ADDR, 0xB4);
  b1 = bmp085ReadInt(BAR_ADDR, 0xB6);
  b2 = bmp085ReadInt(BAR_ADDR, 0xB8);
  mb = bmp085ReadInt(BAR_ADDR, 0xBA);
  mc = bmp085ReadInt(BAR_ADDR, 0xBC);
  md = bmp085ReadInt(BAR_ADDR, 0xBE);
}

// user function: get pressure and temperature from sensor with one command
void bmp085::getBmpData(float bmp[])
{
 bmp[0] = getTempFromBaro(baroReadUT());
 bmp[1] = getPressFromBaro(baroReadUP());
}


// calculate temperature given ut
float bmp085::getTempFromBaro(unsigned int ut)
{
  long x1, x2;
  
  x1 = (((long)ut - (long)ac6)*(long)ac5) >> 15;
  x2 = ((long)mc << 11)/(x1 + md);
  b5 = x1 + x2;

  return (float) ((b5 + 8)>>4)/10.;
}


// Calculate pressure given up
// calibration values must be known
// b5 is also required so bmp085GetTemperature(...) must be called first.
// Value returned will be pressure in units of Pa.
long bmp085::getPressFromBaro(unsigned long up)
{
  long x1, x2, x3, b3, b6, p;
  unsigned long b4, b7;
  
  b6 = b5 - 4000;
  // Calculate B3
  x1 = (b2 * (b6 * b6)>>12)>>11;
  x2 = (ac2 * b6)>>11;
  x3 = x1 + x2;
  b3 = (((((long)ac1)*4 + x3)<<barOSS) + 2)>>2;
  
  // Calculate B4
  x1 = (ac3 * b6)>>13;
  x2 = (b1 * ((b6 * b6)>>12))>>16;
  x3 = ((x1 + x2) + 2)>>2;
  b4 = (ac4 * (unsigned long)(x3 + 32768))>>15;
  
  b7 = ((unsigned long)(up - b3) * (50000>>barOSS));
  if (b7 < 0x80000000)
    p = (b7<<1)/b4;
  else
    p = (b7/b4)<<1;
    
  x1 = (p>>8) * (p>>8);
  x1 = (x1 * 3038)>>16;
  x2 = (-7357 * p)>>16;
  p += (x1 + x2 + 3791)>>4;
  
  return p;
}


// read uncorrected temperature value from bmp085
unsigned int bmp085::baroReadUT()
{
  unsigned int ut;
  
  // Write 0x2E into Register 0xF4
  // This requests a temperature reading
  Wire.beginTransmission(BAR_ADDR);
  Wire.write(0xF4);
  Wire.write(0x2E);
  Wire.endTransmission();
  
  // Wait at least 4.5ms
  delay(5);
  
  // Read two bytes from registers 0xF6 and 0xF7
  ut = bmp085ReadInt(BAR_ADDR,0xF6);
  return ut;
}


// read uncorrected pressure value from bmp085
unsigned long bmp085::baroReadUP()
{
  unsigned char msb, lsb, xlsb;
  unsigned long up = 0;
  
  // Write 0x34+(OSS<<6) into register 0xF4
  // Request a pressure reading w/ oversampling setting
  Wire.beginTransmission(BAR_ADDR);
  Wire.write(0xF4);
  Wire.write(0x34 + (barOSS<<6));
  Wire.endTransmission();
  
  // Wait for conversion, delay time dependent on OSS
  delay(2 + (3<<barOSS));
  
  // Read register 0xF6 (MSB), 0xF7 (LSB), and 0xF8 (XLSB)
  Wire.beginTransmission(BAR_ADDR);
  Wire.write(0xF6);
  Wire.endTransmission();
  Wire.requestFrom(BAR_ADDR,3);
  
  // Wait for data to become available
  while(Wire.available() < 3)
    ;
  msb = Wire.read();
  lsb = Wire.read();
  xlsb = Wire.read();
  
  up = (((unsigned long) msb << 16) | ((unsigned long) lsb << 8) | (unsigned long) xlsb) >> (8-barOSS);
  
  return up;
}



// read integer on bmp085
int bmp085::bmp085ReadInt(int device, byte address)
{
  unsigned char msb, lsb;
  
  Wire.beginTransmission(device);
  Wire.write(address);
  Wire.endTransmission();
  
  Wire.requestFrom(device, 2);
  while(Wire.available()<2)
    ;
  msb = Wire.read();
  lsb = Wire.read();
  
  return (int) msb<<8 | lsb;
}



X=========================X X=========================X

Example Code for a TMP102 I2c Thermometer sensor

.from:  https://github.com/codebendercc/arduino-files/blob/master/extra-libraries/ArduSat-I2Csensors/tmp102.cpp


X------------------------X tmp102.h X-------------------------X




/*
mag3110.h
libary for using the I2C tmp102 temperature sensor

(c) Written by Jeroen Cappaert for NanoSatisfi, August 2012
*/

#ifndef tmp102_h
#define tmp102_h

#include <Arduino.h>

#define TEMP_ADDR 0x48 // Temp-sensor data register


class tmp102
{
  public:
    tmp102();
   float getTemperature();
  private:
};



#endif

X------------------------X tmp102.cpp X-------------------------X




/*
tmp102.cpp
libary for using the I2C tmp102 temperature sensor

(c) Written by Jeroen Cappaert for NanoSatisfi, August 2012
*/

#include <Arduino.h>
#include "tmp102.h"
#include <Wire.h>

//Constructor
tmp102::tmp102()
{
  
}


// get temperature value
float tmp102::getTemperature()
{
  Wire.requestFrom(TEMP_ADDR,2);

  byte MSB = Wire.read();
  byte LSB = Wire.read();

  //it's a 12bit int, using two's compliment for negative
  int TemperatureSum = ((MSB << 8) | LSB) >> 4;

  float celsius = TemperatureSum*0.0625;
  return celsius;
}
X-------------------------------------------------X

Sunday, 8 January 2012

MBED: Read BH1751FVI Digital Light Sensor IC using I2C (CookBook)

.from: mbed.org/users/hasegawa00/notebook/digital-light-sensor-ic-bh1751fvi/

.datasheet: http://www.rohm.com/products/databook/sensor/pdf/bh1751fvi-e.pdf

/media/uploads/hasegawa00/_scaled_201111031116000.jpg


XxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXx main.cpp XxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXx

#include "mbed.h"

I2C i2c(p9, p10);        // sda, scl
Serial pc(USBTX, USBRX); // tx, rx
DigitalOut myled1(LED1);
DigitalOut myled2(LED2);

const int addr = 0x46; // define the I2C Address L
int mode = 1, mode0;
float echo, bai = 1.0;
int main() {
    char cmd[2];
    char cmdr[2];

    cmd[0] = 0x01;           // Power On
    i2c.write(addr, cmd, 1);
    cmd[0] = 0x07;           // Reset
    i2c.write(addr, cmd, 1);
    cmd[0] = 0x42;           // Typ Upper-bit
    i2c.write(addr, cmd, 1);
    cmd[0] = 0x65;           // Typ Lower-bit
    i2c.write(addr, cmd, 1);
    cmd[0] = 0x10;           // H-resolutiom Mode
    i2c.write(addr, cmd, 1);

    myled1 = 1;
    while(1) {
        myled2 = 1;
        wait(0.18);              // 
        if(mode == 0) wait(0.54);
        i2c.read(addr, cmdr, 2); // read the 2-byte echo result
        echo = ((cmdr[0] << 8) + cmdr[1]) / 1.2 * bai;

        mode0 = mode;
        if(mode == 0){
            if(echo >= 6000){
                mode = 2;
            }else if(echo >= 1000){
                mode = 1;
            }
        }
        if(mode == 1){
            if(echo < 100){
                mode = 0;
            }
            if(echo >= 30000){
                mode = 2;
            }
        }
        if(mode == 2){
            if(echo < 60){
                mode = 0;
            }else if(echo < 3000){
                mode = 1;
            }
        }
        if(mode != mode0){
            if(mode == 0){
                cmd[0] = 0x47;           // Max Upper-bit
                i2c.write(addr, cmd, 1);
                cmd[0] = 0x7E;           // Max Lower-bit
                i2c.write(addr, cmd, 1);
                cmd[0] = 0x11;           // H-resolutiom Mode2
                i2c.write(addr, cmd, 1);
                bai = (69.0 / 254.0) / 2.0;
            }
            if(mode == 1){
                cmd[0] = 0x42;           // Typ Upper-bit
                i2c.write(addr, cmd, 1);
                cmd[0] = 0x65;           // Typ Lower-bit
                i2c.write(addr, cmd, 1);
                cmd[0] = 0x10;           // H-resolutiom Mode
                i2c.write(addr, cmd, 1);
                bai = 1.0;
            }
            if(mode == 2){
                cmd[0] = 0x40;           // Min Upper-bit
                i2c.write(addr, cmd, 1);
                cmd[0] = 0x7F;           // Min Lower-bit
                i2c.write(addr, cmd, 1);
                cmd[0] = 0x10;           // H-resolutiom Mode
                i2c.write(addr, cmd, 1);
                bai = 69.0 / 31.0;
            }
            wait(2.0);              // 
            i2c.read(addr, cmdr, 2); // read the 2-byte echo result
            echo = ((cmdr[0] << 8) + cmdr[1]) / 1.2 * bai;
        }

        pc.printf("light = %.2f mode = %d bai = %.2f\r\n", echo, mode, bai);
        myled2 = 0;
        wait(0.82);
    }

XxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXx EOF XxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXxXx

MBED: Read TMP102 Temperature Sensor using I2C (Cookbook)

.from: http://mbed.org/users/donatien/programs/TMP102/5z9h3

The TMP102 is another I2C digital temperature sensor in a small SOT563 package
/media/uploads/chris/tmp102_crop.jpg
TMP102mbed
1 - Vcc (square pad)Vout
2 - SDAp9
3 - SCLp10
4 - GndGnd

XxXxXxXxXxXxXxXxXxXxXxXxXxXx TMP102.cpp XxXxXxXxXxXxXxXxXxXxXxXxXxXx


/*
Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include "TMP102.h"

#define TEMP_REG_ADDR 0x00

TMP102::TMP102(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr)
{

}

TMP102::~TMP102()
{

}

float TMP102::read()
{
  I2C temperatureIn(p9, p10);
 
  const char tempRegAddr = TEMP_REG_ADDR;

  m_i2c.write(m_addr, &tempRegAddr, 1); //Pointer to the temperature register

  char reg[2] = {0,0};
  m_i2c.read(m_addr, reg, 2); //Rea
 
  unsigned short res = (reg[0] << 4) | (reg[1] >> 4);
 
  float temp =  (float) ((float)res * 0.0625);
  
  return temp;
}

XxXxXxXxXxXxXxXxXxXxXxXxXxXx TMP102.h XxXxXxXxXxXxXxXxXxXxXxXxXxXx


/*
Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#ifndef TMP102_H
#define TMP102_H

#include "mbed.h"

//!Library for the TI TMP102 temperature sensor.
/*!
The TMP102 is an I2C digital temperature sensor in a small SOT563 package, with a 0.0625C resolution and 0.5C accuracy.
*/
class TMP102
{
public:
  //!Creates an instance of the class.
  /*!
  Connect module at I2C address addr using I2C port pins sda and scl.
  TMP102
  \param addr <table><tr><th>A0 pin connection</th><th>Address</th></tr><tr><td>GND</td><td>0x90</td></tr><tr><td>V+</td><td>0x92</td></tr><tr><td>SDA</td><td>0x94</td></tr><tr><td>SCL</td><td>0x96</td></tr></table> 
  */
  TMP102(PinName sda, PinName scl, int addr);
 
  /*!
  Destroys instance.
  */
  ~TMP102();
 
  //!Reads the current temperature.
  /*!
  Reads the temperature register of the TMP102 and converts it to a useable value.
  */
  float read();
 
private:
  I2C m_i2c;
  int m_addr;

};

#endif

XxXxXxXxXxXxXxXxXxXxXxXxXxXx EOF XxXxXxXxXxXxXxXxXxXxXxXxXxXx