How many If...ElseIf statements can I use?

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
CS
Posts: 127
Joined: Thu Nov 02, 2006 9:14 am

How many If...ElseIf statements can I use?

Post by CS » Wed Dec 27, 2006 10:56 am

Hi everybody,

I hope that everyone have had a very nice christmas. After a long time of testing I found something, that makes me confused. Please have a look at the following code:

Code: Select all


interrupt OnChange()
   If LineSwitch Then
      LineSwitch = false
      Toggle(PORTE.2)
      If DLine = 0 Then
         DLine = 1
      Else
         DLine = 0
      End If
   End If   
   If Setup Then 
      Setup = false
      SpecialMessage = false
      Button.Timeout = btnTimeout
      Button.Debounce = btnDebounce
   ElseIf TMR0IF Then 
      TMR0IF = false 
      TMR0 = TMR0 + Word(ReloadTimerValue)
      If Button.Debounce > 0 Then 
         Dec(Button.Debounce)
      ElseIf PORTB.1 = 1 Then 
         Button.Timeout = 0
         SetupAktiv = false
      ElseIf Button.Timeout > 0 Then 
         Dec(Button.Timeout) 
         If Button.Timeout = 0 Then 
            SetupAktiv = true
         EndIf 
      EndIf
   ElseIf Extern Then
      'check kind of message
      Extern = false
      SpecialMessage = true
      High(PORTE.0)
   EndIf
This interrupt handle works ok. My first try was the following that doesn't work:

Code: Select all

If Setup Then 
      Setup = false
      SpecialMessage = false
      Button.Timeout = btnTimeout
      Button.Debounce = btnDebounce
   ElseIf TMR0IF Then 
      TMR0IF = false 
      TMR0 = TMR0 + Word(ReloadTimerValue)
      If Button.Debounce > 0 Then 
         Dec(Button.Debounce)
      ElseIf PORTB.1 = 1 Then 
         Button.Timeout = 0
         SetupAktiv = false
      ElseIf Button.Timeout > 0 Then 
         Dec(Button.Timeout) 
         If Button.Timeout = 0 Then 
            SetupAktiv = true
         EndIf 
      EndIf
   ElseIf Extern Then
      'check kind of message
      Extern = false
      SpecialMessage = true
      High(PORTE.0)
   ElseIf lineswitch Then
      lineswitch = false
      toggle(porte.2)
      If dline = 0 Then
         dline = 1
      Else
         dline = 0
      End If
   EndIf
In the second code the Interrupt 'lineswitch' was added at the end with an ElseIf but it doesn't work :!: :?:

I thing the last interrupt handle should also work ok. That is the reason for the question how many If....ElseIf... statements can I use. Is there a limit? (Maybe I have made a little misstake, that I myself can't find!?)

Please help.

Reguards,

CS

User avatar
Steven
BETA Tester
Posts: 406
Joined: Tue Oct 03, 2006 8:32 pm
Location: Cumbria, UK

Post by Steven » Wed Dec 27, 2006 12:27 pm

It's not simply the second to last line where you have 'End If' rather than 'EndIf'?

Regards,

Steven

CS
Posts: 127
Joined: Thu Nov 02, 2006 9:14 am

Post by CS » Wed Dec 27, 2006 8:01 pm

Hi Steven,

the 2 examples compile without errors but only the first code works perfect. It doesn't matter how I write the code 'End If' or 'EndIf'.

Any other suggestions?

CS

xor
Posts: 286
Joined: Sun Nov 05, 2006 1:15 pm
Location: NYC
Contact:

Post by xor » Sun Dec 31, 2006 8:58 pm

You may be digging very deeply into the available stack. You have several nested tests and then calls to button subroutines while inside the ISR, which has already pushed several values onto the stack. You might want to close out some tests and move to the next one instead of getting deeply nested....where possible of course.

There are some opportunities to optimize the code and save some If-Then-Else tests. For instance:

Code: Select all

      If DLine = 0 Then 
         DLine = 1 
      Else 
         DLine = 0 
      End If
....can become:

Code: Select all

      DLine = DLine xor 1 
....or:

Code: Select all

      Toggle(DLine.0) 
I think you will find other opportunities to help yourself and assist the compiler.

CS
Posts: 127
Joined: Thu Nov 02, 2006 9:14 am

Post by CS » Sun Dec 31, 2006 10:27 pm

Hi xor,

thanks for the tip. Of course I'm looking everytime for optimisations of my code. I have to admit that I didn't thought about the 'DLine xor 1'. Because DLine is a variable of type Bit I can't use 'toggle(DLine)'. Maybe a thing for the wishlist.

Maybe I can post only my IRS so everyone can have a look and tell me some optimations !?

Best wishes for new year!

CS

P.S.: For the 'Dline = DLine xor 1' I have to use my school book for understanding :roll: :oops:

xor
Posts: 286
Joined: Sun Nov 05, 2006 1:15 pm
Location: NYC
Contact:

Post by xor » Sun Dec 31, 2006 10:48 pm

I can't tell what you are doing exactly but I don't understand using button routines within the ISR. If an active button sent you to the ISR then you are already committed to the fact it happened. Maybe add a little delay and check again to be sure it wasn't a glitch or to help determine which pin of the PORTB.4..PORTB.7 Interrupt-On-Change caused the jump to the ISR.

Regards using XOR. XOR 1 will toggle any bit position where the 1 is found. For instance:

Code: Select all

%11110000
   xor
%11111110 
__________
%00001110

CS
Posts: 127
Joined: Thu Nov 02, 2006 9:14 am

Post by CS » Mon Jan 01, 2007 1:20 am

I use three buttons to control all the functions that my program will have. For example when I press one button (the setup-button) more than 3 seconds the display shows me the setup-mode. As long as I hold the setup button I can toggle with one of the other buttons between the setup-functions and with the last button I can change the values of the function.

When I release the setup-button the other two buttons have other functions. One of the buttons makes it possible to switch between different measurements (8 different things will be displayable) while the other button toggles between the positions on the display because only 2 measurments at a time are visible.

As I started I wasn't able to use the ISR correcly. In an other thread David showed me how easy it is to use the ISR, debounce a button and integrate the delay function for the setup button. Now the complete button control is in the ISR. (3 Buttons on PORTB.0...PORTB.2)

Because I know that you're a profi I'm a little bit confused. Is it not right to handle all the importend button things in the ISR?

CS

P.S.: After I studied my school book I was more confused about the DLine = DLine XOR 1. After you explained it with a little example it is nearly clear. :) Thanks!

normnet
Posts: 55
Joined: Mon Jan 01, 2007 6:32 pm

Post by normnet » Mon Jan 01, 2007 6:47 pm

I am in the process of writing a utility which draws keywords "IF - ENDIF etc." together.

How can I attach a IDE FineLine screenshot?

Norm

TimB
Posts: 262
Joined: Wed Oct 04, 2006 7:25 am
Location: London UK

Post by TimB » Mon Jan 01, 2007 10:12 pm

I can't see directly how you post pics here so I suggest you put it on a picture share site and post the URL.

In the mean while any more info, it sounds interesting.

xor
Posts: 286
Joined: Sun Nov 05, 2006 1:15 pm
Location: NYC
Contact:

Post by xor » Mon Jan 01, 2007 10:28 pm

If you email the picture to me I will put in on my server. I will send you the URL and you will need to put that address in your post here and then encase it with
.

normnet
Posts: 55
Joined: Mon Jan 01, 2007 6:32 pm

Post by normnet » Mon Jan 01, 2007 10:55 pm


xor
Posts: 286
Joined: Sun Nov 05, 2006 1:15 pm
Location: NYC
Contact:

Post by xor » Tue Jan 02, 2007 12:18 am

Looks good from where I'm standing. :D

Post Reply