wiringPi常用函数
注释:PWM占空比是指PWM信号中高电平占整个周期的比例。在一个PWM周期中,高电平持续时间与整个周期时间的比例称为占空比。通常用百分比表示,例如50%的占空比表示高电平持续时间占整个周期时间的50%。占空比越大,输出电平平均值越高,反之亦然。PWM占空比的大小直接影响到输出电平的大小,因此在控制电机、LED灯等需要调节亮度或速度的场合,常用PWM占空比来控制输出信号的强弱。
1. pinMode:设置GPIO引脚的输入输出模式
```c
int pinMode(int pin, int mode);
```
示例:```c
#include <wiringPi.h>
int main() {
int pin = 0;
wiringPiSetup();
pinMode(pin, OUTPUT);
digitalWrite(pin, HIGH);
delay(1000);
digitalWrite(pin, LOW);
return 0;
}
```
2. digitalWrite:设置GPIO引脚的电平
```c
void digitalWrite(int pin, int value);
```
示例:```c
#include <wiringPi.h>
int main() {
int pin = 0;
wiringPiSetup();
pinMode(pin, OUTPUT);
digitalWrite(pin, HIGH);
delay(1000);
digitalWrite(pin, LOW);
return 0;
}
```
3. digitalRead:读取GPIO引脚的电平
```c
int digitalRead(int pin);
```
示例:```c
#include <wiringPi.h>
int main() {
int pin = 0;
wiringPiSetup();
pinMode(pin, OUTPUT);
digitalWrite(pin, HIGH);
delay(1000);
digitalWrite(pin, LOW);
return 0;
}
```
4. softPwmCreate:创建软件PWM
```c
int softPwmCreate(int pin, int initialValue, int pwmRange);
```
示例:```c
#include <wiringPi.h>
int main() {
int pin = 0;
wiringPiSetup();
softPwmCreate(pin, 0, 100);
while(1) {
for(int i = 0; i <= 100; i++) {
softPwmWrite(pin, i);
delay(10);
}
for(int i = 100; i >= 0; i--) {
softPwmWrite(pin, i);
delay(10);
}
}
return 0;
}
```
5. softPwmWrite:设置软件PWM的占空比
```c
void softPwmWrite(int pin, int value);
```
示例:```c
#include <wiringPi.h>
int main() {
int pin = 0;
wiringPiSetup();
softPwmCreate(pin, 0, 100);
while(1) {
for(int i = 0; i <= 100; i++) {
softPwmWrite(pin, i);
delay(10);
}
for(int i = 100; i >= 0; i--) {
softPwmWrite(pin, i);
delay(10);
}
}
return 0;
}
```
6. wiringPiISR:设置中断回调函数
```c
int wiringPiISR(int pin, int mode, void (*function)(void));
```
示例:```c
#include <wiringPi.h>
#include <stdio.h>
void myInterrupt() {
printf("Interrupt!\n");
}
int main() {
int pin = 0;
wiringPiSetup();
pinMode(pin, INPUT);
wiringPiISR(pin, INT_EDGE_FALLING, &myInterrupt);
while(1) {
delay(1000);
}
return 0;
}
```
7. delay:延时函数
```c
void delay(unsigned int howLong);
```
示例:```c
#include <wiringPi.h>
int main() {
int pin = 0;
wiringPiSetup();
pinMode(pin, OUTPUT);
digitalWrite(pin, HIGH);
delay(1000);
digitalWrite(pin, LOW);
return 0;
}
```