CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

BME280 - Kinda working maybe Driver

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

BME280 - Kinda working maybe Driver
PostPosted: Mon Apr 02, 2018 6:26 am     Reply with quote

Hi All,

I'm posting here my "working" efforts to get the BME280 working.
I get all 3 readings, but i really don't trust any of the values received although they are in the right ranges.

Any calculation relying on Dig_5 and Dig_4 I don't trust. You can see i have tried many interpretations of the formula to calculate these values...

All my mistakes and corrections are included commented out or not...

Overall I've had a terrible experience trying get this to work. I can't say I've put in "the extra mile" to get this to work properly... and by now i hate the sensor.

The idea of this post is for this to be a starting point and for others to contribute to get this to work...


Cludge Code Ahead:
Code:

#define BME280 0x77      //The I2C Address of the Device


void Write_BME_Register(int,int,int);
int Read_BME_Register(int,int);
void Get_BME_Data(int, int);

void Setup_BME();
void Calculate_Temp();
void Calculate_Hum();
void Calculate_Pess();

float Temperature;
signed int32 t_fine;
int32 Pressure;

int32 rawpres;
int32 rawtemp;
int32 rawhum;


int16 Dig_T1;
signed int16 Dig_T2;
signed int16 Dig_T3;

int16 Dig_H1;
signed int16 Dig_H2;
int16 Dig_H3;
signed int16 Dig_H4;
signed int16 Dig_H5;
signed int16 Dig_H6;

int16 Dig_P1;
signed int16 Dig_P2;
signed int16 Dig_P3;
signed int16 Dig_P4;
signed int16 Dig_P5;
signed int16 Dig_P6;
signed int16 Dig_P7;
signed int16 Dig_P8;
signed int16 Dig_P9;

void Setup_BME()
{   
   Write_BME_Register(BME280,0xF4,0x00);   //Put it in sleep mode
   delay_ms(2000);
   Write_BME_Register(BME280,0xF5,0x84);   //Set Config Word
   Write_BME_Register(BME280,0xF2,0x04);   //Set Humidity Control
   Write_BME_Register(BME280,0xF4,0x93);   //Set Temp & Press Control + Mode

   Dig_T1 = make16 (Read_BME_Register(BME280,0x89),Read_BME_Register(BME280,0x88));
   Dig_T2 = make16 (Read_BME_Register(BME280,0x8B),Read_BME_Register(BME280,0x8A));
   Dig_T3 = make16 (Read_BME_Register(BME280,0x8D),Read_BME_Register(BME280,0x8C));

   Dig_H1 = Read_BME_Register(BME280,0xA1);
   Dig_H2 = make16 (Read_BME_Register(BME280,0xE2),Read_BME_Register(BME280,0xE1));
   Dig_H3 = Read_BME_Register(BME280,0xE3);

   Dig_H4=(((int16)Read_BME_Register(BME280,0xE4)<<4)|(Read_BME_Register(BME280,0xE5)& 0x0F)); //SO FAR BEST
   Dig_H5 = make16((Read_BME_Register(BME280,0xE5)>>4), Read_BME_Register(BME280,0xE6));

//   Dig_H4 = make16((Read_BME_Register(BME280,0xE4)<<4),(Read_BME_Register(BME280,0xE5)& 0x0F));
//   Dig_H5 = make16((Read_BME_Register(BME280,0xE6)<<4), ((Read_BME_Register(BME280,0xE5)>>4)&0x0F));

//    Dig_H4 = (Read_BME_Register(BME280,0xE4)<<4|(Read_BME_Register(BME280,0xE5) & 0x0F));
//   Dig_H5 = (Read_BME_Register(BME280,0xE6)<<4|(Read_BME_Register(BME280,0xE5) >>4));
//   Dig_H5 = make16((Read_BME_Register(BME280,0xE6)<<4),(Read_BME_Register(BME280,0xE5) >>4)); //SO FAR BEST

   Dig_H6 = Read_BME_Register(BME280,0xE7);

   Dig_P1 = make16 (Read_BME_Register(BME280,0x8F),Read_BME_Register(BME280,0x8E));
   Dig_P2 = make16 (Read_BME_Register(BME280,0x91),Read_BME_Register(BME280,0x90));
   Dig_P3 = make16 (Read_BME_Register(BME280,0x93),Read_BME_Register(BME280,0x92));
   Dig_P4 = make16 (Read_BME_Register(BME280,0x95),Read_BME_Register(BME280,0x94));
   Dig_P5 = make16 (Read_BME_Register(BME280,0x97),Read_BME_Register(BME280,0x96));
   Dig_P6 = make16 (Read_BME_Register(BME280,0x99),Read_BME_Register(BME280,0x98));
   Dig_P7 = make16 (Read_BME_Register(BME280,0x9B),Read_BME_Register(BME280,0x9A));
   Dig_P8 = make16 (Read_BME_Register(BME280,0x9D),Read_BME_Register(BME280,0x9C));
   Dig_P9 = make16 (Read_BME_Register(BME280,0x9F),Read_BME_Register(BME280,0x9E));
}

void Write_BME_Register(int Dev_Read_Add, int Register_add, int Data)
{
   //int readbit =1;
   //int writebit =0;
   int tempaddress = 0;

   tempaddress = Dev_Read_Add <<1;    //Left Shift 1 bit to allow space for R/W bit
   //tempaddress |= writebit;         //OR the shifted address with the WRITE Bit
   i2c_start();
   i2c_write(tempaddress);          //Address the device with shifted address with WRITE bit set
   i2c_write(Register_add);         //Tell the Device Which Register you want to read
   i2c_write(Data);   
   i2c_stop();
}

int Read_BME_Register(int Dev_Read_Add, int Register_add)
{
   //int readbit=1;
   //int writebit=0;
   int tempaddress=0;
   int Result_Byte=0;

   tempaddress = Dev_Read_Add <<1;    //Left Shift 1 bit to allow space for R/W bit
//   tempaddress |= 0x00;         //OR the shifted address with the WRITE Bit

   i2c_start();                  //Start I2C sequence
   i2c_write(tempaddress);          //Address the device with shifted address with WRITE bit set
   i2c_write(Register_add);         //Tell the Device Which Register you want to read
   
//   tempaddress = Dev_Read_Add <<1;    //Left Shift 1 bit to allow space for R/W bit
   tempaddress |= 0x01;            //OR the shifted address with the READ Bit

   i2c_start();                  //Start I2C sequence
   i2c_write(tempaddress);          //Address the device with shifted address with READ bit set
   Result_Byte = i2c_read(0);         //READ the data onto a Variable - Device ID in this case
   i2c_stop();                     //Stop The I2C transaction
   
   //fprintf(lcd_putc,"Register:%X\n\r", Result_Byte);    //Print your Data --- For the Win.

   return(Result_Byte);
}

void Calculate_Pess()
{
   float var1, var2, p;
   
   var1 = ((float)t_fine/2.0) - 64000.0;
   var2 = var1 * var1 * ((float)Dig_P6) / 32768.0;
   var2 = var2 + var1 * ((float)Dig_P5) * 2.0;
   var2 = (var2/4.0)+(((float)Dig_P4) * 65536.0);
   var1 = (((float)Dig_P3) * var1 * var1 / 524288.0 + ((float)Dig_P2) * var1) / 524288.0;
   var1 = (1.0 + var1 / 32768.0)*((float)Dig_P1);
   if (var1 == 0.0)
   {
   //return 0; // avoid exception caused by division by zero
   }
   p = 1048576.0 - (float)rawpres;
   p = (p - (var2 / 4096.0)) * 6250.0 / var1;
   var1 = ((float)Dig_P9) * p * p / 2147483648.0;
   var2 = p * ((float)Dig_P8) / 32768.0;
   p = p + (var1 + var2 + ((float)Dig_P7)) / 16.0;

   fprintf(lcd_putc,"PRESSURE: %4.2f\n\r",(p/100.0));    //Print your Data --- For the Win.

}

void Calculate_Hum()
{
   int32 var1;

   var1 = (t_fine - ((int32)76800));
   var1 = (((((rawhum << 14) - (((int32)Dig_H4) << 20) - (((int32)Dig_H5) * var1)) + ((int32)16384)) >> 15) * (((((((var1 * ((int32)Dig_H6)) >> 10) * (((var1 * ((int32)Dig_H3)) >> 11) + ((int32)32768))) >> 10) + ((int32)2097152)) *((int32)Dig_H2) + 8192) >> 14));
   var1 = (var1 - (((((var1 >> 15) * (var1 >> 15)) >> 7) * ((int32)Dig_H1)) >> 4));
   var1 = (var1 < 0 ? 0 : var1);
   var1 = (var1 > 419430400 ? 419430400 : var1);

   var1= var1>>12;
   fprintf(lcd_putc,"HUMIDITY: %3.2f \n\r",((float)var1/1024.0));    //Print your Data --- For the Win.
}


void Calculate_Temp()
{
   float var1, var2, T;

   var1 = (((float)rawtemp)/16384.0 - ((float)Dig_T1)/1024.0) * ((float)Dig_T2);
   var2 = ((((float)rawtemp)/131072.0 - ((float)Dig_T1)/8192.0) *
   (((float)rawtemp)/131072.0 - ((float) Dig_T1)/8192.0)) * ((float)Dig_T3);
   t_fine = (signed int32)(var1 + var2);
   Temperature = (var1 + var2) / 5120.0;

   fprintf(lcd_putc,"TEMPERATURE: %3.2f \n\r",Temperature);    //Print your Data --- For the Win.
}

void Get_BME_Data(int Dev_Read_Add, int Register_add)
{
//   int readbit =1;
//   int writebit =0;
   int tempaddress = 0;
   int Temp[8];

   tempaddress = Dev_Read_Add <<1;    //Left Shift 1 bit to allow space for R/W bit
//   tempaddress |= writebit;         //OR the shifted address with the WRITE Bit
   i2c_start();                  //Start I2C sequence
   i2c_write(tempaddress);          //Address the device with shifted address with WRITE bit set
   i2c_write(Register_add);         //Tell the Device Which Register you want to read
   
//   tempaddress = Dev_Read_Add <<1;    //Left Shift 1 bit to allow space for R/W bit
   tempaddress |= 0x01;            //OR the shifted address with the READ Bit
   i2c_start();                  //Start I2C sequence
   i2c_write(tempaddress);          //Address the device with shifted address with READ bit set
   Temp[0]= i2c_read(1);            //0xF7
   Temp[1]= i2c_read(1);            //0xF8
   Temp[2]= i2c_read(1);            //0xF9
   Temp[3]= i2c_read(1);            //0xFa
   Temp[4]= i2c_read(1);            //0xFb
   Temp[5]= i2c_read(1);            //0xFc
   Temp[6]= i2c_read(1);            //0xFd
   Temp[7]= i2c_read(0);            //0xFe
   i2c_stop();                     //Stop The I2C transaction
   

   rawpres = make32(0x00, Temp[0], Temp[1],Temp[2]);
    rawpres >>= 4;

   rawtemp = make32(0x00, Temp[3], Temp[4],Temp[5]);
    rawtemp >>= 4;

   rawhum = make32(0x00,0x00,Temp[6],Temp[7]);

}


G.
_________________
CCS PCM 5.078 & CCS PCH 5.093
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Mon Apr 02, 2018 6:33 am     Reply with quote

To use the above:

Call the following functions in this order:

Code:
   Setup_BME();
   Get_BME_Data(BME280, 0xF7);
   Calculate_Temp();
   Calculate_Hum();
   Calculate_Pess();

_________________
CCS PCM 5.078 & CCS PCH 5.093
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Sat Jul 14, 2018 6:56 am     Reply with quote

Has anyone actually confirmed this driver works?
I would certainly appreciate a second pair of eyes on this... its a cool sensor but i had a lot of issues and not enough time to get it right.

G.
_________________
CCS PCM 5.078 & CCS PCH 5.093
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Aug 05, 2018 2:35 am     Reply with quote

Probably no one has replied because CCS released their own driver file,
bme280.c, on 3/29/2016. It's included with the compiler.
Based on the release date, it would first be available in vs. 5.057, which
came out on 4/14/2016.

The CCS driver is fairly complicated. It has lots of math using signed
integers. It doesn't use floats.

Here is the file header:
Code:
///////////////////////////////////////////////////////////////////////////
////                                                                   ////
////                             bme280.c                              ////
////                                                                   ////
//// Driver for Bosch BME280 Environmental sensor.  This sensor can    ////
//// read temperature, pressure and humidity.  This driver returns     ////
//// calibrated/compensated values by using the trim values on the     ////
//// sensor.  At this time this driver only supports I2C mode, it      ////
//// doesn't support SPI mode.                                         ////
////                                                                   ////
////                                                                   
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Sun Aug 05, 2018 12:13 pm     Reply with quote

..... well that makes sense.

Thanks for the heads up.
I purchased 5.078 but im still running 5.012 until i deliver the project im currently working on.

Thanks
G.
_________________
CCS PCM 5.078 & CCS PCH 5.093
shalts



Joined: 20 Oct 2006
Posts: 17
Location: Ankara

View user's profile Send private message Send e-mail

PostPosted: Sat Nov 03, 2018 7:58 am     Reply with quote

PCM programmer wrote:
Probably no one has replied because CCS released their own driver file,
bme280.c, on 3/29/2016. It's included with the compiler.
Based on the release date, it would first be available in vs. 5.057, which
came out on 4/14/2016.


Hello,
So, does this mean if I update my compiler I will have this library included in my "drivers" folder ?
I have 5.026 right now.

Or, is there another way to get this library from CCS ?

Shalt
_________________
Murat Shalt Unal
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Nov 03, 2018 12:30 pm     Reply with quote

shalts wrote:


So, does this mean if I update my compiler I will have this library included in my "drivers" folder ?
I have 5.026 right now.

That is correct.

shalts wrote:

Or, is there another way to get this library from CCS ?

You could email CCS support and politely ask them if they would give
it to you. They might do it for one file.
shalts



Joined: 20 Oct 2006
Posts: 17
Location: Ankara

View user's profile Send private message Send e-mail

PostPosted: Sat Nov 03, 2018 3:23 pm     Reply with quote

Oh. I didn't think that.

That would be really great ! Thanks a lot for your advice sir. I will try that on Monday.

On the other hand I am searching for the updates above my current version. Would be good to buy a 1 year update too.

Thanks a lot !
_________________
Murat Shalt Unal
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group