EmbeddedRelated.com
Forums
The 2024 Embedded Online Conference

function not executed when called from main program

Started by "cva...@yahoo.com [msp430]" July 11, 2014
Hello

I'm very new to controllers and I have some troubles with my first programs. I use CC Studio from TI and a Launchpad with the MSP430G2553.

I wrote a waiting function:

void wait(int j){
int i;
for(i=0;i }

When I call this function in the main program (wait(50000) ), it is not called at all. In debug mode the program does not stop at the line that calls the function. I cannot even put a breakpoint on that particular line, the breakpoint is automatically shifted one instruction further.

Is this some compiling optimization that takes my function out?

Thanks!

Cristian

Beginning Microcontrollers with the MSP430

You are using an int parameter. The maximum positive value is 32767. Try using a wait time less than 32767.

-- Paulo Ricardo
On 11 de julho de 2014 03h36min24s BRT, "c...@yahoo.com [msp430]" wrote:
>Hello
>I'm very new to controllers and I have some troubles with my first
>programs. I use CC Studio from TI and a Launchpad with the MSP430G2553.
>
> I wrote a waiting function:
> void wait(int j){
> int i;
> for(i=0;i >}
>
>When I call this function in the main program (wait(50000) ), it is not
>called at all. In debug mode the program does not stop at the line that
>calls the function. I cannot even put a breakpoint on that particular
>line, the breakpoint is automatically shifted one instruction further.
> Is this some compiling optimization that takes my function out?
> Thanks!
>
> Cristian
On Thu, 2014-07-10 at 23:36 -0700, c...@yahoo.com [msp430] wrote:
> Hello

> I wrote a waiting function:
>
> void wait(int j){
> int i;
> for(i=0;i > }

volatile int i;
may prevent the optimisations.

- Brian


Posted by: Brian Drummond




Thank you Brian. It works perfectly now. I still do not get why the compiler took my function out of the code when I did not wrote volatile int...

@Paulo
The fact that the number I gave as parameter to the function was not the problem. This generated a warning at compile time only. The function was not called at all.

Thanks a lot!
> I still do not get why the compiler took my function out > of the code when I did not wrote volatile int…

Because the function does not return anything, and does not have any side-effect (it doesn't modify any global variables or call other functions that might) so calling it is basically useless from the compiler's point of view.

By specifying that the 'i' is volatile, you tell the compiler that the variable can be accessed from outside your program (typically by the hardware). In that case, your function does have a side-effect and it won't be optimized out.
I would change your function to;
void wait(unsigned int j){
volatile unsigned int i;
for(i=0;i {
}
}
Hints:
volatile should help the complier not optimize away your delay loop.
unsigned int should give you the range you require; 50,000 (See
below for int sizes)

Data type Size Range
signed int 16 bits -32768 to 32767
unsigned int 16 bits 0 to 65535

Posted by: Mi Ke




hi..  i've used this:

void wait(unsigned int delay){
   while(--delay);

}
On Friday, July 11, 2014 1:36 AM, "c...@yahoo.com [msp430]" wrote:

 
Hello

I'm very new to controllers and I have some troubles with my first programs. I use CC Studio from TI and a Launchpad with the MSP430G2553.
I wrote a waiting function:

void wait(int j){
    int i;
    for(i=0;i }
When I call this function in the main program (wait(50000) ), it is not called at all. In debug mode the program does not stop at the line that calls the function. I cannot even put a breakpoint on that particular line, the breakpoint is automatically shifted one instruction further.

Is this some compiling optimization that takes my function out?

Thanks!

Cristian
Please note:

Whether you use the for loop or the while loop, this is NOT portable code.

You are letting the cpu cycle time of the instructions being generated by a particular compiler to delay an amount of time that you probably calculated using the same code, as to how long it would take to reach your desired time value.

While a certain compiler will generate certain code for the while loop, does not guarantee that the same compiler will generate similar code taking up the same amount of cpu time for the for loop.

Then you get into the differences of different compilers.

Can’t you use a timer interrupt?? That would be more exact as to the value that you want, regardless of what compiler you use or what code you use.

From: m... [mailto:m...]
Sent: Friday, July 11, 2014 9:54 AM
To: m...
Subject: Re: [msp430] function not executed when called from main program

hi.. i've used this:

void wait(unsigned int delay){

while(--delay);

}

On Friday, July 11, 2014 1:36 AM, "c...@yahoo.com [msp430] " wrote:

Hello

I'm very new to controllers and I have some troubles with my first programs. I use CC Studio from TI and a Launchpad with the MSP430G2553.

I wrote a waiting function:

void wait(int j){
int i;
for(i=0;i }

When I call this function in the main program (wait(50000) ), it is not called at all. In debug mode the program does not stop at the line that calls the function. I cannot even put a breakpoint on that particular line, the breakpoint is automatically shifted one instruction further.

Is this some compiling optimization that takes my function out?

Thanks!

Cristian
In CCS use __delay_cycles instead:

// Using 25MHz XTal
void DelayMs(unsigned int n) {
while(n--) {
__delay_cycles(25000);
}
}

gcalin
On 11/07/2014 10:54, Alexander Espinosa i...@yahoo.com [msp430] wrote:
> hi.. i've used this:
>
> void wait(unsigned int delay){
> while(--delay);
>
> }
> On Friday, July 11, 2014 1:36 AM, "c...@yahoo.com [msp430]"
> wrote:
> Hello
>
> I'm very new to controllers and I have some troubles with my first
> programs. I use CC Studio from TI and a Launchpad with the MSP430G2553.
>
> I wrote a waiting function:
>
> void wait(int j){
> int i;
> for(i=0;i > }
>
> When I call this function in the main program (wait(50000) ), it is
> not called at all. In debug mode the program does not stop at the line
> that calls the function. I cannot even put a breakpoint on that
> particular line, the breakpoint is automatically shifted one
> instruction further.
>
> Is this some compiling optimization that takes my function out?
>
> Thanks!
> Cristian
>












 

I agree. Using timer interrupt - Basic timer or watchdog timer, or any timer of timer A or timer B will do the job. Here is examples in assembler:

 

 

;********************** SETUP DELAY TIMES ***********************************
;
;TIMERB CONTINUOUSLY OPERATES WITH A CLOCK OF 32KHZ THUS THE PERIOD PER PULSE
;IS 30.518uS.  THE COUNT IS ADDED TO THE DELAY TIME AND THEN IS SET INTO
;REGISTER CCBR2.  THE COMPARE FLAG GOES HI WHEN THE DELAY PERIOD HAS ELAPSED.

 


T01MS:     MOV     #0033,R15             ;33x30.518 = 1MS
                  JMP       TDL00  

 

T02MS:     MOV     #0066,R15             ;66x30.518 = 2MS
                  JMP      TDL00  

 

T05MS:     MOV     #0164,R15             ;164x30.518 = 5MS
                  JMP        TDL00 

 

T10MS:     MOV     #0328,R15             ;328x30.518 = 10MS
                  JMP      TDL00  

 

T15MS:     MOV     #0492,R15             ;492x30.518 = 15MS
                   JMP      TDL00  

 

T50MS:      MOV     #1638,R15             ;1638x30.518 = 50MS
                    JMP     TDL00  

 

T250MS:    MOV    #8192,R15             ;8192x30.518 = 250MS
                   JMP      TDL00  

 

T500MS:     MOV     #16384,R15         ;16384x30.518 = 500MS

 

TDL00:         ADD     &TBR,R15                             ;COMPARE TIME
                      MOV     R15,&TBCCR2                     ;MOVE TO COMPARE REG #2
                      MOV     #0010H,&TBCCTL2             ;CLEAR FLAG, ENABLE INTER
                      MOV     #00D8H,SR                           ;SLEEP WHILE WAIT

 

                     CLR       TBCCTL2                              ;DISABLE DELAY TIMER INTERRUPT
                     BIS        #BIT3,SR                                 ;ENABLE GENERAL INTERRUPT
                     RET                                                       ;DELAY OVER

 

 

 

You can make more delay as you wish and when you need a delay time, just

 

                    CALL     #TxxMS

 

 

Anthony

 


 

 


-------Original Message-------

 



Date: 7/11/2014 7:09:28 AM

To: m...

Subject: RE: [msp430] function not executed when called from main program

 


Please note:


Whether you use the for loop or the while loop, this is NOT portable code.


You are letting the cpu cycle time of the instructions being generated by a particular compiler to delay an amount of time that you probably calculated using the same code, as to how long it would take to reach your desired time value.


While a certain compiler will generate certain code for the while loop, does not guarantee that the same compiler will generate similar code taking up the same amount of cpu time for the for loop.


Then you get into the differences of different compilers.


 


Can’t you use a timer interrupt?? That would be more exact as to the value that you want, regardless of what compiler you use or what code you use.


 




From: m... [mailto:msp430]
Sent: Friday, July 11, 2014 9:54 AM
To: msp430
Subject: Re: [msp430] function not executed when called from main program


 


 







hi..  i've used this:



 



void wait(unsigned int delay){



   while(--delay);



 



}



 






On Friday, July 11, 2014 1:36 AM, "c...@yahoo.com [msp430]" <msp430> wrote:


 





 






Hello



 



I'm very new to controllers and I have some troubles with my first programs. I use CC Studio from TI and a Launchpad with the MSP430G2553.



 



I wrote a waiting function:



 



void wait(int j){
    int i;
    for(i=0;i<j;i++){    };
}



 



When I call this function in the main program (wait(50000) ), it is not called at all. In debug mode the program does not stop at the line that calls the function. I cannot even put a breakpoint on that particular line, the breakpoint is automatically shifted one instruction further.



 



Is this some compiling optimization that takes my function out?



 



Thanks!



Cristian


 





 











__._,_.___








Posted by: "a...@gmail.com" <anthonyha2011@gmail.com>























stime=1405089228


























__,_._,___

The 2024 Embedded Online Conference