PIC16F1827 and mathematical calculations

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 4.
To post in this forum you must have a registered copy of Flowcode 4 or higher. To sign up for this forum topic please use the "Online Resources" link in the Flowcode Help Menu.

Moderator: Benj

Post Reply
Dmitry Maximenko
Flowcode V4 User
Posts: 36
Joined: Mon Sep 26, 2011 10:26 pm
Has thanked: 9 times
Been thanked: 2 times
Contact:

PIC16F1827 and mathematical calculations

Post by Dmitry Maximenko »

In my program, there are several parameters that affect each other. I have to do mathematical calculations as you can see below.
I enter into the calculation blocks: Ustrkorr = ( ( ( adc_level_current / 100 ) / 100 ) + 1 )
I wonder if I can do so.
Mathematically it is correct, at least when I'm counting on the calculator. But PIC seems to do wrong anyway :roll:
I shall use the "Ustrkorr" variable to make further calculations.
Very grateful for advice

--------------------------
Flow V4.5 Pro

User avatar
JonnyW
Posts: 1230
Joined: Fri Oct 29, 2010 9:13 am
Location: Matrix Multimedia Ltd
Has thanked: 63 times
Been thanked: 290 times
Contact:

Re: PIC16F1827 and mathematical calculations

Post by JonnyW »

Hello. What type of variable is adc_level_current (and also Ustrkorr)?

If it is an integer this is -32768 to 32767, you are dividing this by 10,000 so will only ever get a value from -3 to 3, is this right? If this is a float, then you need to use the floating point divide functions in v4.5 for PIC.

It is likely that the optimizer in the compiler is crunching ((A / 100) / 100) into (A / 10000) so doing in two parts like this may not be necessary.

If you give an example of the values you are getting in and the values you get out, this will make things more clear.

Cheers,

Jonny

Dmitry Maximenko
Flowcode V4 User
Posts: 36
Joined: Mon Sep 26, 2011 10:26 pm
Has thanked: 9 times
Been thanked: 2 times
Contact:

Re: PIC16F1827 and mathematical calculations

Post by Dmitry Maximenko »

Hi,
I vill use variable "Ustrkorr" to adjust another integer variable "Ukorred" depending on in data.
so "Ustrkorr" is an correction variable.

Ustrkorr = INT
Uout = INT
Ukorred = INT

Ustrkorr can vary betveen 1.0 to 1.09

In calculation box I was going to multiply the variables to get a corrected value:

Ustrkorr = ( adc_level_current / 60000 ) + 1
Ukorred = U_bor_Step_One * Ustrkorr

User avatar
JonnyW
Posts: 1230
Joined: Fri Oct 29, 2010 9:13 am
Location: Matrix Multimedia Ltd
Has thanked: 63 times
Been thanked: 290 times
Contact:

Re: PIC16F1827 and mathematical calculations

Post by JonnyW »

Hi. For integers, values are always whole numbers (integral) so you will never get a 0.09 value. Typically the PIC will always round these values down.

If you are on Windows 7 then the calculator provided has a 'Programmer' mode. This always does integer maths so you can find what your calculations would be using integers.

If you change your formula to be:

Ustrkorr = ( adc_level_current / 100 )

Then this is the value multiplied by 100 - in your case 100 to 109. You can then use a bit of maths to get the integer and remainder parts:
integer = Ustrkorr / 100
decimal = Ustrkorr % 100
There are more efficient ways than this if you need this to run quickly.

If all you need to do with Ustrkorr is this temporary value then:

Code: Select all

Ustrkorr = ( adc_level_current / 600 )
Ukorred = (U_bor_Step_One * Ustrkorr) / 100 + U_bor_Step_One
Should get you the result you require a bit more accurately. I suspect though that unless U_bor_Step_One is quite large this will also truncate to an integer in a way you wont like.

You may need to have a look at your program and see whether it is possible to keep the values as large as possible for as long as possible - in general you would only do the divide when you need to display the values, to maintain accuracy.

Jonny

Post Reply