I made a function that should be able to create a delay for a certain number of microseconds, here the code.
Code: delay_us.cpp Select all
void delay_us(uint64_t number_of_us){
// Initializing the variable with the time BEFORE the count.
uint64_t microseconds = esp_timer_get_time();
// Starting the count, it exits when it is greater than number_of_us.
while(number_of_us <= (esp_timer_get_time() - microseconds));
}
I encountered the following problem when using it: if I use a value such as delay_us(20), it doesn't work!
Driving a pin (from high to low and vice versa) with a period of 40us I see, using an oscilloscope, that the time is not respected.
The period time is more or less 7us, very far from 40us (it is 3us low and 3us high!!!!).
That's not all! If I put delay_us(1000) the result doesn't change...
What am I doing wrong?
