Am 04.02.2003 06:05:50, schrieb "Shierly Wijaya" 
<shierly.wij...@nautronix.com.au>:
>Does anyone know if there's a delay function defined for msp430 ? I need to 
>create a delay function in order of microseconds.
>Any suggestions on how to do this ? Thanks =)

see msp430-libc/doc/doc.txt, Appendix F: "Ticks & Tricks", Point 18, Line 1574 
and following.
basicaly you do:

void delay(unsigned short d) {
    for(;d;d--) __asm__ __volatile__("; loop %0": "+r" (d));
}

the exact timing depens on your clock settings and compiler optimisations.
you can also code it in assembler if you want:

__attribute__ ((naked)) void delay(unsigned int n) {
    //first arg comes in register R15
    //the loop uses 3 cycles per round
    //the eight cycles come from the call overhead and ret
    //
    //delay_time = (1/MCLK)*(8+(3*n))
    asm("lcdloop: dec r15\n  jnz lcdloop\n ret");
}

should also do a nice busy loop and you can count exacly how many cycles
it's using. without knowing what the compilers does for optimizations.
(this is taken from examples/char_lcd/lcd.c)

you can also set up a timer, and enter a lowpower mode (LPM0) and use the timer
compare interrupt to wakeup again. but this only works for longer delays as you
need time to set up the timer but its very suitable for longer delays while 
saving
electrons.

chris



Reply via email to