Menu

BME280 SPI

2017-11-11
2018-05-04
1 2 3 4 > >> (Page 1 of 4)
  • David Thompson

    David Thompson - 2017-11-11

    Follow on from open discussion "Decimal Point Sums"
    Using MMote's equation I have Temperature Working but I haven't checked what happens with a Negative temp.
    I've been trying to break down the equation for Humidity, it's too big for GCB.
    I've sort of got the equation working but despite numerous checks, I cannot get the correct result. It's about X86 too big. You would think that an error of that size should be easy to find, but there's some big numbers being processed.

    I've come to a brick wall, In the hope that someone can spot what I can't, here is my work so far.
    A spreadsheet and word document.
    I've used colours to identify the parenethesis.
    The gcb file although this shouldn't be the problem because changing ADC_H, T_fine or any of the calibration values doesn't make enough of a difference unless it is a rediculous change.
    Data from an I2C connected BME280 will work in the spreadsheet.

     

    Last edit: David Thompson 2017-11-11
  • mmotte

    mmotte - 2017-11-11

    David and programmers in the bush,

    Bought three of the BME280's , sucked the calibration parameters out, read the adc values, and put it into a spreadsheet. Nothing seems to work even in the spread sheet.

    So I went back to basics and read the bytes in as bytes and then nailed them together as signed and unsigned words as specified. i did find an error in the BME280.h (renamed BMP280), copy and paste error where the variable wasn't numbered correctly. Ahh ha that's got to be it!! No luck. it wasn't.

    So reading the BME280.pdf spec sheet in their example I2C read they start at 0xF6 instead of 0xF7 . Wonder why they start at F6 because the register chart doen't even have F6?? Yes i am grasping for any hold. So I read the adc values as bytes and checked to make sure the third and sixth value had low nibble 0b000 zeros. Hand assembled the adc values with the shift. Everything came out the same as the coding I was using..

    It bothers me that the numbers don't calculate in the spread sheet!! I used the floating point calc and the 32bit calc and they come out the same until the end but none of them have the right answer(local conditions). I am having a local mathmetician looking at this too to find my errors. We can't write code that works until we got the math right. Actually the GCB code comes out similar to the spreadsheet's wrong answers.

    My long history of calibrating process equipment, i had to gather adc_P values and Barometer values for a couple days and regress a line that fits pretty well. This is very device specific. I was hoping correalate the line parameters to the calibration parameter(not hopeful). For my device #3 I came up with y(barometer inHg) = - 0.0000497 * adc_P +46.87 and this is very close at room temperature. I don't know how temperature is going to affect it. That is the next test. You say GCB don't do float but this formula can be put into GCB. Code follows. No temperature correction and only device #3 but you could do the same process for any device.

    sub CalcPress
        Press = (4687 - (adc_P / 200))  *3386
    end sub
    

    I have not tried to decode the humidity yet but should. i will work on that this week. I keep bumping into walls and checking that I am using good "GARBAGE IN".

    I will post my spread sheets and programs. Remember this BME280.h library is improved.

     
  • mmotte

    mmotte - 2017-11-11

    Maybe you can' post mult file or my internet is screwing up

     
  • mmotte

    mmotte - 2017-11-11

    Here is the spreadsheet

     
  • David Thompson

    David Thompson - 2017-11-12

    Hi Mike
    Well at least I'm not alone!. I've also tried a variety of variable types etc. makes no difference. It's the magnitude of the error that I find most off putting, especially as I can't even reproduce manually a set of figures that come out with the correct result. I have't tried going backwards yet.
    The last couple of lines for humidity include: if varh => 4XXXXXXXXXXX then varh = 100%
    & if varh =< than 4xxxxxxxxxxx then varh = 0
    The input for ADC_H isn't linear which doesn't help. When I tried manual entry, the magnitude of the error varied! ;-(

    To add multiple attachments, you have to post one, then edit the post and add the next attachment. post, edit, attach etc.

     
    • Anobium

      Anobium - 2017-11-12

      I may be a little of context and I don’t want to stop the fun... but, has anyone emailed/contacted the manufacturer of sensor(s) and ask for an APPNOTE for 8bit, non-float support? A manufacturer will probably push back but they may have libraries available.

       
  • stan cartwright

    stan cartwright - 2017-11-12

    Mike-Maybe you can' post mult file or my internet is screwing up
    do
    attatch 1 file, post, edit then add another
    loop :)

     

    Last edit: stan cartwright 2017-11-12
  • David Thompson

    David Thompson - 2017-11-12

    Going backwards I divided one big number by the other instead of multiply. Although still wrong the result looks much better! So either there's a mistake in the DS or my understanding of
    <<<14 and >>>14 is completely wrong. I'll have another look at the Arduino code, I have a feeling it won't look so daunting this time.

    Anobium, good idea, I get the feeling from the data sheet comments that they're not really interested in the 32 bit community.

    Yep I was right, I've just found an equation in the AdaFruit Library, it has subtle differences. I've definitely spent too much time on this, I can see the differences just by just looking at their version! I'll give that a go.

    Mike don't know if it will help but I've attached the AdaFruit Equation for Pressure.

    What I can't understand, is most of the C/C++ code not only does it not have any equations, I can't even see a jump off to an external file or anything resembling a calculation.

     

    Last edit: David Thompson 2017-11-12
  • mmotte

    mmotte - 2017-11-13

    David T,
    Been working on Humidity today,
    Got it working in the spread sheet.
    and here is GCB routine.

    dim v_x1_u32r, v_x1_u32s, v_x1_u32t, v_x1_u32u as long
    
    sub CalcHumidity
        v_x1_u32r = t_fine - 76800
        v_x1_u32s =  (adc_H*16384)'-(dig_H4*1048576)'-(dig_H5 * v_x1_u32r))+16384)/32768
        v_x1_u32t = (dig_H4*1048576)
        v_x1_u32u = v_x1_u32s - v_x1_u32t
        v_x1_u32s = (v_x1_u32u -(dig_H5 * v_x1_u32r) +16384)/32768
         HSerPrint "   S "
        HSerPrint v_x1_u32s
        v_x1_u32t = ((((((v_x1_u32r*dig_H6)/1024)*(((v_x1_u32r *dig_H3)/2048)+32768))/1024)+2097152)*dig_H2+8192)/32768
         HSerPrint "  T "
        HSerPrint v_x1_u32t
        v_x1_u32r = v_x1_u32s * v_x1_u32t
        HSerPrint "  $F63 = "
        HSerPrint v_x1_u32r
    
        v_x1_u32r = (v_x1_u32r-((((v_x1_u32r/32768)*(v_x1_u32r/32768))/128)*dig_H1/16))
    
        v_x1_u32r = v_x1_u32r/2048
        'v_x1_u32r = v_x1_u32r/1024
    
    end sub
    

    Had to break the equation into pieces then it started to work.

    I'll clean out the HSerPrints later.

    BR
    Mike

     
  • David Thompson

    David Thompson - 2017-11-13

    That boy's a genius! I've just had another go, I've definitely spent too much time, I'm getting delusional. The Ada Fruit one is exactly the same. I split it into two parts and still get an error but only by X4 on the 2nd part so I'm close.
    Where did the 4 variables v_x1_u32r, v_x1_u32s, v_x1_u32t, v_x1_u32u come from?
    I can only see v_x1_u32r in the Equation.

     
  • stan cartwright

    stan cartwright - 2017-11-13

    It's such fun...
    I'm trying to convert rgb 565 to 16 bit gcb value from c++.
    This isn't my idea of programming, it's not de-bugging, which is also fun.

     
  • David Thompson

    David Thompson - 2017-11-13

    I've seen reference to BME/BMP in "the GCB library", or at least I thinkI have.
    If there is one, where is it hiding?
    Debugging it great fun, you just have to keep reminding yourself while doing it.

     
  • stan cartwright

    stan cartwright - 2017-11-13

    the gcb lib is an interface in the decimal sums by Anobium to use the device and provide the raw numbers we like so much.. I think..it's what I used and others equations to try.

     
  • mmotte

    mmotte - 2017-11-13

    David t,
    I think the only BME/BMP library is the one started by Anobium and I have been modifying. It is I2C though not spi. That could be another routine in it. It is attached but needs much work yet.

    Things that do work are : Initialization, reading the calibration parameters, reading the adc values.

    Still working on the pressure calculation.

    I think that is what you are refering to?

     
  • David Thompson

    David Thompson - 2017-11-14

    Hi Mike
    Thanks, I already found it in an earlier post. It looks pretty good. Where did Anobium learn to program like that? I often try to avoid using includes just to see if I can, but this sensor is incredibly complex, even withBME280.h it's complcated.
    I'm still confused over Humidity calculation, I put the whole equation i.e. not broken up, into LibreOffice Spreadsheet it still comes out to big by 3.65. I've done nothing to the equation except substitute adc_h and H1 to 6 and remove the data types which Libre doesn't understand. So it's not parenthesis unless C/C++ is different to xls/ods.

    In your example above you use 4 variables
    v_x1_u32r, v_x1_u32s, v_x1_u32t, v_x1_u32u
    Where did they come from?. I can't see any reference to them in the DS.

     
  • mmotte

    mmotte - 2017-11-14

    David T,
    There is nothing magic about the crazy named variables. v_x1_u32r comes from the humidity calculation on page 23,24 of the BME280pdf. When I broke the second equation into pieces I needed some temporary variables and temp was already used for temperature. Knowing these equations are taking a toll on me, i went with their crazy variables.

     v_x1_u32s =  (adc_H*16384)'-(dig_H4*1048576)'-(dig_H5 * v_x1_u32r))+16384)/32768
        v_x1_u32t = (dig_H4*1048576)
        v_x1_u32u = v_x1_u32s - v_x1_u32t
        v_x1_u32s = (v_x1_u32u -(dig_H5 * v_x1_u32r) +16384)/32768
        v_x1_u32t = ((((((v_x1_u32r*dig_H6)/1024)*(((v_x1_u32r *dig_H3)/2048)+32768))/1024)+2097152)*dig_H2+8192)/32768
        v_x1_u32r = v_x1_u32s * v_x1_u32t
    

    So this is only the second equation. It was coming up with number larger than a 'long' and not complaining. As you can see in the first line the remainder of the equation is commented out. Why it was not working I can only quess that the parsing of the math stack got mixed up.

    So i needed a temporary variable and checked the first part by print statement and by hand with the calculator. Theni tried hooking the rest of the equation on but that did not work. So I needed another variable, put on print statements and hand checked on calculator, that worked. IDid the subtration and it worked. Hooked the rest of the equation on to the result 'u' and it worked. Must have been small enough.
    So after all of that why did the second part v_x1_u32t equation work right off? but it did. The bottom line worked too.

     v_x1_u32r = (v_x1_u32r-((((v_x1_u32r/32768)*(v_x1_u32r/32768))/128)*dig_H1/16))
        v_x1_u32r = v_x1_u32r/2048
        'v_x1_u32r = v_x1_u32r/1024
    end sub
    

    So here is the rest of the equations. i don't know why the middle one needed a 2048 instead of the 4096 but it sure works. The last equation takes off the high resolution answer(5digits) and gives just a two digit answer. Why 1024 I don't know, it is in the pdf. I did not check the range like in the pdf but it should probably be added.

    attached is the spreadsheet I used . The humidity is below the floating pt pressure calcs FGH,57-68.

    Anobium put a lot of fluff in the BMP280.h file. There are things we are never going to use but in a true library you want to make it as universal as possible. What bothers me is the variables dimensioned in the BME280.h need to to be dimensioned in the main? why is that? something is not right about that.

    Got to go
    73
    Mike

     
  • stan cartwright

    stan cartwright - 2017-11-14

    Is the priority *,/,+,- universal in c++,basic,spread sheet? What about shift << >> and & or ?

     
  • mmotte

    mmotte - 2017-11-14

    "My Dear Aunt Sally" i think is true in any programming language. Thanks for paranthesis for the rest.

    Also multiply and divide should be communitive. So why did Bosch do this?

    if (p < 0x80000000)
    {
    p = (p << 1) / ((BME280_U32_t)var1);
    }
    else
    {
    p = (p / (BME280_U32_t)var1) * 2;
    }
    

    Isn't it the same? left shift 1 is mult by 2

     
  • stan cartwright

    stan cartwright - 2017-11-14

    part of lifes rich tapestry maybe. brackets make it easier to read definitely
    rotate is faster than * multiply. add 1+1 is faster than 1 * 2
    some of the math looks like it needs shifting or bool logic cos lots of 1024,2468,32768 stuff.

     
  • David Thompson

    David Thompson - 2017-11-15

    ich habe vergessen das es war ein durch ein deutsch geschrieben. Back to front grammer from an english speaker's perspective.
    Literal translation:
    I forgot that it was by a German written.
    instead of "My Dear Aunt Sally" how about "Dear Sally, My Aunt" !
    I visited Bosch's site and found a version of the equation broken down into 7 elements so it would likely fit into gcb. The down side is I get exactly the same error as before.
    Using a commercial humidity display, I get an error of about X3.65 so if I dvided the result by that I get something like the correct result. Up scaling ADC_H to 75% also gives an acceptable result.
    The new equation is expecting double precision not sure what the spreadsheet does.
    Since humidity isn't all that accurate, I can live with this but somethings not right.

    Attached is my spreadsheet, I boxed the 2 examples in with green so you don't have to plough through all the other stuff. Now that I'm convinced it's not my math, I might draft a note to Bosch as suggested by Anobium.

    Mike how big was your "fudge factor" for pressure?
    I've attached the Bosch file as .txt,it was originally .h

     

    Last edit: David Thompson 2017-11-15
  • David Thompson

    David Thompson - 2017-11-15

    What are you posting Stan?

     
  • stan cartwright

    stan cartwright - 2017-11-15

    Time for a joke after all the brain ache, like I just gave up on one gcb thing and done loads on another.

     
  • David Thompson

    David Thompson - 2017-11-16

    Well all this brain ache has not bee in vain, I've come up with Plan H
    I'm now sufficienly well versed in BME280 config etc that I have no concerns about reading and writing to it using SPI (I don't have an I2C version at the minute, same for the equations.
    I've gone back to the free Proton Basic and have now got SPI working with the 16f887. I really love that Digital Analyser!
    Next (Plan H) I'll see if I can convert Bob Marshall's code for BME280 I2C into SPI. Proton Basic has all the bells and whistles i.e. Floats, SWORD etc. If (such a little word) I can get it working, I'll see what happens when the Floats are changed to LONGs etc.
    Wish me luck :-)
    Mike, if you want to try the same exercise for I2C, you can download a free but chip limited version of Proton Basic. It includes the 16f887 and 18f25k40 which has 32K Flash, I don't think all the code will fit on the 16F887

     
  • stan cartwright

    stan cartwright - 2017-11-16

    David,it's not the destination,it's the journey...could be.. I've seen " the data sheet is wrong " before.
    Converting stuff as well. Good luck. Aren't there easier devices to use that do the same?

     
1 2 3 4 > >> (Page 1 of 4)

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.