I want to write a program that controls a servo motor myself using a pi B+. Controlling the motor involves sending 1.5 millisecond pulses along a GPIO pin every 20 milliseconds. I have tested the following stripped-down code on a 32-bit x86 Ubuntu machine and the B+, and I get very different results. I compiled both with gcc.
#include <stdio.h>
#include <time.h>
int main()
{
int num;
long int signalTime = CLOCKS_PER_SEC * 3/2/1000; /* clocks in a 1.5 millisecond clock pulse */
long int twentyMilliSec = CLOCKS_PER_SEC / 1000 * 20;
clock_t start = clock();
for (num = 0; num < 20; num++)
{
/* turn it on right here */
printf("Turning on: %ld\n", clock() - start);
while ((clock() - start) % twentyMilliSec < signalTime)
;
/*turn it off right here*/
printf("Turning off: %ld\n", clock() - start);
/* now we wait until 20 milliseconds is done */
while ((clock() - start) % twentyMilliSec > signalTime)
;
}
}
on the x86, I get the following output. Each number is 1 microsecond.
Turning on: 2
Turning off: 1501
Turning on: 20001
Turning off: 21501
Turning on: 40001
Turning off: 41501
Turning on: 60001
Turning off: 61501
Turning on: 80000
Turning off: 81500
Turning on: 100000
Turning off: 101500
Which is what I want, 1500 microsecond pulses with 20000 microsecond gaps between them. On the pi, I get the following.
Turning on: 0
Turning off: 10000
Turning on: 20000
Turning off: 30000
Turning on: 40000
Turning off: 50000
Turning on: 60000
Turning off: 70000
Turning on: 80000
Turning off: 90000
Turning on: 100000
It appears that the clock in is only accurate to 10000 microseconds. How can I get a clock in C that gives me enough precision to control a servo? I know it can be done, since there are C libraries that control servos for you.