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.

1 second Delay.

Other Parts Discussed in Thread: MSP430WARE

How do I do a one second delay in MSP430 C programming.. is it like

  __delay_cycles(1); or

  __delay_cycles(1000);

or something else .. I need to know because I am messing with the temperature code, and I wanna delay between readings.

Please give me tips on this. thanks.

  • You could use delays as you described, but in that case you would be committing the CPU to a function that does not add much value to your application. As a result, your code not only lacks elegancy, but also waste power. The best way is to use a timer, such as Timer A, which is available in any MSP430. You can also set an interrupt so the CPU is committed only when necessary. Please try to find timer A examples within MSP430ware and also refer to our wiki page -- one of the sections is dedicated to setting up the timers.

    http://processors.wiki.ti.com/index.php/Getting_Started_with_the_MSP430_LaunchPad_Workshop

     

  • H,

    The intrinsic function __delay_cycles() takes number of cycles as input and delays the execution of next instruction by those many cycles.

    internally what it does is, make the CPU loop for those many cycles doing nothing. As Lenio cacula said, its waste of power and lacks elegance unless you are not so stringent about timing and power.

    to achieve a delay of 1 second, u need to calculate the required number of cycles for the same.

    Assuming your CPU frequency is 1Mz , each cycle takes 1uS. so for 1 second you have to use the count 1000000.

  • HI,

                 _delay_cycles(Count value)  is an intrinsic function in MSP. which is a kind of equivalent to while( --Count value ){ NOP(); } . NOP() statement takes 1 clock cycle to execute. This clock cycle period depends on the clock u r using . for example if u r using  16 MHz clock 1 clock period = 62.5 nS(1/16MHz).  so you need to count this clock periods to generate the required delay u needed. A simple formula goes like this

                         Count value = required delay (seconds) * frequency of clock u r using.

                        count value for 1 second = 1 (second) * 16,000,000 (if using 16 MHz clock)

    Regards,

    Sri.

  • sri-sri said:
    NOP() statement takes 1 clock cycle to execute.

    Right. But the loop requires more. 1 cycle for decrementing the counter, two for the branch if not zero. And two cycles for setting up the counter plus some cycles for saving the count register before and restoring it after the loop. The intrinsic takes care of all this so its execution will take exactly the given number of MCLK ticks.

    However, a loop delay won't acount for any interruption by an ISR. Any ISR that is executed during the loop will add to the delay time. If using a timer, only interruptions on the final ticks will enlarge the delay (if the timer expires while the interrupt is still processed). Interrupts htat are handles befor ethe timer expires do not influence the delay duration.
    And while waiting for the tiemr to expire, the CPU can go to sleep and save power.

    2:0 for the timer :)

  • Jens-Michael Gross said:
    2:0 for the timer :)

    But how about the execution is in sequence as follows,

    function()

    {

    Operation1

    An abvious delay  //introduce a small delay of 10ms

    Operation2

    }

    Do u recommend in this case to enter sleep mode after Operation1 and on timer expiry resume with Operation2

  • Hmm, Delay using timers is much efficient way to do the job. I can c that. In the case discribed by chethu, I wonder how good is to employ delay using timer. well if there is some sort of acknowledge that we need before executing operation 2...its okay but if we r looking for some thing to settle etc then. well, even in timers...every time the timer increments/decrements the value and looks for any of ccr registers right, does these effect the precession of delay?

    Regards,

    Sri.

  • Hi Sri, When you are using timer, its not the CPU which keep incrementing and comparing the timer register. Rather the timer operates independently and raises interrupt to CPU on reaching the count. So the timer circuit is a seperate module from CPU which takes the input from timer register.

    Sent from my phone

**Attention** This is a public forum