Barometric Presure sensor BMP180

**
Karma: 58
Posts: 2794
View Profile
Email
Personal Message (Offline)

Re: Barometric Presure sensor BMP180
« Reply #31 on: Today at 01:29:29 am »
Bigger Bigger Smaller Smaller Reset Reset Reply with quoteQuote Modify messageModify Remove messageRemove
Looking at the code I have here, I seem to have two different versions of the temperature calculation, which are different. I had to "fix" it, for some reason, which I don't fully recall now.

I am not 100% certain, but it appears that the first calculation is the wrong one, and the second one is correct.

Code:

X1 = ((latest_raw_temp - (int32_t)ac6) * (int32_t)ac5) >> 15;
X2 = ((int32_t)mc << 11) - (X1 + md)/2; // round up
X2 /= (X1 + md);
B5 = X1 + X2;

Code:

int32_t X1 = ((latest_raw_temp - (int32_t)ac6) * (int32_t)ac5) >> 15;
int32_t X2 = ((int32_t)mc << 11) / (X1 + (int32_t)md);
int32_t B5 = X1 + X2;
float temp = (float)((B5 + 8) >> 4);
temp /= 10; // divide by 10

This algorithm may have been intended for a device with a 32 bit int type.

Two further comments on this.

The mc<<11 will also potentially overflow if it is done as a 16 bit calculation. It is important to have the cast in this line to ensure this happens.

The apparent big difference between the two calculations of X2 is not what it seems. The result will actually the almost the same. By subtracting half of the amount you are going to divide by, before doing the division, this has the effect of correcting the rounding of the least significant bit in the result of the division.