EmbeddedRelated.com
Forums
The 2024 Embedded Online Conference

Delay loop

Started by Frederic Beaulieu December 28, 2004
Hi all,
I try to find a way to replace my C delay loop
function (in attachement at the end of this email)
since it seems not reliable (and accepted) between
different compilers. I need something to wait a fixed
and determined number of SMCLK clock cycles (in my
case 4MHz) under every optimizators.

I'm not very familiar with assembler and I wonder if
it possible to be sure that it will not be optimized
by any compiler if I write it in assembly?

Is there someone who have a similar fct or can help me
to write it?

Happy new year from Canada!
Fred

Beginning Microcontrollers with the MSP430

Fred,

The best way to get this is to use one of the timers in the unit - that's what they are there for...

1st, set up the timer - have it give you an interrupt when countdown gets to 0

2nd, in your code, when you want the timer to happen, just load the count and turn the timer on

3rd, ISR could either have the action you want to have hapen after the delay OR have it set a flag to be read and cleared by your program which would then perform the action you wanted to have performed after the delay.

I have found that counted delay loops are not reliable, though I would have thought ASM routine wouldn't be optimized. Use a volatile variable, maybe, to avoid having an optimized loop?

I don't know what you're using for a clock course, but if you're relying on the DCO, it is very suceptible to variations (part-to-part, temp, etc) so I wouldn't trust it.

Hope this helps...

Rachel Adamec
Norristown, PA
This fonction is only used to wait very short time
(eg. 6 clock cycles @ 4Mhz) and don't need to be very
accurate. I try to avoid using timer in this case
since my timer is pretty much used by many other
tasks.
Fred,

from the User's Guide (I used slau144.pdf - page 3-53)
you could use a NOP implementation (if the MCLK and SMCLK are the same):

"Note: Emulating No-Operation Instruction
Other instructions can emulate the NOP function while providing different numbers of instruction cycles and code words. Some examples are:
Examples:


However, care should be taken when using these examples to prevent unintended results. For example, if MOV 0(R4), 0(R4) is used and the value in R4 is 120h, then a security violation will occur with the watchdog timer
(address 120h) because the security key was not used."

I would think that a function call would, in itself, be more than 6 clock cycles. At least with the C-overhead that's generally involved...

Have fun!

Rachel Adamec
Norristown, PA

The 2024 Embedded Online Conference