Content originally posted in LPCWare by NXP_USA on Thu Jun 03 13:40:22 MST 2010
Quote: bhallett
Hello all,
I am looking to create a software timer that does not use any of the timers supplied in the LPC1342 microcontroller. I am thinking of reading a ram, rom, or flash location a number of times in order to make up a specific time period (i.e. using the access time of these devices). Has anyone achieved this under the ARM/Thumb2 environment programmed in C?
Regards
Barry
It is nearly impossible to create an accurate delay loop in C that is stable over changing compiler versions and optimization settings.
This code may be helpful for shorter time delays. For longer time delays, maybe you could use the SysTick timer built into the Cortex M0/M3 core. It is independent from the CT16B0/1 and CT32B0/1 timers. Using the SysTick timer, if you set up a 10 mS interrupt and a counter, you could go into sleep mode and save power while waiting.
// Microsecond delay loop-
void DelayuS(uint32_t uS)
{
uint32_t CyclestoLoops;
CyclestoLoops = SystemCoreClock;
if(CyclestoLoops >= 2000000)
{
CyclestoLoops /= 1000000;
CyclestoLoops *= uS;
} else
{
CyclestoLoops *= uS;
CyclestoLoops /= 1000000;
}
if(CyclestoLoops <= 100)
return;
CyclestoLoops -= 100; // cycle count for entry/exit 100? should be measured
CyclestoLoops /= 4; // cycle count per iteration- should be 4 on Cortex M0/M3
if(!CyclestoLoops)
return;
// Delay loop for Cortex M3 thumb2
asm volatile (
// Load loop count to register
" mov r3, %[loops]\n"
// loop start- subtract 1 from r3
"loop: subs r3, #1\n"
// test for zero, loop if not
" bne loop\n\n"
: // No output registers
: [loops] "r" (CyclestoLoops) // Input registers
: "r3" // clobbered registers
);
}