Best way to do nested IFs

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 5.
To post in this forum you must have a registered copy of Flowcode 5 or higher.

Moderator: Benj

Post Reply
echase
Posts: 429
Joined: Mon Jun 11, 2007 11:55 am
Has thanked: 49 times
Contact:

Best way to do nested IFs

Post by echase »

I have a routine that goes like:

Main Loop;
Do some simple things (takes about 4msec)
Wait about 6 msec till 10msec gone by, using TMR0 interrupt to time it
Repeat 100 times (One sec has now gone by)
Go to Pause Macro

Pause Macro;
Pause TMR0
Do some serious things for 50 msec, such as write to LCD and big calculations
Restart TMR0
Repeat from Main Loop

The long pause is a problem so I would like to break down that 50msec into ten 5msec blocks of statements (each a macro) and space them out over the main loop in the 6msec waits. The pause can then disappear or be seriously shortened (LCD Clear is difficult to move out of the pause macro as it takes so long to execute). A hundred 0.5msec blocks might be more elegant but not technically possible as some statements, like LCD write, take longer than 0.5msec to execute.

To do this in Flowcode I could have a nested set of IF statements that are executed every 10msec in the main loop like:-

IF count =10? Yes: do block 1. No: go to next IF
IF count =20? Yes: do block 2. No: go to next IF
.
.
IF count =90? Yes: do block 9. No: go to next IF
IF count =100? Yes: do block 10. No: back to first IF

Problem is that it’s going to take a fair bit of time to execute 10 IFs and the execution time will vary, as when the count is less than 10 only one IF gets executed but 10 are when count more than 90. So is there a simpler set of commands I can use to jump off into the 10 different macros? Like IF 10, 20, .. 90, 100. Do block 1, 2, …9,10? Or something that uses other than IFs?

I could simplify it a bit by having count2 = count/10 so only need to step to a macro on each increment of the count2 number. But not sure then how to deal with count numbers 11-19 etc. as on these no macro is executed. Or can make it count2 = count/8 and have 8 macros if it’s quicker to calculate.

Ideal command might be:

GO TO MACRO(count2) but not aware that macro names can include a variable in them.

User avatar
DavidA
Matrix Staff
Posts: 1076
Joined: Fri Apr 23, 2010 2:18 pm
Location: Matrix Multimedia Ltd
Has thanked: 58 times
Been thanked: 258 times
Contact:

Re: Best way to do nested IFs

Post by DavidA »

Hello,

Not sure i understand entirely what you want to do, ive just given it a quick cursory read, but would a switch statement on count do?
GO TO MACRO(count2) but not aware that macro names can include a variable in them.
Im not sure what you mean by this but you can pass parameters to a macro

dazz
Posts: 1314
Joined: Sun Jan 15, 2012 3:15 pm
Has thanked: 474 times
Been thanked: 458 times
Contact:

Re: Best way to do nested IFs

Post by dazz »

hi
Does the attached flowchart help
Flowcode1.fcf
(15.73 KiB) Downloaded 302 times
Regards
Dazz
To sign up to the V5 forum follow this link http://www.matrixmultimedia.com/forum_upgrades.php

echase
Posts: 429
Joined: Mon Jun 11, 2007 11:55 am
Has thanked: 49 times
Contact:

Re: Best way to do nested IFs

Post by echase »

DavidA wrote:Hello,

Not sure i understand entirely what you want to do, ive just given it a quick cursory read, but would a switch statement on count do?
GO TO MACRO(count2) but not aware that macro names can include a variable in them.
Im not sure what you mean by this but you can pass parameters to a macro
What is a switch statement on count?

In summary I am trying to jump off into one of ten macros (or a new starting point in a single macro) based on where I am in a count from 1 to 100. So I jump off at 10, 20, 30 etc. But not at 11, 12, 35, etc. Need a method of jumping that does not take too long to work out which macro/starting point to pick.

On reflection, if easier, it could also be done by jumping at count 1, 2, …10 and not at 11-100 as bunching up the ten jumps/macros is OK. Or the counting can go from 0-99.

Thought providing lots of detail above was helpful but maybe I went overboard.

echase
Posts: 429
Joined: Mon Jun 11, 2007 11:55 am
Has thanked: 49 times
Contact:

Re: Best way to do nested IFs

Post by echase »

dazz wrote:hi
Does the attached flowchart help
Flowcode1.fcf
Regards
Dazz
Thank you, something like that Switch Count may well work. Never used that function before. What happens though if the count exceeds the max number that it allows which is 10? I see you are reseting it to zero at count =10. I want it to do nothing from 11-99. I’ll play with you fcd tomorrow.

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: Best way to do nested IFs

Post by Enamul »

Hi,
You problem's solution is switch..as David and Dazz said..
I am giving a sample c code (not to use in PIC) to show how switch works:

Code: Select all

switch (count) {
  case 10:
    printf("");
    break;
  case 20:
    printf("");
    break;
   case 30:
    printf("");
    break;
  default:
    printf("");
}
So, switch looks for count value if it matches with any of case value it jumps there otherwise go to default..
I have attached a simple fc of switch using count variable and 10 switch options..
Hope this will help. :)
Enamul
Attachments
Switch.fcf
(23.66 KiB) Downloaded 288 times
Enamul
University of Nottingham
enamul4mm@gmail.com

dazz
Posts: 1314
Joined: Sun Jan 15, 2012 3:15 pm
Has thanked: 474 times
Been thanked: 458 times
Contact:

Re: Best way to do nested IFs

Post by dazz »

Hi
Ive changed the switch counts to tens and added a macro(as per enamuls chart, just to show that you can either add your code directly to the branch or call a macro), depending on what you need to do and how tidy you like your code to look you can use either, run it ans watch what it does, on any number that isnt a ten it will simply not enter the switch, but when it gets a ten it will enter the branch repetedly until the number changes( ive changed the int counter to 3 otherwise you will be watching all day), i reset the count in the last switch just to show it doing something, but you could also reset the counter in the interupt the choice is yours hth

Regards
Dazz
Flowcode2.fcf
(17.86 KiB) Downloaded 292 times
To sign up to the V5 forum follow this link http://www.matrixmultimedia.com/forum_upgrades.php

echase
Posts: 429
Joined: Mon Jun 11, 2007 11:55 am
Has thanked: 49 times
Contact:

Re: Best way to do nested IFs

Post by echase »

Thank you for all the good ideas. When I look at the ASM files from all your helpful examples each Switch decision is listed as something like:

MOVLW 0x28
CPFSEQ gbl_FCV_COUNT_SWITCH
BRA label39
BRA label48

I don’t know ASM well but I interpret this as:

Read the index number we are now on
Compare it against the current switch_count value
If no match go to next index number (at label 39)
If match go to label 48 (the LCD write macro call)

Label 39. Read the index number we are now on
Compare it against the current switch_count value
If no match go to next index number
If match go to label 48 (the macro call)

This is done 10 times in total so if the switch_count value is not 10, 20, 30, etc. the routine will always have to execute all 40 lines. At the other extreme if exactly 10 it will only execute 4 lines.

Interestingly if I use 10 nested IFs it gives the same basic 10 lots of 4 lines. So I conclude that whilst this Switch statement is very elegant in Flowcode it may not actually give me a faster execution time unless somehow one line of ASM for the Switch function is quicker than for the IF function.

It may though be that I am worrying about nothing as even processing 40 lines of simple code is not that lengthy (a lot less than 6msec?? Any idea how many instruction cycles? My clock speed is 60Mhz.) and if I make sure the last macros are slightly shorter it compensates for the extra time going through these (nearly) 40 lines.

User avatar
Steve
Matrix Staff
Posts: 3421
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Re: Best way to do nested IFs

Post by Steve »

I think the SWITCH statement basically creates a set of nested IFs in assembly code.

If you wanted efficiency of code, the switch is probably as good as you're going to get in C. If you want it better, then you'll need to resort to assembly.

echase
Posts: 429
Joined: Mon Jun 11, 2007 11:55 am
Has thanked: 49 times
Contact:

Re: Best way to do nested IFs

Post by echase »

Steve wrote:I think the SWITCH statement basically creates a set of nested IFs in assembly code.

If you wanted efficiency of code, the switch is probably as good as you're going to get in C. If you want it better, then you'll need to resort to assembly.
Thanks, I feared that was the answer. But assembly is well beyond me, which is why I am a Flowcode customer!

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: Best way to do nested IFs

Post by JonnyW »

Hello. If this is still an issue, try looking at the ASM of a switch with consecutive numbers (so switch on 1 to 5, say). Some compilers will not use a jump table if the numbers are far apart to save space. If this uses a jump table you could split your counter up into a count of units and a count of tens (or to be more efficient work in powers of 2 instead of 10s).

This is just a suggestion - I don't know for sure the compiler does this.

Jonny

echase
Posts: 429
Joined: Mon Jun 11, 2007 11:55 am
Has thanked: 49 times
Contact:

Re: Best way to do nested IFs

Post by echase »

Thank you for all the suggestions and offers of help. In the end I concluded that there was little difference in the C Code/ASM between nested IFs and a SWITCH statement but the SWITCH was much quicker to write in Flowcode so is really neat and thanks for suggesting that.

Both do take variable times to execute, depending on how far down/across the routine you have to go to reach the matching condition. But as it is a couple of microseconds at most it is within what I can tolerate and I was unnecessarily worrying that it would be too long. I just need to ensure the later blocks of code/macros last a couple of microseconds less that the first ones.

Post Reply