先安裝wiringPi
使用 GPIO 4 , GPIO 17, GPIO 18, GPIO24 (not pin number..)
使用四條杜邦線....從上面四個 GPIO .... 聯到你要量的四個點....
然後執行
g++ -Wall -o PWM pwm.cpp -lwiringPi
pwm.cpp 程式碼如下:
#include <math.h>
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <wiringPiI2C.h>
#include <string.h>
int PWM_PIN0 = 7; // GPIO 4
int PWM_PIN1 = 0; // GPIO 17
int PWM_PIN2 = 1; // GPIO 18
int PWM_PIN3 = 5; // GPIO 24
void measure(int PWM_PIN);
{
for (;;)
{
measure(PWM_PIN0);
delay (500) ;
}
}
PI_THREAD (myThread1)
{
for (;;)
{
measure(PWM_PIN1);
delay (500) ;
}
}
PI_THREAD (myThread2)
{
for (;;)
{
measure(PWM_PIN2);
delay (500) ;
}
}
PI_THREAD (myThread3)
{
for (;;)
{
measure(PWM_PIN3);
delay (500) ;
}
}
int main (void)
{
int x;
wiringPiSetup () ;
x = piThreadCreate (myThread0) ;
if (x != 0)
printf ("it didn't start\n");
x = piThreadCreate (myThread1) ;
if (x != 0)
printf ("it didn't start\n");
x = piThreadCreate (myThread2) ;
if (x != 0)
printf ("it didn't start\n");
x = piThreadCreate (myThread3) ;
if (x != 0)
printf ("it didn't start\n");
for (;;)
{
delay (500) ;
}
return 0 ;
}
void measure(int PWM_PIN)
{
unsigned int start,mid,end,temp,diff;
while(digitalRead(PWM_PIN)==HIGH)
{
}
while(digitalRead(PWM_PIN)==LOW)
{
}
start = micros();
while(digitalRead(PWM_PIN)==HIGH)
{
}
mid = micros();
while(digitalRead(PWM_PIN)==LOW)
{
}
end = micros();
diff = mid - start;
temp = (diff * 100) / (end-start);
printf("pin : %d, pulse : %d us, PWM : %d, Freq : %d \n",PWM_PIN,diff,temp,1000000/(end-start));
}
沒有留言:
張貼留言