I2C Interfacing XC8 PIC16F877Ahttps://www.microchip.com/forums/(c) Microchip30Re: I2C Interfacing XC8 PIC16F877A (pcbbc)<blockquote class="quote"><span class="original">delfindelfin</span><br>Does the atmospheric pressure fluctuate that much also in cities above sea level?<br></blockquote><br>It fluctuates everywhere. Have you never heard of a barometer or watched a weather forecast?<br/><br>It's not possible to calculate altitude accurately without knowing an initial condition. Either a correct p0 or an initial altitude (so you can reverse p0).<br/><br>I am not a meteorologist, but I would say you will routinely be out by +/-50m. A lot more in severe weather, which is usually accompanied by larger atmospheric highs and lows.<br/><br><blockquote class="quote"><span class="original"></span>The sensor says there is 783.89 hP and I am supposed to be at 2,100 meters above sea level</blockquote><br>Then pressure at sea level for you today at your location would be 1011.61hPa. So close to average, but not the standard value. Try measuring over a number of days, and in different weather conditions, and you will see how it changes.<br/> <br/>If you assume p0 is constant at 1013.25:<br>Just using 12hPa per 100m would give you: 1911 m<br>Whereas using the real formula gives you: 2113 m<br/><br>So perhaps at your altitude it is worth using a better approximation to the real formula. At lower altitudes, less so. But regardless my point is that using the precise x^(1/5.255) formula is overkill as you cannot achieve any level of accuracy when assuming p0 constant.<br/> <br/>Use a GPS if you require an accurate altitude without need for calibration.<br/>https://www.microchip.com/forums/FindPost/1106573Fri, 26 Jul 2019 06:35:45 GMTRe: I2C Interfacing XC8 PIC16F877A (delfindelfin)Does the atmospheric pressure fluctuates that much also in cities above sea level? .. Let's say I am in a city at 2,100 meters above sea level .. How much can fluctuate? .. I thought I could calculate that altitude figure just by knowing the atmospheric pressure .. But as you mention, is not possible .. That would change a lot of daily things, like for example the temperature at boiling water .. The sensor says there is 783.89 hP and I am supposed to be at 2,100 meters above sea level<br/>https://www.microchip.com/forums/FindPost/1106559Fri, 26 Jul 2019 04:04:04 GMTRe: I2C Interfacing XC8 PIC16F877A (pcbbc)<blockquote class="quote"><span class="original">delfindelfin</span><br> Well, I am assuming p0 as the standard value of pressure at sea level: 1013.25 hPa<br> </blockquote><br> That is not a "standard value". It's the average for atmospheric pressure at sea level. If you are expecting it always to be the average value, then your altitude readings are going incorrect, possibly by a substantial amount.<br/> <br/>If I take a location near me at the coast, just this week in some very mild weather, the pressure has fluctuated between 1008 hPa to 1018 hPa. Given 12hPa equates to approximately 100m at low altitudes, that's approximately +/- 40m of error.<br/> <br/>But in extreme conditions it could be as high as 1085 hPa or as low as 870 hPa in places, which would be -600m or +1200m.<br/> <br/>To calculate a meaningful altitude from atmospheric pressure you need to know either:<br/>a) The current <b>actual</b> pressure at sea level, p0, at a location nearby<br/>b) Your starting altitude (at which point you measure the pressure, and so reverse calculate p0)<br/> <br/>What kind of altitudes are you wanting to handle? If you are close to the earth’s surface (i.e. not in a balloon or plane or hiking to the top of a mountain), and you do not have the above data, you might as well assume 12hPa per 100m. That is a linear relation, and so easy to do in integer only maths.<br/> <br/>Also, if you know your starting altitude you can make a much better estimate using 12hPa/100m as you can remove the error due to current local conditions.<br/>https://www.microchip.com/forums/FindPost/1106538Fri, 26 Jul 2019 03:00:17 GMTRe: I2C Interfacing XC8 PIC16F877A (delfindelfin)Well, I am assuming p0 as the standard value of pressure at sea level: 1013.25 hPa<br/>https://www.microchip.com/forums/FindPost/1106528Fri, 26 Jul 2019 01:09:18 GMTRe: I2C Interfacing XC8 PIC16F877A (pcbbc)You can’t. But it’s pointless without p0. How are you obtaining p0?https://www.microchip.com/forums/FindPost/1106527Fri, 26 Jul 2019 01:03:48 GMTRe: I2C Interfacing XC8 PIC16F877A (delfindelfin)Well, actually my next question is how to calculate x^(1/5.255)? ... This is for the calculation of meters above sea level .. I know that I am supposed to work with only integers when programming microcontrollers, because floating point numbers would take so much memory, so .. any suggestions?<br/>https://www.microchip.com/forums/FindPost/1106523Fri, 26 Jul 2019 00:36:38 GMTRe: I2C Interfacing XC8 PIC16F877A (pcbbc)<blockquote class="quote"><span class="original">ric</span>In the original, it's clear the power is 8</blockquote><br>Now you’ve spoilt all the fun. :wink:<br>Queue next question - how to calculate 2 ^ 8?<br><br>Edit: And no, it must be some other copy/version of the original. The text to the right showing the workings is different on the screen capture. Still, it appears to have been rendered in 1980s CGA.https://www.microchip.com/forums/FindPost/1106495Thu, 25 Jul 2019 17:55:02 GMTRe: I2C Interfacing XC8 PIC16F877A (ric)<blockquote class="quote"><span class="original">pcbbc</span><br>Original datasheet from page 2 of this very thread...<br>Here. Take a look at page 15.<br>What on earth you are working from I can only guess.<br></blockquote>I guess he's working from a low-res printout.<br/>In the original, it's clear the power is 8<br/>https://www.microchip.com/forums/FindPost/1106493Thu, 25 Jul 2019 17:49:40 GMTRe: I2C Interfacing XC8 PIC16F877A (pcbbc)Original datasheet from page 2 of this very thread...<br><a href="https://cdn-shop.adafruit.com/datasheets/BST-BMP180-DS000-09.pdf" target="_blank" rel="nofollow" title="https://cdn-shop.adafruit.com/datasheets/BST-BMP180-DS000-09.pdf">Here</a>. Take a look at page 15.<br>What on earth you are working from I can only guess.https://www.microchip.com/forums/FindPost/1106490Thu, 25 Jul 2019 17:21:47 GMTRe: I2C Interfacing XC8 PIC16F877A (delfindelfin)This website: <a href="https://www.sitesbay.com/...-expression-evaluation" target="_blank" rel="nofollow">https://www.sitesbay.com/...-expression-evaluation</a><br/>But you are right 1and0. Here is a more complete one: <a href="https://i0.wp.com/aticlew...it=536%2C723&ssl=1" target="_blank" rel="nofollow">https://i0.wp.com/aticlew...it=536%2C723&ssl=1</a><br/> <br/>Yes, RIC told me in #18, but I guess I didn't pay attention because either way my IC2 function was not working correctly and I was trying to fix that, but now it makes sense, sorry for asking it again ..<br/> <br/>and yes, I would like to be better at computer programming .. But I think I almost got this .. I have already measured the temperature, and now I am trying to measure the pressure .. I am using the oversampling ratio of 0, this is the mode ultra low power (oss=0), is this ok? I see that there are other modes with higher sampling ratio, but take more time <br/> <br/>And also, do you know at what power is the number 2? Because, it doens't look like a number, and the computation, is strange .. I only need to know that, and it would be done ...<br/> <br/>https://www.microchip.com/forums/FindPost/1106480Thu, 25 Jul 2019 15:43:33 GMTRe: I2C Interfacing XC8 PIC16F877A (PStechPaul)This is getting tedious and frustrating. I suggest the OP should take one or more formal courses on computer programming. Also, for much of this, the MPLAB software simulator will clearly and quickly show what is going on in numerical calculations such as this. But it may be necessary to expand an expression over several lines to see intermediate results, and perhaps the assembly view may help to see the order in which it is evaluated.<br/>https://www.microchip.com/forums/FindPost/1106470Thu, 25 Jul 2019 13:56:49 GMTRe: I2C Interfacing XC8 PIC16F877A (1and0)<blockquote class="quote"><span class="original">delfindelfin</span><br/>Yeah, I have already understood Harry, you need to use 32bit int for the computation, pcbpc explain it on post #88 ..<br/></blockquote><br/>... and ric explained it to you back in Post #18.<br/> <br/><blockquote class="quote"><span class="original"></span><br/>Although I didn't know the listing was incomplete and wrong at a few spots .. That's a list from a C programming website <br/></blockquote><br/>What website is that? That listing is missing a lot of operators; e.g. . ~ & << >> & ^ | just to name a few.<br/> <br/>Edit: Postfix and prefix increment/decrement have different precedence. <br/> <br/>https://www.microchip.com/forums/FindPost/1106449Thu, 25 Jul 2019 12:17:02 GMTRe: I2C Interfacing XC8 PIC16F877A (delfindelfin)Yeah, I have already understood Harry, you need to use 32bit int for the computation, pcbpc explain it on post #88 .. Although I didn't know the listing was incomplete and wrong at a few spots .. That's a list from a C programming website <br/>https://www.microchip.com/forums/FindPost/1106447Thu, 25 Jul 2019 12:05:54 GMTRe: I2C Interfacing XC8 PIC16F877A (1and0)<blockquote class="quote"><span class="original">delfindelfin</span><br/>It needs to follow this:<br/>Operator Priority Associativity<br>{},(),[] 1 Left to Right<br>++,--,! 2 Right to Left<br>*,/,% 3 Left to Right<br>+,- 4 Left to Right<br><,<=,>,>=,==,!= 5 Left to Right<br>&& 6 Left to Right<br>|| 7 Left to Right<br>?: 8 Right to Left<br>=,+=,-=,*=,/=,%= 9 Right to Left<br/></blockquote><br/>That precedence listing is incomplete and wrong at a few spots.<br/> <br/><blockquote class="quote"><span class="original"></span><br/>So, the correct answer is:<br/>((24317-16005)*25037)/32768=6,350<br/>(int) 24,317-16,005 = 8,312<br/>(int) 8,312*25,037 = 208,107,544<br/>(int) 208,107,544/32,768 = 6,350<br/></blockquote><br>Even after mlp in Post #85 shown you how this expression is evaluated in C, you still think your answer is correct!? Hint: (int) is a cast to 16-bit signed integer, where its values are -32768 to +32767.<br/> <br/>https://www.microchip.com/forums/FindPost/1106401Thu, 25 Jul 2019 07:52:55 GMTRe: I2C Interfacing XC8 PIC16F877A (1and0)<blockquote class="quote"><span class="original">delfindelfin</span><br/>Why do you say it is 0? After making the computation, I get the value: 6350.938232 .. But as I said, you can't store that on a long type, so I assume you only store the integer part, in other words: 6350 .. Why 0?<br/></blockquote><br/>I knew you're going to ask that :( because you does not seem to acknowledge that you've understood the subjects that others spent (wasted) their times teaching you, like Posts #18 and #19 in this very thread, quoted here:<br/> <br/><blockquote class="quote"><span class="original">ric in Post #18</span><br/>Because you are about to multiply it by a value that will give a result bigger than 16 bits.<br/>The multiply would overflow if you didn't force it to use 32 bit arithmetic. Casting one of the arguments to 32 bits makes that happen. It does not change the actual value. <br/> <br/>Just to be very clear, if the penny has not dropped yet. When you do a mathematical operation on two arguments in C, it is done using the size of the larger of the two variables. So, if one is 16 bit and one is 32 bit, it will be done using 32 bit arithmetic.<br/>If you use two 16 bit variables, then it's done using 16 bit arithmetic, and if the result is too big for a 16 bit variable, it will overflow. This is a fundamental feature of arithmetic in C, so you need to get your head around it.<br/></blockquote><br/><blockquote class="quote"><span class="original">cobusve in Post #19</span><br/>And just to be 100% clear on that, you have to think not only of the final answer but all the intermediary values as well. If you are going to do x *100 /99 then you have to be able to store x*100 so that you can divide it down by 99 in the last step, so even if your answer will be small the intermediary value will be large.<br/>... <br/>Not being aware of data type sizes and when they will overflow or what the rules are the compiler will use to do the math is a very common source of errors in C code. These errors can be extremely tricky as the math could work out fine for some values you test with and not for other values that come up during later use.<br/></blockquote><br/> <br/>If you could be bothered to understand what they are teaching you, you would not have asked this question and the other basic 101 questions (e.g. how to check a number is negative, etc.). Also, you have an annoying habit of not answering questions that are asked of you. I don't know ... maybe I'm wasting my time here too. :(<br/>https://www.microchip.com/forums/FindPost/1106399Thu, 25 Jul 2019 07:44:09 GMTRe: I2C Interfacing XC8 PIC16F877A (pcbbc)It does follow that precedence.<br>What seems to have escaped your grasp is that ALL intermediate values in arithmetic involving int values result in only an int result.<br>An int (in XC8) is only a 16 bit signed value. It can only hold values between -32768 and +32767.<br>When you multiply 8312 and 25037 that gives you only 30744, because that is the 16 low order bits of the result (everything else gets ignored).<br>You need a full 32 bit result here.<br>Cast the multiplier or multiplicand to a int32_t type. Then C will use 32 bit arithmetic and give you the answer you are expecting.<br><br>You are not going to get very far in embedded programming with C if you do not understand the size of variables and the impact of exceeding the minimum and maximum values of the int type.<br><br>Edit: In case you still do not understand...<br>208,107,544 = 0xC677818<br>But you get only the low 16 bits...<br>0x7818 = 30744https://www.microchip.com/forums/FindPost/1106333Wed, 24 Jul 2019 15:11:02 GMTRe: I2C Interfacing XC8 PIC16F877A (delfindelfin)It needs to follow this:<br/>Operator Priority Associativity<br> {},(),[] 1 Left to Right<br> ++,--,! 2 Right to Left<br> *,/,% 3 Left to Right<br> +,- 4 Left to Right<br> <,<=,>,>=,==,!= 5 Left to Right<br> && 6 Left to Right<br> || 7 Left to Right<br> ?: 8 Right to Left<br> =,+=,-=,*=,/=,%= 9 Right to Left<br/>So, the correct answer is:<br/>((24317-16005)*25037)/32768=6,350<br/>(int) 24,317-16,005 = 8,312<br/>(int) 8,312*25,037 = 208,107,544<br/>(int) 208,107,544/32,768 = 6,350<br/>https://www.microchip.com/forums/FindPost/1106330Wed, 24 Jul 2019 14:50:15 GMTRe: I2C Interfacing XC8 PIC16F877A (ric)<blockquote class="quote"><span class="original">delfindelfin</span><br/>Why do you say it is 0? After making the computation, I get the value: 6350.938232 .<br></blockquote><br>Because it is using integers for ALL the intermediate computations as well.<br/><strike>You are doing it by hand using floating point, then just converting to an integer at the end, which is wrong.</strike><br/> <br/><i>Edit: I see now, your actual error is not realising an intermediate calculation is overflowing the maximum value an int can hold.</i><br/><i>Others have explained this clearly.</i><br/> <br/>https://www.microchip.com/forums/FindPost/1106319Wed, 24 Jul 2019 13:17:46 GMTRe: I2C Interfacing XC8 PIC16F877A (mlp)<blockquote class="quote"><span class="original">delfindelfin</span><br/>Why do you say it is 0?<br/></blockquote><br/>Because, in C, <font face="courier new,courier">X1=(24317-16005)*25037/(32768)</font> is evaluated as:<br/><font face="courier new,courier">(int)(24317-16005) == 8312</font><br/><font face="courier new,courier">(int)(8312*25037) == 30744</font><br/><font face="courier new,courier">(int)(30744/32768) == 0</font><br/> <br/>If you don't understand why, then you really should go back to basics:<br/>Work through all the examples in K&R over and over again until you understand each one.<br/>Come back here with specific questions if you can't figure something out.<br/>Answer the clarifying questions you are asked.<br/>https://www.microchip.com/forums/FindPost/1106308Wed, 24 Jul 2019 12:25:14 GMTRe: I2C Interfacing XC8 PIC16F877A (delfindelfin)I am using the Real Hardware now and I think I am getting good values .. I use the simulator because I feel it is faster when you are programming ..<br/> <br/>Why do you say it is 0? After making the computation, I get the value: 6350.938232 .. But as I said, you can't store that on a long type, so I assume you only store the integer part, in other words: 6350 .. Why 0?<br/>https://www.microchip.com/forums/FindPost/1106307Wed, 24 Jul 2019 12:13:56 GMT