2015-07-08 04:50 AM
Does anyone know of a good delay function I could use with my STM32F4. I need delay in miliseconds and microseconds.
#nano-delay() #ns-delay2015-07-08 05:36 AM
2015-07-08 06:42 AM
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 }2015-07-08 09:34 AM
2015-07-08 09:46 AM
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.2015-07-08 09:58 AM
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 wanted2015-07-08 10:11 AM
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?2015-07-08 10:25 AM
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);
}
}
2015-07-08 11:16 AM
2015-07-08 11:32 AM
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