Simple problem with if-then statements, where am I going wrong?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Simple problem with if-then statements, where am I going wrong?

2,905 Views
aaron800
Contributor I
I am very confused why the code is doing the following in a simple if-then structure:

interrupt 12 void MTIM_ISR(void){

    MTIMSC_TOF = 0;
 
    if(i== 0)
    {
      PTBD_PTBD4 = 0;
      PTBD_PTBD5 = 1;
      PTBD_PTBD6 = 1;      // JUMPING FROM HERE
      i++;
    }
    else if(i==1)
    {
      PTBD_PTBD4 = 1;
      PTBD_PTBD5 = 0; 
      PTBD_PTBD5 = 1;
      i++;                             // TO HERE, AND THEN OUT
    }
    else
    {
      PTBD_PTBD4 = 1;
      PTBD_PTBD5 = 1;
      PTBD_PTBD6 = 0;
      i= 0;
    }
    
   
}

This is just a interrupt handler for a timer-based delay.  Is the compiler optimizing the code in some weird way?  This basically blinks three led's in order based on the i index, and then starts back over at the first led (i=0).

Thanks,
Aaron
Labels (1)
0 Kudos
Reply
7 Replies

955 Views
aaron800
Contributor I
Hello, yes i corrected the mistake, and everything works now.  Thanks again, sorry for my newbie question.
0 Kudos
Reply

955 Views
aaron800
Contributor I
tony, you're right, i am an idiot, I think i'm dyslexic sometimes :-P

I still don't see why it is jumping though, unless the optimizer says that is the same thing

Thanks
0 Kudos
Reply

955 Views
bigmac
Specialist III
Hello Aaron,
 
I tried compiling your sample code, and then single stepping through the ISR under full chip simulation.  I found that the code does work as intended (apart from the typographical error previously observed).
 
The compiler seems to generate the following assembly code for the ISR (I have added the labels):
 
    PSHH
    LDA   i
    BNE   B1
 
    ; Case: i = 0
    BCLR  4,PTBD
    BSET  5,PTBD
    BSET  6,PTBD
    BRA   B2
 
B1: DBNZA B3
 
    ; Case: i = 1
    BSET  4,PTBD
    BCLR  5,PTBD
    BSET  5,PTBD  ; Typographical error
B2: LDHX  #i
    INC   ,X
    PULH
    RTI
 
B3: ; Case: i > 1
   BSET  4,PTBD
    BSET  5,PTBD
    BCLR  6,PTBD
    CLRA
    STA   i
    PULH
    RTI
 
Because of the typograqhical error, the LED connected to PTBD5 will never flash.
 
Regards,
Mac
 
0 Kudos
Reply

955 Views
aaron800
Contributor I
Tony, in the next case (i==2), the PTBD5 is turned back off, so I dont see the minor bug??

As far as debugging, yes I can step through the code in the debugger, I do a run from point at the beginning of the interrupt handler, I can step through and see it execute each time, the middle case (i==1) is never executed.  Yes i is initialized as a global variable before the handler routine. 

Thank you for helping a noobie!  I am more familiar with C development on the Atmel AVR series, but these processors seem more powerful and are considerably cheaper too.

Aaron
0 Kudos
Reply

955 Views
tonyp
Senior Contributor II


aaron800 wrote:
This basically blinks three led's in order based on the i index, and then starts back over at the first led (i=0).


Indeed, that's what it should do (if it didn't have a minor bug)!

    else if(i==1)
    {
      PTBD_PTBD4 = 1;
      PTBD_PTBD5 = 0; 
      PTBD_PTBD5 = 1;

should be


    else if(i==1)
    {
      PTBD_PTBD4 = 1;
      PTBD_PTBD5 = 0; 
      PTBD_PTBD6 = 1;
0 Kudos
Reply

955 Views
fabio
Contributor IV
Hi Aaron,

Yes, this is due to compiler optimization and I do not consider it weird.

The compiler is, in fact, reusing common code, instead of two increment instructions, it is using just one. Look at the disassembly listing and you will notice that.

Best regards,

0 Kudos
Reply

955 Views
Ake
Contributor II
Hi,
 
Assuming that i = 0 when the program starts.
 
Thís means that the first test will be true and PTB4 will turn low.
i is incremented.
 
The next time, i = 1, which means that PTB5 drops low.
i is incremented.
 
Next time i = 2, so neither of two earlier comparisons are OK, which makes PTB6 go low.
i is set to 0
 
Is there something hidden that I have not understood?
 
Regards,
Ake
0 Kudos
Reply