I trying to toggle one of my GPIO pins at a specific frequency, at the kHz range.
I set up the wiringPi Library, the blinking example seems to work just fine.So I modified the code, so it will oscillat at 35 Khz, but the output frequency is about 5.5 kHz. However when I ran the code with no delay at all the output frequency was 0.5 Mhz. Note that I don't output anything to the screen.
I guess that that this related to the definition of sleeping function "suspends the execution of the calling thread", so returning the thread to the foreground might take some time.
How can I define the precise sleeping time?
- I tired to use usleep as well an nanosleep, but same problem.
- All the above was done done via an SSH, as I mentioned, no output was sent during execution
- The pi is running an Apache server, but I guess it doesn't have a big affect on it
Here is the code I used
#include <wiringPi.h>
#include <stdio.h>
#include <time.h>
int main (void){
int pin = 7;
struct timespec time;
time.tv_sec = 0;
time.tv_nsec = 14000L;
if (wiringPiSetup() == -1)
exit (1);
pinMode(pin, OUTPUT);
while(1){
digitalWrite(pin, 1);
nanosleep(&time,&time);
digitalWrite(pin, 0);
nanosleep(&time,&time);
}
return 0;
}