C Program Blinks external LEDs using GPIOs and plays tones using pwm

How to write a C Program Blinks external LEDs using GPIOs and plays tones using pwm in C programming Language ?


Solution For C Program:
/*C Program Blinks external LEDs using GPIOs and plays tones using pwm*/


#include <stdio.h>      // for File IO and printf
#include <time.h>       // for usleep

#define GPIO_PIN_0 45      // LED pin green
#define GPIO_PIN_1 69      // LED pin red
#define GPIO_PIN_2 66      // LED pin orange

int main() {

// Creates the File pointers to create the gpio file system,
// set the direction of the GPIO, and change the data stored there.
FILE *sys, *dir0, *dir1, *dir2, *val0, *val1, *val2;
FILE *pwmsys, *period, *duty, *power;

// Specifies the file that the pointer will be used for (w = //write)
sys = fopen("/sys/class/gpio/export", "w");
pwmsys = fopen("/sys/devices/bone_capemgr.9/slots", "w");
    // int fseek(FILE *stream, long int offset, int whence)
    // sets the file position of the stream to the given offset.
    // Whence has the following constants:
    // SEEK_SET = Beginning of file
    // SEEK_CUR = Current position of the file pointer
    // SEEK_END = End of file
    //
    // In this instance fseek is not necessarily needed.

// prints value inside quotes into given file.
// %d means it will print some variable as a decimal.
// GPIO_PIN is the variable refered to by %d.

//fprintf(sys, "%d", GPIO_PIN_0);
int ehc = system("echo 45 > /sys/class/gpio/export");

//fprintf(sys, "%d", GPIO_PIN_1);
ehc = system("echo 69 > /sys/class/gpio/export");

//fprintf(sys, "%d", GPIO_PIN_2);
ehc = system("echo 66 > /sys/class/gpio/export");

//fprintf(pwmsys, "%s", "am33xx_pwm");
ehc = system("echo am33xx_pwm > /sys/devices/bone_capemgr.9/slots");
//fprintf(pwmsys, "%s", "bone_pwm_P9_14");
ehc = system("echo bone_pwm_P9_14 > /sys/devices/bone_capemgr.9/slots");

// Clears the FILE stream for sys file object.
fflush(sys);
fflush(pwmsys);

//Set the pwm to on
power = fopen("/sys/devices/ocp.3/pwm_test_P9_14.15/run", "w");

fseek(power, 0, SEEK_SET);
fprintf(power, "%d", 1);
fflush(power);

//Set the pwm period
period = fopen("/sys/devices/ocp.3/pwm_test_P9_14.15/period", "w");

fseek(period, 0, SEEK_SET);
fprintf(period, "%d", 1911132);
fflush(period);

//Set the pwm duty

duty = fopen("/sys/devices/ocp.3/pwm_test_P9_14.15/duty", "w");
fseek(duty, 0, SEEK_SET);
fprintf(duty, "%d", 1911132);
fflush(duty);

//Set the gpio to output
dir0 = fopen("/sys/class/gpio/gpio45/direction", "w");

fseek(dir0, 0, SEEK_SET);
fprintf(dir0, "%s", "out");
fflush(dir0);

dir1 = fopen("/sys/class/gpio/gpio69/direction", "w");

fseek(dir1, 0, SEEK_SET);
fprintf(dir1, "%s", "out");
fflush(dir1);

dir2 = fopen("/sys/class/gpio/gpio66/direction", "w");

fseek(dir2, 0, SEEK_SET);
fprintf(dir2, "%s", "out");
fflush(dir2);

//Opens the file that controls if the pin is high or low
val0 = fopen("/sys/class/gpio/gpio45/value", "w");

fseek(val0, 0, SEEK_SET);

val1 = fopen("/sys/class/gpio/gpio69/value", "w");

fseek(val1, 0, SEEK_SET);

val2 = fopen("/sys/class/gpio/gpio66/value", "w");

fseek(val2, 0, SEEK_SET);

//Blinks the LED
int i=0;
if(val2 && val0 && val1 && period && duty) {
while(i<4) {
//Set the pwm period
fprintf(period, "%d", 1911132);
fflush(period);
//Set the pwm duty
fprintf(duty, "%d", 955566);
fflush(duty);

fprintf(val0, "%d", 0);
fflush(val0);
fprintf(val1, "%d", 0);
fflush(val1);
fprintf(val2, "%d", 0);
fflush(val2);
usleep(500000);
//Set the pwm period
fprintf(period, "%d", 3214090);
fflush(period);
//Set the pwm duty
fprintf(duty, "%d", 1607045);
fflush(duty);
fprintf(val0, "%d", 1);
fflush(val0);
fprintf(val1, "%d", 0);
fflush(val1);
fprintf(val2, "%d", 0);
fflush(val2);
      // delays for given length of time in microseconds.
    usleep(500000);
//Set the pwm period
fprintf(period, "%d", 2800046);
fflush(period);
//Set the pwm duty
fprintf(duty, "%d", 1400023);
fflush(duty);
fprintf(val0, "%d", 0);
fflush(val0);
fprintf(val1, "%d", 1);
fflush(val1);
fprintf(val2, "%d", 0);
fflush(val2);
      usleep(500000);
//Set the pwm period
fprintf(period, "%d", 1275526);
fflush(period);
//Set the pwm duty
fprintf(duty, "%d", 637763);
fflush(duty);
fprintf(val0, "%d", 1);
fflush(val0);
fprintf(val1, "%d", 1);
fflush(val1);
fprintf(val2, "%d", 0);
fflush(val2);
      // delays for given length of time in microseconds.
    usleep(500000);
//Set the pwm period
fprintf(period, "%d", 568181);
fflush(period);
//Set the pwm duty
fprintf(duty, "%d", 284090);
fflush(duty);
fprintf(val0, "%d", 0);
fflush(val0);
fprintf(val1, "%d", 0);
fflush(val1);
fprintf(val2, "%d", 1);
fflush(val2);
      usleep(500000);
//Set the pwm period

fprintf(period, "%d", 1911132);
fflush(period);
//Set the pwm duty
fprintf(duty, "%d", 955566);
fflush(duty);
// delays for given length of time in microseconds.
fprintf(val0, "%d", 1);
fflush(val0);
fprintf(val1, "%d", 0);
fflush(val1);
fprintf(val2, "%d", 1);
fflush(val2);
      usleep(500000);
//Set the pwm period
fprintf(period, "%d", 1703577);
fflush(period);
//Set the pwm duty
fprintf(duty, "%d", 851788);
fflush(duty);
// delays for given length of time in microseconds.
fprintf(val0, "%d", 0);
fflush(val0);
fprintf(val1, "%d", 1);
fflush(val1);
fprintf(val2, "%d", 1);
fflush(val2);
      usleep(500000);
//Set the pwm period
fprintf(period, "%d", 1517450);
fflush(period);
//Set the pwm duty
fprintf(duty, "%d", 758725);
fflush(duty);
// delays for given length of time in microseconds.
fprintf(val0, "%d", 1);
fflush(val0);
fprintf(val1, "%d", 1);
fflush(val1);
fprintf(val2, "%d", 1);
fflush(val2);
      usleep(500000);
i++;
}
}

   // close all files
fclose(sys);
fclose(dir0);
fclose(dir1);
fclose(dir2);
fclose(val0);
fclose(val1);
fclose(val2);


return 0;
}


Learn More :