This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Using Timer A in MSP430 vs using a simple for loop of NOPS to implement a delay

Other Parts Discussed in Thread: MSP430F2013

Hello All,

I am implementing a simple code to generate a 1 sec delay in MSP430F2013. I am using the VLOCLK with a frequency of 12KHz.

There are two ways I am doing this

1. The first code uses timer A and it's ISR and toggles on LED every 1 sec. I have used VLOCLK and Timer A CLK = VLOCLK.  Since I need a 1 sec delay, I have used the 16 bit timer values as 12000.

This method is giving me a correct 1 sec delay.

2. The second way I have implemented this is by setting MCLK = VLOCLK.  I have then written a for loop with loop count =12000. I am doing just one NOP inside the for loop.

while(1){

for(i=0;i<12000;i++){

__no_operation();

}

P1OUT ^= P1OUT;   //Toggle LED

}

Technically both the methods should give me a 1 sec delay. However only Timer A seems to give me an accurate 1 sec delay.

In the seond method the LED seems to flash faster than the first one.

 

Can anyone tell me what is the differece between the two methods ?

Regards,

Sharwari

  • Sharwari

    When you use the timer interrrupt, the interrupt happens at exactly 1200 clock cycles plus the few clock cycles for the interrupt latency.  But, a for loop has to do CPU operations for each loop.  It has to compare i against 1200, it has to increment i, and then it does nothing for one clock cycle.  This means that for each loop there are about 10 clock cycles that pass instead of just one.

    I am not sure why the for loop flashes more rapidly than the timer method, but this is the reason why the for loop doesn't generate a 1 second interval

    sparkchaser

  • As sparkchaser said, your second solution should be slower then the one with the timer because the for-construct needs more than just one cycle to be executed.

    If you are using IAR you should have a look at: VIEW/ REGISTER/ CPU Registers/ Cyclecounter and step through the for-construct for one inkrement of i. Then you can calculate i again.

    One possibility that the second solution runs faster might be that you didnt properly configure VLO as your MCLK!

  • since your delay variable is not declared volitile the compiler is free to optimize the entire for loop out since its unnecessary code (in the eye of the copiler it does nothing).. if you need a delay by code use the intrinsic function __delay_cycles

     

     

  • Hi, Yes I understand that the "loop method" should take longer to execute that the "timer method". But in my case it's taking shorter (the LED is glowing faster for the "loop method" than for the timer method. This is the way I am configuring my MCLK = VLOCLK. BCSCTL3 = BCSCTL3 | LFXT1S_2; //selects VLOCLK Source BCSCTL2 = BCSCTL2 | SELM_3 | DIVM_1; //Selects MCLK = VLOCLK Am I doing something wrong in the way I am configuring MCLK? Thanks Sharwari
  • Hi All,

    Also which of the methods will consume less power if we are to use LPM_3?

    The "loop method" or the "timer A" method. I feel that the timer method will consume less power in LPM 3 since the CPU is OFF in LPM 3

    Regards,

    Sharwari

**Attention** This is a public forum