• Forums
• Posts
Latest Posts
Active Posts
Recently Visited
Search Results
• Page Extras
• Forum Themes
• AVR Freaks

### Hot!Mathematical operations

Page: 123 > Showing page 1 of 3
Author
GunkutA
Junior Member
• Total Posts : 120
• Reward points : 0
• Joined: 2019/01/10 00:09:38
• Location: 0
• Status: offline
0

# Mathematical operations

Hello I am using PIC18F26K83 and I am reading ADC value. I use LED in order to debug my software. So from ADC I am getting a value between 1000 and 1500. That is a logical value since it is 12 bit value. Let's assume it is 1500. Then I multiply it with some number which is: 0.01315917968. This calculation gives me the voltage value. Normally result of this multiplication is:
0.01315917968 * 1500 = 19,7387.  But when I make the calculation in my software result becomes less than 10 ( I use LED to check the result so I do know the exact value.)

This is the related part of the code:

`int alt_v;float alt_v1; float value = 0.01315917968 ;void main(){while(1){alt_v=ADC_READ(0); // alt_v is between 1000 and 2000alt_v1=value*alt_v; if((int)alt_v1>10){LATA.RA2=1;}}}`

And LED do not get ON. I believe it is because of float and integer multiplication. But as far as I know float and integer multiplication should give me a float result. Where am I doing it wrong? I would appreciate any suggestion.
post edited by GunkutA - 2019/04/22 05:25:25

1and0
Access is Denied
• Total Posts : 10351
• Reward points : 0
• Joined: 2007/05/06 12:03:20
• Location: Harry's Gray Matter
• Status: offline
Re: Mathematical operations 2019/04/22 05:22:33 (permalink)
+2 (2)
If you use only the LED for testing, how you know ADC_READ(0) returns 1500?  Why don't you just set it to 1500:
`alt_v = 1500;`
and check again.

Edit: More importantly, you need an infinite loop in main().
GunkutA
Junior Member
• Total Posts : 120
• Reward points : 0
• Joined: 2019/01/10 00:09:38
• Location: 0
• Status: offline
Re: Mathematical operations 2019/04/22 05:24:08 (permalink)
0
I checked the ADC value with LED and I saw that the value between 1200 and 1300. But when I write any constant instead of alt_v like your suggestion, the result does not change unfortunately.
Edit: Yes I have an infinite loop in the code. I forgot while I am writing it in here. I will add it.
1and0
Access is Denied
• Total Posts : 10351
• Reward points : 0
• Joined: 2007/05/06 12:03:20
• Location: Harry's Gray Matter
• Status: offline
Re: Mathematical operations 2019/04/22 05:30:24 (permalink)
0
Anyway, I tested your snippet here assigning 1500 to alt_v with the MPLAB8 Sim and it works.
GunkutA
Junior Member
• Total Posts : 120
• Reward points : 0
• Joined: 2019/01/10 00:09:38
• Location: 0
• Status: offline
Re: Mathematical operations 2019/04/22 05:31:56 (permalink)
0
1and0
Anyway, I tested your snippet here assigning 1500 to alt_v with the MPLAB8 Sim and it works.

Thanks. What was the result?
1and0
Access is Denied
• Total Posts : 10351
• Reward points : 0
• Joined: 2007/05/06 12:03:20
• Location: Harry's Gray Matter
• Status: offline
Re: Mathematical operations 2019/04/22 05:33:04 (permalink)
+1 (1)
GunkutA
1and0
Anyway, I tested your snippet here assigning 1500 to alt_v with the MPLAB8 Sim and it works.

Thanks. What was the result?

alt_v1 = 19.73877 and it executes the body of the if() statement.
post edited by 1and0 - 2019/04/22 05:34:07
GunkutA
Junior Member
• Total Posts : 120
• Reward points : 0
• Joined: 2019/01/10 00:09:38
• Location: 0
• Status: offline
Re: Mathematical operations 2019/04/22 05:41:37 (permalink)
0
But when I use my debugger it enters some library (LibMathDouble) that I do not know or use in my project. Do you use any library? I believe that when I do  alt_v1= alt_v * value; . Something wrong happens in my compiler maybe?
Edit: Maybe problem is in float value: 0.01315917968. Maybe it has too many fractional parts? Maybe when I multiply this float and integer the result becomes too big for my MCU?
post edited by GunkutA - 2019/04/22 05:44:17
1and0
Access is Denied
• Total Posts : 10351
• Reward points : 0
• Joined: 2007/05/06 12:03:20
• Location: Harry's Gray Matter
• Status: offline
Re: Mathematical operations 2019/04/22 05:48:14 (permalink)
+1 (1)
GunkutA
But when I use my debugger it enters some library (LibMathDouble) that I do not know or use in my project. Do you use any library? I believe that when I do  alt_v1= alt_v * value; . Something wrong happens in my compiler maybe?

What compiler are you using?

Edit: Maybe problem is in float value: 0.01315917968. Maybe it has too many fractional parts? Maybe when I multiply this float and integer the result becomes too big for my MCU?

Don't think so. ;)
GunkutA
Junior Member
• Total Posts : 120
• Reward points : 0
• Joined: 2019/01/10 00:09:38
• Location: 0
• Status: offline
Re: Mathematical operations 2019/04/22 05:53:33 (permalink)
0
I am using MicroC.... ( please do not say ask in their forum because they do not really have any community)
1and0
Access is Denied
• Total Posts : 10351
• Reward points : 0
• Joined: 2007/05/06 12:03:20
• Location: Harry's Gray Matter
• Status: offline
Re: Mathematical operations 2019/04/22 06:00:58 (permalink)
+1 (1)
It should work; I'm not familiar withe MicroC.  Remove the cast in the if() and try again.

GunkutA
Junior Member
• Total Posts : 120
• Reward points : 0
• Joined: 2019/01/10 00:09:38
• Location: 0
• Status: offline
Re: Mathematical operations 2019/04/22 06:10:25 (permalink)
0
Ah yes 1and0 thanks, when I use constant instead of alt_v it works. But when I use alt_v it does not work. However, I checked and confirmed (with led and if statement) alt_v is between 1000 and 1500. So ADC_Read() function needs to return 12 bit value to me. Can I directly put it into an integer? Maybe that one was my mistake?
qhb
Superb Member
• Total Posts : 9999
• Reward points : 0
• Joined: 2016/06/05 14:55:32
• Status: offline
Re: Mathematical operations 2019/04/22 06:35:25 (permalink)
+1 (1)
So why use MikroC if there is no good help?
XC8 is free, just without all optimisations.

Nearly there...
GunkutA
Junior Member
• Total Posts : 120
• Reward points : 0
• Joined: 2019/01/10 00:09:38
• Location: 0
• Status: offline
Re: Mathematical operations 2019/04/22 06:36:47 (permalink)
0
I will start using XC8 soon, but right now I do not have any time to get used to it or learn it.
qhb
Superb Member
• Total Posts : 9999
• Reward points : 0
• Joined: 2016/06/05 14:55:32
• Status: offline
Re: Mathematical operations 2019/04/22 06:40:11 (permalink)
+1 (1)
But plenty of time to flail around with things that don't work?

Nearly there...
GunkutA
Junior Member
• Total Posts : 120
• Reward points : 0
• Joined: 2019/01/10 00:09:38
• Location: 0
• Status: offline
Re: Mathematical operations 2019/04/22 06:40:35 (permalink)
0
A new question tho: is this doable? : float a = int a * 55;

I mean can I set a float number to a result of multiplication of 2 integers?

GunkutA
Junior Member
• Total Posts : 120
• Reward points : 0
• Joined: 2019/01/10 00:09:38
• Location: 0
• Status: offline
Re: Mathematical operations 2019/04/22 06:41:42 (permalink)
0
qhb
But plenty of time to flail around with things that don't work?

I hope that I will make it work :( and after this and some other little things I will start using XC8.
qhb
Superb Member
• Total Posts : 9999
• Reward points : 0
• Joined: 2016/06/05 14:55:32
• Status: offline
Re: Mathematical operations 2019/04/22 06:44:08 (permalink)
+1 (1)
GunkutA
A new question tho: is this doable? : float a = int a * 55;

This will convert "a" to an "int", multiply it by 55, which will give an "int" result, and then it will convert that int value back to a float before saving it into "a".
Have you ever read a C textbook? They explain in detail how C does mathematical operations.

Nearly there...
GunkutA
Junior Member
• Total Posts : 120
• Reward points : 0
• Joined: 2019/01/10 00:09:38
• Location: 0
• Status: offline
Re: Mathematical operations 2019/04/22 06:49:48 (permalink)
0
qhb
GunkutA
A new question tho: is this doable? : float a = int a * 55;

This will convert "a" to an "int", multiply it by 55, which will give an "int" result, and then it will convert that int value back to a float before saving it into "a".
Have you ever read a C textbook? They explain in detail how C does mathematical operations.

Actually, I have never read C textbook. I am learning it for 5 months at most. In the schools they teach VHDL, assembly and stuff.

Now I have tested it and if I directly divide my alt_v to 100 it gives a correct value. Like this:

`alt_v1= alt_v/100;`

But when I first multiply it with 55 and then divide it to 1000. It does not give me a correct result:

`alt_v1= alt_v*55;alt_v1= alt_v1/1000;`

Also I cannot directly divide alt_v to my float value which is 0.01315917968.

Anddd most important thing I have realised:

This one works:
`alt_v1=alt_v/100;alt_v1=alt_v1*2;`

However, this thing does not work:

` alt_v1 = alt_v*2; alt_v1= alt_v1/100;`

Does it even make sense? I probably miss something very obvious.
post edited by GunkutA - 2019/04/22 06:54:28
pcbbc
Super Member
• Total Posts : 1515
• Reward points : 0
• Joined: 2014/03/27 07:04:41
• Location: 0
• Status: offline
Re: Mathematical operations 2019/04/22 07:07:39 (permalink)
+1 (1)
Unless you say what size and type alt_v and alt_v1 are we cannot help you much. Most likely there is a type conversion or overflow “somewhere” is about all we can say.
Posting code snippets and asking what is wrong is always problematic, because the fault may not lie in the snippet you post and also (as you have discovered) you can make mistakes retyping which just result non-existent errors being identified.

What I can tell you is this operation:
`alt_v1= alt_v/100;`

If alt_v is an integer, will do an entirely integer divide (truncating any fractional result) and then assign the integer result to alt_v1 (regardless of if altv_1 is a float or integer type).
qhb
Superb Member
• Total Posts : 9999
• Reward points : 0
• Joined: 2016/06/05 14:55:32
• Status: offline
Re: Mathematical operations 2019/04/22 07:09:45 (permalink)
+1 (1)
GunkutA
Actually, I have never read C textbook. I am learning it for 5 months at most. In the schools they teach VHDL, assembly and stuff.

If you've been "learning it" for 5 months, then you should have read a C textbook in the first week.

Now I have tested it and if I directly divide my alt_v to 100 it gives a correct value. Like this:

`alt_v1= alt_v/100;`

What test did you do to determine that gave a correct result?
If MikroC follows the official C rules, then "alt_v/100" is an int divided by an int, and it must give an int result before it is then converted to a float to store in "alt_v1"

But when I first multiply it with 55 and then divide it to 1000. It does not give me a correct result:

`alt_v1= alt_v*55;alt_v1= alt_v1/1000;`

Again, what exact test did you do, and what result did you get. I can't see your screen!

post edited by qhb - 2019/04/22 07:11:22

Nearly there...
Page: 123 > Showing page 1 of 3