Page 1 of 1

How to define a U Long Constant for dealing with time on a 32 bit "scale"

Posted: Mon Feb 21, 2022 8:58 pm
by miggarc
Hi

I'm dealing with time, applying the Arduino "millis" concept to Flowcode on a PIC uC .

From what I perceived, Flowcode sets Constants to " Z " domain ( - 32768 to 32767 ) by default .

That said and, until now, I'm creating Constants as they were variables, so they could assumed
as U Long ones, like here, with .CNT_msToWait :


msCurrent - .msPrevious >= .CNT_msToWait


All of the above are variables in the U Long domain, otherwise I could not obtain correct results from the inequation .


So, obviously, question is:

How to create a U Long Constant for dealing with time in a 32 bit "scale" ?

Re: How to define a U Long Constant for dealing with time on a 32 bit "scale"

Posted: Mon Feb 21, 2022 9:27 pm
by medelec35
Hi.
A constant will set the type automatically depending on the constant value.
For example if you set the constant to 1234567 it will automatically set as a long.
Enter 1234 it well set the constant will be automatically set as an integer.
123.45 the constant will be automatically set a float etc.
In the end the constant are evaluated at compiler time to make the actual calculation faster and consume less memory.

Re: How to define a U Long Constant for dealing with time on a 32 bit "scale"

Posted: Tue Feb 22, 2022 10:14 am
by miggarc
H

So, if I define a Constant
CNT_msToWait = 1000
it will not give correct results on the inequation bellow, because msCurrent and
.msPrevious variables are U Long .

msCurrent - .msPrevious >=.CNT_msToWait

Can I force U Long, when creating the Constant, by putting zeros at the left of desired value ?

Or,

what must I need to write when editing C Constat icon to force it to be U Long ?

Re: How to define a U Long Constant for dealing with time on a 32 bit "scale"

Posted: Tue Feb 22, 2022 11:26 am
by medelec35
Hi
miggarc wrote:
Tue Feb 22, 2022 10:14 am
Can I force U Long, when creating the Constant, by putting zeros at the left of desired value ?
No that would work.
The first calculation needs to be Long variable = constant
That way the constant should be typecast to a long.

Re: How to define a U Long Constant for dealing with time on a 32 bit "scale"

Posted: Tue Feb 22, 2022 11:35 am
by Steve-Matrix
Some more info on this that may be helpful...

In the generated code, a constant will be defined at the beginning of the generated C code using a "#define" directive. When the compilation occurs, the C compiler will first essentially replace any instances of the constant name in the code with the value given in the #define statement. So it does not matter what the type is.

For example, the generated C code might look like this:

Code: Select all

#define MY_CONSTANT (32323)
.
.
.
MY_VAR = MY_CONSTANT + 6;
The statement near the end of this code will get translated as a first step to:

Code: Select all

MY_VAR = (32323) + 6;
At this point, the C compiler will perform any appropriate type casting.

The reason these constants are given a type (string, float or int) within Flowcode is so that Flowcode's simulation will work and also to ensure the code you generate will be valid for the C compiler.

Re: How to define a U Long Constant for dealing with time on a 32 bit "scale"

Posted: Tue Feb 22, 2022 4:48 pm
by miggarc
Thanks for the explanation.

I will try it.

Anyway, both a U Long Variable and a
U Long Constant use the same amount of program space memory, doesn't they ?

Re: How to define a U Long Constant for dealing with time on a 32 bit "scale"

Posted: Tue Feb 22, 2022 5:51 pm
by Steve-Matrix
miggarc wrote:
Tue Feb 22, 2022 4:48 pm
Anyway, both a U Long Variable and a
U Long Constant use the same amount of program space memory, doesn't they ?
It is probably more complex that that and depends on the compiler implementation, but there should not be a large difference (if any).