## help with types

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 6.

Moderator: Benj

mikn
Posts: 209
Joined: Mon Mar 03, 2014 10:11 pm
Has thanked: 54 times
Been thanked: 41 times
Contact:

### help with types

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 4995 times

here's macro of i2c reading of raw temperature 2readt.gif (12.38 KiB) Viewed 4995 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.
FC 6.1.3.2 (18.02.2016)

LeighM
Matrix Staff
Posts: 1962
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 474 times
Been thanked: 674 times
Contact:

### Re: help with types

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...`
These users thanked the author LeighM for the post:
mikn (Mon Apr 04, 2016 5:32 pm)
Rating: 5.26%

mikn
Posts: 209
Joined: Mon Mar 03, 2014 10:11 pm
Has thanked: 54 times
Been thanked: 41 times
Contact:

### Re: help with types

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 4964 times 3readuint.gif (15.1 KiB) Viewed 4964 times
FC 6.1.3.2 (18.02.2016)

mikn
Posts: 209
Joined: Mon Mar 03, 2014 10:11 pm
Has thanked: 54 times
Been thanked: 41 times
Contact:

### Re: help with types

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!
FC 6.1.3.2 (18.02.2016)

mikn
Posts: 209
Joined: Mon Mar 03, 2014 10:11 pm
Has thanked: 54 times
Been thanked: 41 times
Contact:

### Re: help with types

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 4924 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

FC 6.1.3.2 (18.02.2016)

kersing
Valued Contributor Posts: 1894
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 541 times
Been thanked: 1058 times
Contact:

### Re: help with types

What are the datatypes of the variables?
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

mikn
Posts: 209
Joined: Mon Mar 03, 2014 10:11 pm
Has thanked: 54 times
Been thanked: 41 times
Contact:

### Re: help with types

here are they (marked with red + are unsigned) 4vars.gif (5.91 KiB) Viewed 4915 times
FC 6.1.3.2 (18.02.2016)

mikn
Posts: 209
Joined: Mon Mar 03, 2014 10:11 pm
Has thanked: 54 times
Been thanked: 41 times
Contact:

### Re: help with types

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.
These users thanked the author mikn for the post (total 4):
LeighM (Tue Apr 05, 2016 8:59 am) • kersing (Tue Apr 05, 2016 11:48 am) • medelec35 (Tue Apr 05, 2016 2:48 pm) • Benj (Wed Apr 06, 2016 9:36 am)
Rating: 21.05%

FC 6.1.3.2 (18.02.2016)

andreimacoveiciuc
Posts: 1
Joined: Thu May 14, 2015 2:54 pm
Contact:

### Re: help with types

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!