3

Arduino has a delay(ms) function to pause the program for a certain amount of time. It is very accurate in milliseconds.

I have a delay function in C used in Keil uVision for the AT89C5131 microcontroller:

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

This does some delay job but the long value is not accurate like Arduino.

Is there a way to create a function that works like delay() function in Arduino?

The crystal is running at 24Mhz.

Yi Xu Chee
  • 67
  • 1
  • 1
  • 13

3 Answers3

1

If you want to do busy wait, this is how it's done in Keil:

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

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

Felipe Lavratti
  • 2,887
  • 16
  • 34
1

Try the SysTick interrupt handler that can add delay and find the example below:

  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
            }


 
samba siva
  • 11
  • 2
0

Will, I think you can use a Multi-cycle codes, try to add some for(); and I think that if you need a long delay(like some sec.) in 51 MCU, I guess it doesn't need a very good accurate.

Tony
  • 1
  • 2