01 Raspberry Pi direct PWM output - hardware PWM program (recommended)
#include <stdio.h>
#include <wiringPi.h>
#include <softPwm.h> //必不可少
int main( void)
{
int pwm_gpio5 = . 1 ; // use GPIO5,
int I = 0 ;
wiringPiSetup (); // write rigor should not judge it, to see whether successful initialization
pinMode(pwm_gpio5 ,PWM_OUTPUT);
printf("pwm_gpio5 is blinking...\n");
for(;;)
{
for(i=0;i<1024;i++)
{
pwmWrite(1,i);
delay(10);
printf("Testing is %d......\n",i);
}
for(i=1023;i>0;i--)
{
pwmWrite(1,i);
delay(10);
printf("Testing is %d......\n",i);
}
}
}
02 Raspberry Pi direct PWM output - PWM software program
#include <stdio.h>
#include <wiringPi.h>
#include <softPwm.h>
int main( void)
{
int pwm_gpio5 = 1;
int i=0;
wiringPiSetup();
pinMode(pwm_gpio5 ,PWM_OUTPUT);
printf("pwm_gpio5 is blinking...\n");
softPwmCreate (pwm_gpio5, 100 ); // create soft PWM default value 100
for (;;)
{
for(i=0;i<100;i++)
{
softpwmWrite(1,i);
delay(10);
printf("Testing is %d......\n",i);
}
for(i=99;i>0;i--)
{
softpwmWrite(1,i);
delay(10);
printf("Testing is %d......\n",i);
}
}
}
In the PWM 03 wiringPI comprising a software-driven processing library may be output in the PWM signal to send any raspberry GPIO port. Wherein the PWM frequency is higher, the more CPU resources needed to pay special attention to the need to strike a balance
04 compiled executable file, do not forget to add -lwiringPi -lpthread