• AVR Freaks

Hot!I2C Interfacing XC8 PIC16F877A

Page: < 12345.. > >> Showing page 5 of 6
Author
delfindelfin
Super Member
  • Total Posts : 387
  • Reward points : 0
  • Joined: 2017/01/19 12:32:58
  • Location: Mexico
  • Status: offline
Re: I2C Interfacing XC8 PIC16F877A 2019/07/24 09:34:48 (permalink)
0
But then, If a computation has decimal parts, but it is assigned to long type, for example X1:
X1=(24317-16005)*25037/(32768)=6350.9...
It is just the Integer part, right? 6350?

MPLAB X IDE v5.05
XC8 2.00
#81
1and0
Access is Denied
  • Total Posts : 9630
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: I2C Interfacing XC8 PIC16F877A 2019/07/24 09:42:29 (permalink)
+1 (1)
delfindelfin
But then, If a computation has decimal parts, but it is assigned to long type, for example X1:
X1=(24317-16005)*25037/(32768)=6350.9...
It is just the Integer part, right? 6350?

Yes and NO!
 
Yes, integer has NO decimal digits.
 
NO, X1 = 0, NOT 6350.
#82
1and0
Access is Denied
  • Total Posts : 9630
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: I2C Interfacing XC8 PIC16F877A 2019/07/24 09:44:44 (permalink)
+1 (1)
delfindelfin
Proteus .. But I guess I can test it on Real Hardware .. I even have a Logic Analyzer

Why are you kept using a simulator when you have the real hardware and a PicKit debugger?!!!!!   
#83
delfindelfin
Super Member
  • Total Posts : 387
  • Reward points : 0
  • Joined: 2017/01/19 12:32:58
  • Location: Mexico
  • Status: offline
Re: I2C Interfacing XC8 PIC16F877A 2019/07/24 12:13:56 (permalink)
0
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 ..
 
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?
post edited by delfindelfin - 2019/07/24 12:17:42

MPLAB X IDE v5.05
XC8 2.00
#84
mlp
boots too small
  • Total Posts : 794
  • Reward points : 0
  • Joined: 2012/09/10 15:12:07
  • Location: previously Microchip XC8 team
  • Status: offline
Re: I2C Interfacing XC8 PIC16F877A 2019/07/24 12:25:14 (permalink)
+2 (2)
delfindelfin
Why do you say it is 0?

Because, in C, X1=(24317-16005)*25037/(32768) is evaluated as:
(int)(24317-16005) == 8312
(int)(8312*25037) == 30744
(int)(30744/32768) == 0
 
If you don't understand why, then you really should go back to basics:
Work through all the examples in K&R over and over again until you understand each one.
Come back here with specific questions if you can't figure something out.
Answer the clarifying questions you are asked.

Mark (this opinion available for hire)
#85
ric
Super Member
  • Total Posts : 23596
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: I2C Interfacing XC8 PIC16F877A 2019/07/24 13:17:46 (permalink)
0
delfindelfin
Why do you say it is 0? After making the computation, I get the value: 6350.938232 .

Because it is using integers for ALL the intermediate computations as well.
You are doing it by hand using floating point, then just converting to an integer at the end, which is wrong.
 
Edit: I see now, your actual error is not realising an intermediate calculation is overflowing the maximum value an int can hold.
Others have explained this clearly.
 
post edited by ric - 2019/07/24 15:17:37

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#86
delfindelfin
Super Member
  • Total Posts : 387
  • Reward points : 0
  • Joined: 2017/01/19 12:32:58
  • Location: Mexico
  • Status: offline
Re: I2C Interfacing XC8 PIC16F877A 2019/07/24 14:50:15 (permalink)
-1 (1)
It needs to follow this:
Operator             Priority    Associativity
{},(),[]                  1            Left to Right
++,--,!                   2            Right to Left
*,/,%                     3            Left to Right
+,-                         4            Left to Right
<,<=,>,>=,==,!=   5            Left to Right
&&                        6           Left to Right
||                             7           Left to Right
?:                            8           Right to Left
=,+=,-=,*=,/=,%=  9           Right to Left
So, the correct answer is:
((24317-16005)*25037)/32768=6,350
(int) 24,317-16,005 = 8,312
(int) 8,312*25,037 = 208,107,544
(int) 208,107,544/32,768 = 6,350

MPLAB X IDE v5.05
XC8 2.00
#87
pcbbc
Super Member
  • Total Posts : 1254
  • Reward points : 0
  • Joined: 2014/03/27 07:04:41
  • Location: 0
  • Status: offline
Re: I2C Interfacing XC8 PIC16F877A 2019/07/24 15:11:02 (permalink)
+2 (2)
It does follow that precedence.
What seems to have escaped your grasp is that ALL intermediate values in arithmetic involving int values result in only an int result.
An int (in XC8) is only a 16 bit signed value. It can only hold values between -32768 and +32767.
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).
You need a full 32 bit result here.
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.

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.

Edit: In case you still do not understand...
208,107,544 = 0xC677818
But you get only the low 16 bits...
0x7818 = 30744
post edited by pcbbc - 2019/07/24 15:15:51
#88
1and0
Access is Denied
  • Total Posts : 9630
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: I2C Interfacing XC8 PIC16F877A 2019/07/25 07:44:09 (permalink)
+2 (2)
delfindelfin
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?

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:
 
ric in Post #18
Because you are about to multiply it by a value that will give a result bigger than 16 bits.
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. 
 
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.
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.

cobusve in Post #19
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.
... 
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.

 
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. :(
#89
1and0
Access is Denied
  • Total Posts : 9630
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: I2C Interfacing XC8 PIC16F877A 2019/07/25 07:52:55 (permalink)
+1 (1)
delfindelfin
It needs to follow this:
Operator             Priority    Associativity
{},(),[]                  1            Left to Right
++,--,!                   2            Right to Left
*,/,%                     3            Left to Right
+,-                         4            Left to Right
<,<=,>,>=,==,!=   5            Left to Right
&&                        6           Left to Right
||                             7           Left to Right
?:                            8           Right to Left
=,+=,-=,*=,/=,%=  9           Right to Left

That precedence listing is incomplete and wrong at a few spots.
 

So, the correct answer is:
((24317-16005)*25037)/32768=6,350
(int) 24,317-16,005 = 8,312
(int) 8,312*25,037 = 208,107,544
(int) 208,107,544/32,768 = 6,350

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.
 
#90
delfindelfin
Super Member
  • Total Posts : 387
  • Reward points : 0
  • Joined: 2017/01/19 12:32:58
  • Location: Mexico
  • Status: offline
Re: I2C Interfacing XC8 PIC16F877A 2019/07/25 12:05:54 (permalink)
0
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 
post edited by delfindelfin - 2019/07/25 12:08:28

MPLAB X IDE v5.05
XC8 2.00
#91
1and0
Access is Denied
  • Total Posts : 9630
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: I2C Interfacing XC8 PIC16F877A 2019/07/25 12:17:02 (permalink)
+1 (1)
delfindelfin
Yeah, I have already understood Harry, you need to use 32bit int for the computation, pcbpc explain it on post #88 ..

... and ric explained it to you back in Post #18.
 

Although I didn't know the listing was incomplete and wrong at a few spots .. That's a list from a C programming website 

What website is that?  That listing is missing a lot of operators; e.g. . ~ & << >> & ^ |  just to name a few.
 
Edit: Postfix and prefix increment/decrement have different precedence. 
 
post edited by 1and0 - 2019/07/25 12:19:41
#92
PStechPaul
Super Member
  • Total Posts : 2381
  • Reward points : 0
  • Joined: 2006/06/27 16:11:32
  • Location: Cockeysville, MD, USA
  • Status: offline
Re: I2C Interfacing XC8 PIC16F877A 2019/07/25 13:56:49 (permalink)
0
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.

 
#93
delfindelfin
Super Member
  • Total Posts : 387
  • Reward points : 0
  • Joined: 2017/01/19 12:32:58
  • Location: Mexico
  • Status: offline
Re: I2C Interfacing XC8 PIC16F877A 2019/07/25 15:43:33 (permalink)
0
This website: https://www.sitesbay.com/...-expression-evaluation
But you are right 1and0. Here is a more complete one: https://i0.wp.com/aticlew...it=536%2C723&ssl=1
 
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 ..
 
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 
 
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 ...
 
post edited by delfindelfin - 2019/07/25 16:37:35

Attached Image(s)


MPLAB X IDE v5.05
XC8 2.00
#94
pcbbc
Super Member
  • Total Posts : 1254
  • Reward points : 0
  • Joined: 2014/03/27 07:04:41
  • Location: 0
  • Status: offline
Re: I2C Interfacing XC8 PIC16F877A 2019/07/25 17:21:47 (permalink)
+1 (1)
Original datasheet from page 2 of this very thread...
Here. Take a look at page 15.
What on earth you are working from I can only guess.
#95
ric
Super Member
  • Total Posts : 23596
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: I2C Interfacing XC8 PIC16F877A 2019/07/25 17:49:40 (permalink)
0
pcbbc
Original datasheet from page 2 of this very thread...
Here. Take a look at page 15.
What on earth you are working from I can only guess.
I guess he's working from a low-res printout.
In the original, it's clear the power is 8
post edited by ric - 2019/07/25 17:51:44

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#96
pcbbc
Super Member
  • Total Posts : 1254
  • Reward points : 0
  • Joined: 2014/03/27 07:04:41
  • Location: 0
  • Status: offline
Re: I2C Interfacing XC8 PIC16F877A 2019/07/25 17:55:02 (permalink)
+1 (1)
ricIn the original, it's clear the power is 8

Now you’ve spoilt all the fun. :wink:
Queue next question - how to calculate 2 ^ 8?

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.
post edited by pcbbc - 2019/07/25 17:59:27
#97
delfindelfin
Super Member
  • Total Posts : 387
  • Reward points : 0
  • Joined: 2017/01/19 12:32:58
  • Location: Mexico
  • Status: offline
Re: I2C Interfacing XC8 PIC16F877A 2019/07/26 00:36:38 (permalink)
0
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?
post edited by delfindelfin - 2019/07/26 00:38:11

MPLAB X IDE v5.05
XC8 2.00
#98
pcbbc
Super Member
  • Total Posts : 1254
  • Reward points : 0
  • Joined: 2014/03/27 07:04:41
  • Location: 0
  • Status: offline
Re: I2C Interfacing XC8 PIC16F877A 2019/07/26 01:03:48 (permalink)
+1 (1)
You can’t. But it’s pointless without p0. How are you obtaining p0?
#99
delfindelfin
Super Member
  • Total Posts : 387
  • Reward points : 0
  • Joined: 2017/01/19 12:32:58
  • Location: Mexico
  • Status: offline
Re: I2C Interfacing XC8 PIC16F877A 2019/07/26 01:09:18 (permalink)
0
Well, I am assuming p0 as the standard value of pressure at sea level: 1013.25 hPa
post edited by delfindelfin - 2019/07/26 02:01:45

MPLAB X IDE v5.05
XC8 2.00
Page: < 12345.. > >> Showing page 5 of 6
Jump to:
© 2019 APG vNext Commercial Version 4.5