cancel
Showing results for 
Search instead for 
Did you mean: 

Delay function for STM32F4

theanswer
Associate II
Posted on July 08, 2015 at 13:50

Does anyone know of a good delay function I could use with my STM32F4. I need delay in miliseconds and microseconds.

#nano-delay() #ns-delay
20 REPLIES 20
theanswer
Associate II
Posted on July 08, 2015 at 15:42

How about something like this. mills() is like the current clock time so I want to do the same thing in a while loop while the current time doesn't reach endTime

int endTime = millis() + length;

       while (millis() < endTime){

          //do something

       }

theanswer
Associate II
Posted on July 08, 2015 at 18:34

Does STM32f4 have something similar?

Posted on July 08, 2015 at 18:46

Similar to what?

You can use CYCCNT or a free running 16/32-bit timer for micro-second type delays, not a big software delay loop fan, but there are practical applications. You could I guess use SysTick, but interrupting at rates >100KHz is problematic, and it doesn't work at all if another high-priority interrupt is in control.

Your code example is broken. You can't do the math this way, if fails a wrap of the numbering space. Look carefully at how I did it in the TimingDelay() function. If you want the loop to do other things, make an equivalent function that does the same thing, or calls a non-NULL function pointer. You'll lose the fine granularity of the delay.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
theanswer
Associate II
Posted on July 08, 2015 at 18:58

This is what I am working on:

void playNote(int note, int octave, int length){

  char dir[10] = ''HIGH'';

  int pause = floppyDelay[octave][note] * 10;

 

 

unsigned int start;

  start = *DWT_CYCCNT;  

  unsigned int endTime = start + (SystemCoreClock/1000)*(unsigned int)(length/1000);

 

 

while(*DWT_CYCCNT < endTime){

    digitalWrite(''dirPin'', dir);

    if (strcmp(dir, ''LOW'') == 0)

      strcpy(dir, ''HIGH'');

    else

      strcpy(dir, ''LOW'');

    digitalWrite(''stepPin'', ''LOW'');

    digitalWrite(''stepPin'', ''HIGH'');

   

TimingDelay((SystemCoreClock/1000000)*pause);

  }

}

But the things that are in red are not working as I wanted

Posted on July 08, 2015 at 19:11

What did I tell you about computing the end time? You need to compare the difference.

Is the value in DWT_CYCCNT changing? Did you ENABLE the cycle counter?
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on July 08, 2015 at 19:25

You really don't explain the units for the parameters, I looked at the math, not sure that's what you're working with. Or how long your digitalWrite() function actually takes in the scale of things. Probably much more efficient ways the track if the Direction pin on the floppy interface should be high/low than using strings, and string comparisons.

void playNote(int note, int octave, int length){
char dir[10] = ''HIGH'';
unsigned int pause = floppyDelay[octave][note] * (SystemCoreClock/100000); // floppyDelay in units of Tens of Microseconds
unsigned int ontime = (SystemCoreClock/1000000)*(unsigned int)length; // length in units of microseconds
unsigned int start = *DWT_CYCCNT; 
while((*DWT_CYCCNT - start) < ontime){
digitalWrite(''dirPin'', dir);
if (strcmp(dir, ''LOW'') == 0)
strcpy(dir, ''HIGH'');
else
strcpy(dir, ''LOW'');
digitalWrite(''stepPin'', ''LOW'');
digitalWrite(''stepPin'', ''HIGH'');
TimingDelay(pause);
}
}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
theanswer
Associate II
Posted on July 08, 2015 at 20:16

Hmm. The left part of the while loop condition is always larger then the right part. That means I only write to pins once each time. What I want is that when the counter is counting towards the end time I always write on the pins and move the floppy drive head to play a note

theanswer
Associate II
Posted on July 08, 2015 at 20:32

If it helps, this is the original code I am working with.

static void playNote(int note, int octave, int length)

    {

       static int dir = 1;

       int pause = floppyDelay[octave][note] * 10;

       int endTime = millis() + length;

       while (millis() < endTime){

          digitalWrite(dirPin, dir);

          if (dir == 0)

             dir = 1;

          else

             dir = 0;

          digitalWrite(stepPin, HIGH);

          digitalWrite(stepPin, LOW);

          delayMicroseconds(pause);

       }

    }

But since I don't have some of the functions on my STM I can't do the same thing