3

Arduino 具有delay(ms)暂停程序一段时间的功能。它以毫秒为单位非常准确。

我在 AT89C5131 微控制器的 Keil uVision 中使用了 C 语言的延迟功能:

void delay( unsigned long duration)
{
    while ( ( duration -- )!= 0);
}

这做了一些延迟工作,但长值不像 Arduino 那样准确。

有没有办法创建一个像delay()Arduino 中的函数一样工作的函数?

晶体以 24Mhz 运行。

4

3 回答 3

1

如果你想做忙等待,这是在 Keil 中完成的:

#pragma O0
void wait(volatile uint32_t cnt) {
    while(cnt--)
        _nop_();
}

http://www.keil.com/support/docs/606.htm

于 2014-02-04T16:19:44.600 回答
1

尝试可以添加延迟的 SysTick 中断处理程序并找到以下示例:

  volatile uint32_t msTicks;
    //! The interrupt handler for the SysTick module
    
    void SysTick_Handler(void) {
      msTicks++;
    }
/*----------------------------------------------------------------------------
 * Delay: delays a number of Systicks
 *----------------------------------------------------------------------------*/

    void Delay (uint32_t dlyTicks) {
      uint32_t curTicks;
      curTicks = msTicks;
      while ((msTicks - curTicks) < dlyTicks) { __NOP(); }
    }
   int main(){
          
             SysTick_Config(SystemCoreClock / 1000);  // Setup SysTick Timer for 1ms interrupts
             //some code
             Delay(500);
             // some code
            }


 
于 2020-07-12T02:27:48.760 回答
0

请问,我认为你可以使用多循环代码,尝试添加一些 for(); 而且我认为,如果您在 51 MCU 中需要较长的延迟(例如几秒钟),我想它不需要非常好的准确度。

于 2014-02-04T13:44:23.183 回答