32 bit unsigned VAR

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 2 and 3.

Moderators: Benj, Mods

Post Reply
Ron
Posts: 225
Joined: Wed Apr 11, 2007 6:15 pm
Has thanked: 2 times
Contact:

32 bit unsigned VAR

Post by Ron »

Hi,

Is there a way to define and use a 32 bit unsigned VAR to count 10ms ticks in TMR0 macro? I looked in the latest BoostC manual and 32 bit unsigned is supported. I am not sure if it is supported in the BoostC version used by Flowcode or if I can even do it, say in a "C" block.

I want to use this as a TMR0 "system clock". All required "pauses" in my program will capture the current value of this 32 bit VAR at it's starting time. Then I can calculate the ending time by adding required pause time value to current time. Let trm0 run as normal, if Current >= required ..... The pause time has completed. I need to be able to monitor 5 pauses in 5 different routines in my program each "scan".

I have set 5 routines with all pauses (multiples of each other) so all logic should complete at the "same" time, but it does not. The pauses drift if I create a program that blinks 5 LEDs on/off continuosly. Each LED controlled by it's own routine, at end all should turn off at the same time. They do not.

Every way I have tried has allowed time to drift over a couple of minutes (far more than the expected % error). I need to try this new approach using the TMR0 system clock/count.

32 bits counting 10ms ticks is good for over 1 year before I create my own Y2K issues. Controller will never be on for more than a few hours at a time.

Does anyone have a better idea? Trying to learn about MCU's on your own is very difficult. I just know someone is going to say, here use these 3 lines of "C" code and your problems will be solved (after spending several weeks trying to figure it out on my own).

Thank you,

Ron

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: 32 bit unsigned VAR

Post by Benj »

Hello Ron

Yes you can control 32-bit long variables in Flowcode using C. This would be done by creating the following in the defines section of the supplementary code (project options window) to allow the variable to be global.

unsigned long counter = 0;

You can then reference the counter using C code icons eg

counter = counter + 1;
counter = 0;
if (counter > 30000)
{ }

etc.

You should however be able to slow the timer interrupt down to around 75Hz so your count variable could become an Integer and therefore you can do everything inside of Flowcode.

at 75Hz you would get a maximum count of 32768 giving you a maximum time of 436 seconds or 7.28 minutes.

You could take this further by using one variable to count the interrupts eg 0 - 74 and when you reach 75 you can increment a seperate variable that will record the time in seconds. This way you will be able to count up to 255 seconds simply using two byte variables or 32768 seconds using a byte and a Integer variable.

Moving to multiple seconds in between major count values will give you even longer to play with without having to use 32-bit C code variables.

Ron
Posts: 225
Joined: Wed Apr 11, 2007 6:15 pm
Has thanked: 2 times
Contact:

Re: 32 bit unsigned VAR

Post by Ron »

Hi Ben,

I want to be able to count in 10ms ticks as a pause value is allowed to be entered in the format XXX.YY (0 - 200.99). Since there are 2 decimal places this is based on 10ms Ticks.

1/75 = .01333333 which is a 13ms tick (I think). I have worked out a 20MHz, prescale is 32 and then a tmr0l += 100 which gives me .00998 if I remember correctly. I then loop on this 1ms 10 times then do my { 32 bit count logic }.

Since I can have more than 1 pause start on the same scan I think I need to capture a system clock time at the start of the scan, and use this to calculate the "end" time for each of the pause commands started on that scan. Then I can then capture a new value (TMR0 every scan) and compare it to the Done calculated value. If I enter pause values that are multiples of each other (pause 1 is 100ms, pause 2 is 500ms, pause 3 is 1000ms) in the different routines and loop the routines so that all complete on the same scan all LEDs I am blinking should turn off at the same time.. In my many past attempts the loops all looped the proper number of times, It is just that the routine that has the longest pause value finished first, second longest pause value finished second, third longest finished third etc.....

This is the latest idea.

I hope I figure this out in fewer attempts than Edison needed when inventing the light bulb.

Thanks to everyone for your help and patience.

Ron

Ron
Posts: 225
Joined: Wed Apr 11, 2007 6:15 pm
Has thanked: 2 times
Contact:

Re: 32 bit unsigned VAR

Post by Ron »

Hi Guys,

OK, I have entered my new 32 bit VARs in define supp code.

How do I do the following in "C"? I looked at Boost manual but everything I try comes up 1 on compile.

If 32bit_var1 >= 32bit_var2

Yes
8bit_var1 = 0
8bit_var2 = 0
8bit_var3 = 0
32bit_var2 = 0

No
fall through to next section of code in chart

E:\Back Up Items\Step By Step\Flowcode Projects\18F 5 SEQ PAUSE REV 2 - Copy\184620 5 Seq PAUSE REV 2.5.c(14458): error: missing right paren
E:\Back Up Items\Step By Step\Flowcode Projects\18F 5 SEQ PAUSE REV 2 - Copy\184620 5 Seq PAUSE REV 2.5.c(14444): error: failure

failure

Return code = 1

Flowcode was unable to compile the flowchart's C code due to the following errors:


If your flowchart contains C code, please review this carefully. If your flowchart contains no C-code or you have thoroughly reviewed the code, contact Technical Support.

FINISHED



Thanks

Ron

Ron
Posts: 225
Joined: Wed Apr 11, 2007 6:15 pm
Has thanked: 2 times
Contact:

Re: 32 bit unsigned VAR

Post by Ron »

Hi,

After looking at the "C" file I have entered the following code when compiles to 0.

if (TMR0_CURRENT_CAPTURE >= S1_TMR0_END_COUNT)

FCV_S1_PARAM_1 = 0;
FCV_S1_PARAM_2 = 0;
FCV_S1_PARAM_3 = 0;
FCV_S1_PAUSE_1_SHOT = 0;

S1_TMR0_START_COUNT = 0;
S1_TMR0_END_COUNT = 0;


Why don't I need FCV_ in front of the supp defines VARS?

Does the code above do what I wrote in previous reply?

Thank you,

Ron

Ron
Posts: 225
Joined: Wed Apr 11, 2007 6:15 pm
Has thanked: 2 times
Contact:

Re: 32 bit unsigned VAR

Post by Ron »

Hi,

My code is not solving as expected. I think the issue is a calculation I am doing.

S1_TMR0_END_COUNT = S1_TMR0_START_COUNT + ((FCV_S1_PARAM_2 * 100) + FCV_S1_PARAM_3);

S1_TMR0_END_COUNT is unsigned long
S1_TMR0_START_COUNT is unsigned long
FCV_S1_PARAM_2 = BYTE
FCV_S1_PARAM_3 = BYTE


Can I do the calculation with the above var types?

I do not think I am getting a valid "END_Count" calculation, best as I can tell.

Thank you,

Ron

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: 32 bit unsigned VAR

Post by Benj »

Hello Ron

Regarding the C code problem, If statements are done using brackets like this. If you dont need the else then you can leave it out.

if (TMR0_CURRENT_CAPTURE >= S1_TMR0_END_COUNT)
{
FCV_S1_PARAM_1 = 0;
FCV_S1_PARAM_2 = 0;
FCV_S1_PARAM_3 = 0;
FCV_S1_PAUSE_1_SHOT = 0;

S1_TMR0_START_COUNT = 0;
S1_TMR0_END_COUNT = 0;
}
else
{
...
}

Youdo not need the FCV_ prefix under variables created in the supplementary code window because these are raw C variables. Flowcode creates its variables and adds the prefix to the raw C code name which is why you need the prefix when referencing Flowcode variables.

For your calculation you could try this, I have simply added some type casts to try to force the 32-bit variables.
S1_TMR0_END_COUNT = (long) S1_TMR0_START_COUNT + ( (int) (FCV_S1_PARAM_2 * 100) + FCV_S1_PARAM_3);

When you say you are not getting a valid value at the end, how are you checking this value?

You could load the 32-bit variable into four Flowcode byte variables for printing out to a LCD. Each value is a power of 256.

eg
FCV_BY1 = S1_TMR0_END_COUNT & 0xff;
S1_TMR0_END_COUNT = S1_TMR0_END_COUNT >> 8;
FCV_BY2 = S1_TMR0_END_COUNT & 0xff;
S1_TMR0_END_COUNT = S1_TMR0_END_COUNT >> 8;
FCV_BY3 = S1_TMR0_END_COUNT & 0xff;
S1_TMR0_END_COUNT = S1_TMR0_END_COUNT >> 8;
FCV_BY4 = S1_TMR0_END_COUNT & 0xff;

Ron
Posts: 225
Joined: Wed Apr 11, 2007 6:15 pm
Has thanked: 2 times
Contact:

Re: 32 bit unsigned VAR

Post by Ron »

Hi Ben,

I think I have TMR0 working the way I need it to. I stayed up all night working on it.

Putting the { } did solve the compiler return 1 problem. I went into the C file and looked at similar code and the { } were always there, so I added it.

I also fixed a few other problems I had with the "C" portion.


Thanks again to everyone that provided ideas, it is nice to be able to "bounce" ideas off others.

Ron

Post Reply