Skip to content

Instantly share code, notes, and snippets.

@USA-RedDragon
Last active October 18, 2017 07:42
Show Gist options
  • Save USA-RedDragon/f8ef866ad6d1fb8fcf3707f0955e67ce to your computer and use it in GitHub Desktop.
Save USA-RedDragon/f8ef866ad6d1fb8fcf3707f0955e67ce to your computer and use it in GitHub Desktop.
#include <pigpio.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
//Holder for waiting on enter
char s[3];
//Number of arguments, first being program name
if(argc != 4) {
//We don't have all the data
//Exit and tell user
printf("Usage: ./%s 18 100000 50\n", argv[0]);
printf("This would start a hardware PWM on pin 18 at 100000Hz with a duty cycle of 50%%\n);
}
//Start GPIO operations
if(gpioInitialise() < 0) {
printf("Failed to init gpio\n");
return 1;
}
//First argument is pin
int pin = atoi(argv[1]);
//Second is frequency in hz
int freq = atoi(argv[2]);
//Third is percent of duty cycle
float duty = 1000000.0/100.0*atof(argv[3]);
//Start PWM
if(gpioHardwarePWM(pin, freq, duty) < 0) {
//If it doesn't work, end gpio operations
gpioTerminate();
//Tell the user
printf("Failed to set hardware pwm on gpio pin %d\n", pin);
//Exit
return 1;
}
printf("Successfully started hardware PWM on pin %d at %dHz with a duty cycle of %s%%\n", pin, freq, argv[3]);
printf("Press [ENTER] key to exit\n");
//Waiting for [ENTER]
fgets(s, 2, stdin);
//Turn PWM off
gpioPWM(pin, 0, 0);
//End GPIO Operations
gpioTerminate();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment