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

For general Flowcode discussion that does not belong in the other sections.
Post Reply
miggarc
Posts: 52
http://meble-kuchenne.info.pl
Joined: Mon Jan 04, 2021 8:08 pm
Has thanked: 7 times
Been thanked: 4 times

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

Post 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" ?

Many thanks in advance,


Miguel Garcia,
Electronic Systems Developer

medelec35
Matrix Staff
Posts: 1466
Joined: Wed Dec 02, 2020 11:07 pm
Has thanked: 515 times
Been thanked: 474 times

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

Post 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.
Martin

miggarc
Posts: 52
Joined: Mon Jan 04, 2021 8:08 pm
Has thanked: 7 times
Been thanked: 4 times

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

Post 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 ?

Many thanks in advance,


Miguel Garcia,
Electronic Systems Developer

medelec35
Matrix Staff
Posts: 1466
Joined: Wed Dec 02, 2020 11:07 pm
Has thanked: 515 times
Been thanked: 474 times

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

Post 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.
Martin

Steve-Matrix
Matrix Staff
Posts: 1276
Joined: Sat Dec 05, 2020 10:32 am
Has thanked: 169 times
Been thanked: 285 times

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

Post 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.

miggarc
Posts: 52
Joined: Mon Jan 04, 2021 8:08 pm
Has thanked: 7 times
Been thanked: 4 times

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

Post 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 ?

Many thanks in advance,


Miguel Garcia,
Electronic Systems Developer

Steve-Matrix
Matrix Staff
Posts: 1276
Joined: Sat Dec 05, 2020 10:32 am
Has thanked: 169 times
Been thanked: 285 times

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

Post 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).

Post Reply