a question on the wait_us implementation

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

a question on the wait_us implementation

1,353 次查看
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by changchao on Thu Apr 14 18:34:55 MST 2016
I read a blog article on using the lpc810,and in that,I saw the following code for waiting for a specified micro seconds,but I can not understand why it can acheive that,anyone have a clue on that?
thanks.

<code>
void wait_us(uint32_t count)
{
    volatile static uint8_t ram;
    while (count-- > 0) {
        ram = 0x11;        /* Wait(1000ns@12MHz) */
    }
}
</code>

Here is the article And the source code
标签 (1)
0 项奖励
回复
4 回复数

1,278 次查看
lpcware
NXP Employee
NXP Employee
bump
0 项奖励
回复

1,278 次查看
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by bavarian on Tue Apr 19 01:23:15 MST 2016
An  ;-)  ugly  ;-)  alternative is the usage of assembler instructions. There you know how many cycles they need for execution. Then you only need to consider the current CPU frequency to get to the right delay for the loop execution.

But this is really an emergency solution, in case there is no timer available.

Regards,
NXP Support Team.
0 项奖励
回复

1,278 次查看
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by starblue on Sat Apr 16 12:01:22 MST 2016
The multi-rate timer (MRT) of the LPC8xx has an interesting feature called "One-shot bus stall mode". That is, you can use the timer to halt the CPU for a defined number of cycles.
0 项奖励
回复

1,278 次查看
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by vtw.433e on Fri Apr 15 01:10:45 MST 2016
This code will wait for different period if built with different levels of optimization, different compilers or even different versions of the same compiler. It relies on assumption of the code that is generated and the specific clock frequency and memory characteristics of the processor it is running on. In other words, it does not wait for a specific number of micro seconds in any meaningful or reliable way.

The only reliably way to (busy) wait for a period of time is to use one of the timers and to read the timer value in a loop, and checking for the desired period to have elapsed. LPCOpen uses this method - just look in the appropriate version for your chip.
0 项奖励
回复