Page 1 of 1

help with types

Posted: Mon Apr 04, 2016 1:17 pm
by mikn
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
1datash.gif (51.64 KiB) Viewed 5119 times


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


here are calculations according to datasheet

Code: Select all

x1 = ((ut - ac6) * ac5) >> 15
x2 = (mc << 11) / (x1 + md)
b5 = x1 + x2
t = (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.
Thank you in advance.

Re: help with types

Posted: Mon Apr 04, 2016 2:09 pm
by LeighM
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 - ac6
x1 = x1 * ac5
x1 = x1 >> 15

x2 = mc
x2 = x2 << 11
x1 = x1 + md
x2 = x2 / x1

...

Re: help with types

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

Code: Select all

x1 = ut - ac6
x1 = x1 * ac5
x1 = x1 >> 15

x2 = mc
x2 = x2 << 11
x1 = x1 + md
x2 = x2 / x1
b5 = x1 + x2
t = b5
t = t + 8
t = 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
3readcal.gif (8.97 KiB) Viewed 5088 times

3readuint.gif
3readuint.gif (15.1 KiB) Viewed 5088 times

Re: help with types

Posted: Mon Apr 04, 2016 4:50 pm
by mikn
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 = mc
x2 = x2 << 11
x2 = 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
by mikn
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)) >> 11
x1 = b6 * B6
x1 = x1 >> 12
x1 = x1 * B2
x1 = x1 >> 11

//x2 = (ac2 * b6) >> 11
x2 = ac2 * B6
x2 = x2 >> 11

x3 = x1 + x2

//b3 = (((ac1 * 4 + x3) << OSS) + 2) / 4
b3 = ac1 * 4
b3 = b3 + X3
b3 = b3 << OSS
b3 = b3 + 2
b3 = b3 / 4

x1 = ac3 * b6 >> 13

//x2 = (b1 * ((b6 * b6) >> 12)) >> 16
x2 = b6 * B6
x2 = x2 >> 12
x2 = x2 * B1
x2 = x2 >> 16

//x3 = ((x1 + x2) + 2) >> 2
x3 = x1 + x2 + 2
x3 = x3 >> 2

//b4 = (ac4 * (x3 + 32768)) >> 15
b4 = x3 + 32768
b4 = b4 * AC4
b4 = b4 >> 15

//b7 = ((up - b3) * (50000 >> OSS))
b7 = up
b7 = b7 - B3
p_temp = 50000 >> OSS
b7 = b7 * p_temp

//x1 = (p >> 8) * (p >> 8)
X1 = p
x1 = x1 >> 8
x1 = x1 * X1

//x1 = (x1 * 3038) >> 16
x1 = x1 * 3038
x1 = x1 >> 16

//x2 = (-7357 * p) >> 16
X2 = (-7357) * p
X2 = x2 >> 16

//p = p + ((x1 + x2 + 3791) >> 4)
p_temp = x1 + x2 + 3791
p_temp = p_temp >> 4
p = p + p_temp



corresponding part of datasheet
4datash.gif
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

thank you in advance

Re: help with types

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

Re: help with types

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

Re: help with types

Posted: Tue Apr 05, 2016 1:39 am
by mikn
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
by andreimacoveiciuc
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!