Including and Using Libraries in Your Computer Program

Including and Using Libraries in C:

The small C program below toggles pin 12 of port 1 on the Raspberry Pi B+. It includes the bcm2835.h library on the first line. This library includes several functions that give access to the GPIO (General Purpose Input and Output) pins of the Raspberry Pi in a way that is easy to understand. The "#include" part of this line is known as a preprocessor directive. It tells the compiler to go get the file bcm2835.h, and include it into our program. Later in the code the "bcm2835_gpio_set()" function is called. This function comes from and is defined by the bcm2835.h library. If we did not include the bcm2835.h library, we would not be able to use the bcm2835_gpio_set() function. This function simply sets the associated pin to a high voltage.

#include <bcm2835.h>

#define PIN18 RPI_GPIO_P1_12

int main(int argc, char **argv){
	if(!bcm2835_init())return 1; /* Initialize */
	bcm2835_gpio_fsel(PIN18, BCM2835_GPIO_FSEL_OUTP); /*Sets PIN18 to output*/
	
	while(1){		             
		bcm2835_gpio_set(PIN18); /* PIN18 = high */
		bcm2835_gpio_clr(PIN18); /* PIN18 = low  */
	}
}
Home | Products | Electronics | Programming | SpazzOuts | Contact
© Copyright 2014 - 2017 SpazzTech LLC. All Rights Reserved
Made with Adobe Muse