Page 1 of 1

### help with types

Posted: Mon Apr 04, 2016 1:17 pm
I have arduino pro mini and bmp180 barometer.
I read temperature, do calculations according to datasheet but get incorrect values. Tried different ways of calculations (separating values into different variables, changing types) but still no success.

here's part of datasheet:
1datash.gif (51.64 KiB) Viewed 5119 times

here's macro of i2c reading of raw temperature
2readt.gif (12.38 KiB) Viewed 5119 times

here are calculations according to datasheet

Code: Select all

`x1 = ((ut - ac6) * ac5) >> 15x2 = (mc << 11) / (x1 + md)b5 = x1 + x2t = (b5 + 8) >> 4`

variable types are following:
ut, x1,x2,b5,t = long
ac5, ac6 = uint
mc, md = int

results in terminal are

Code: Select all

`ut=29358 t=434`

434 is incorrect, current temp is 27.5C so it must show 275 or around that.
434 is also changes time to time around 434, 438, etc as I open windows to change inside temp.. so seems like it reads the temperature and not some fixed data.

where I could be wrong? I suppose that the problem may be in calculations and types.
help me to locate the problem, please.

### Re: help with types

Posted: Mon Apr 04, 2016 2:09 pm
This is probably due to intermediate (temporary) values being limited to 16 bit.
To force 32 bit you could break down the calculations ...

Code: Select all

`x1 = ut - ac6x1 = x1 * ac5x1 = x1 >> 15x2 = mcx2 = x2 << 11x1 = x1 + mdx2 = x2 / x1...`

### Re: help with types

Posted: Mon Apr 04, 2016 3:20 pm
same story, tried this

Code: Select all

`x1 = ut - ac6x1 = x1 * ac5x1 = x1 >> 15x2 = mcx2 = x2 << 11x1 = x1 + mdx2 = x2 / x1b5 = x1 + x2t = b5t = t + 8t = t >> 4`

result

Code: Select all

`ac5=25369 ac6=20378 mc=-11786 md=2595 ut=29266 t=433`

i have manually calculated everything and result is 433 exactly, so I am disappointed if calibration values were read incorrectly. they are 2 bytes each and I don't know where may be mistake there.
Here are macros which read calibration data
3readcal.gif (8.97 KiB) Viewed 5088 times

3readuint.gif (15.1 KiB) Viewed 5088 times

### Re: help with types

Posted: Mon Apr 04, 2016 4:50 pm
finally, I've got it working

this code was with error, x1 was incremented and then used with invalid value:
x2 = mc
x2 = x2 << 11
x1 = x1 + md
x2 = x2 / x1
b5 = x1 + x2

this code worked

Code: Select all

`x2 = mcx2 = x2 << 11x2 = x2 / (x1 + md)b5 = x1 + x2`

t=273 which is 27.3C yahoo!

### Re: help with types

Posted: Mon Apr 04, 2016 10:06 pm
Here's another problem.
I try to calculate pressure and made calculations simple line by line
But still get incorrect values. Where may be the problem?

Code: Select all

`b6 = b5 - 4000//x1=(b2 * ((b6 * b6) >> 12)) >> 11x1 = b6 * B6x1 = x1 >> 12x1 = x1 * B2x1 = x1 >> 11//x2 = (ac2 * b6) >> 11x2 = ac2 * B6x2 = x2 >> 11x3 = x1 + x2//b3 = (((ac1 * 4 + x3) << OSS) + 2) / 4b3 = ac1 * 4b3 = b3 + X3b3 = b3 << OSSb3 = b3 + 2b3 = b3 / 4x1 = ac3 * b6 >> 13//x2 = (b1 * ((b6 * b6) >> 12)) >> 16x2 = b6 * B6x2 = x2 >> 12x2 = x2 * B1x2 = x2 >> 16//x3 = ((x1 + x2) + 2) >> 2x3 = x1 + x2 + 2x3 = x3 >> 2//b4 = (ac4 * (x3 + 32768)) >> 15b4 = x3 + 32768b4 = b4 * AC4b4 = b4 >> 15//b7 = ((up - b3) * (50000 >> OSS))b7 = upb7 = b7 - B3p_temp = 50000 >> OSSb7 = b7 * p_temp//x1 = (p >> 8) * (p >> 8)X1 = px1 = x1 >> 8x1 = x1 * X1//x1 = (x1 * 3038) >> 16x1 = x1 * 3038x1 = x1 >> 16//x2 = (-7357 * p) >> 16X2 = (-7357) * pX2 = x2 >> 16//p = p + ((x1 + x2 + 3791) >> 4)p_temp = x1 + x2 + 3791p_temp = p_temp >> 4p = p + p_temp`

corresponding part of datasheet
4datash.gif (29.33 KiB) Viewed 5048 times

here's the result
MSB=0x19 LSB=0x86 xLSB=0x27 upHEX=0x330C ac1=9111 ac2=-1095 ac3=-14620 ac4=33626 ac5=25285 ac6=23267 b1=6515 b2=40 mb=-32768 mc=-11786 md=2646
up=13068 pf(mm)=309.767272 p(pa)=41299 t(C)=264

### Re: help with types

Posted: Mon Apr 04, 2016 10:11 pm
What are the datatypes of the variables?

### Re: help with types

Posted: Mon Apr 04, 2016 10:37 pm
here are they (marked with red + are unsigned)
4vars.gif (5.91 KiB) Viewed 5039 times

### Re: help with types

Posted: Tue Apr 05, 2016 1:39 am
damn yes, I have fixed it.
the problem was in readtemperature macro, when I read second byte of data, Last parameter in i2c_Rxbyte was 0 instead of 1.
so next macro ReadPressure was reading completely wrong data and all calculations were wrong.
Whew.

### Re: help with types

Posted: Fri Feb 03, 2017 8:59 pm
Hello!
Can you share your flowchart please. I create something by miself but it read wrong presure, like -934. I want to compare and see where i do wrong.
Thank you!