Hi, guys, I’ve got my Photon recently and am tring to make a high resolution PWM to control a servo.
But then I found someting strange when using delayMicroseconds(n) in a timer timeout.
When n = 1 ~ 983, it seems all right. But when it comes to 984 ~ 1029, the actual delay time is always 1033 micorsecons.
When n goes above 1029, it seems all righ again.
And it happens rapidly. Follow is the regularity I got:
n = 984 ~ 1029, actual delay time is 1033
n = 1983 ~ 2030, actual delay time is 2033
n = 2983 ~ 3030, actual delay time is 3033
n = 3983 ~ 4030, actual delay time is 4033
…
Here is my test code:
int delay_value, delay_time;
void time_out() {
int temp;
temp = micros();
delayMicroseconds(delay_value);
delay_time = micros() - temp;
}
Timer timer(20, time_out);
void setup() {
Particle.variable("delay_value", &delay_value, INT);
Particle.variable("delay_time", &delay_time, INT);
Particle.function("set_value", set_delay_value);
}
void loop() {
}
int set_delay_value(String command) {
int temp;
timer.stop();
delay_value = command.toInt();
time_out();
temp = delay_time;
timer.start();
return temp;
}
In the Console, delay_value could be set by calling the function "set_value ", and it will return the delay time of delayMicroseconds(delay_value) called by the function directly. This delay time seems always be right.
Then the function will start the timer. When the timer times out, the time_out() function will be called, and the delay time of delayMicroseconds(delay_value) in the time_out() function will be saved in delay_time, witch can be get from the Console page.
For example:
When I set the delay_value = 983, the set_value function returns 985, the delayMicroseconds() called by time out delays 985.
When I set the delay_value = 984, the set_value function returns 986, but the delayMicroseconds() called by time out delays 1033.
This confused me for a few days. Hope someone could help.