Page 3 of 21
Chapter 4
Page 50
#include <bcm2835.h>
#include <stdio.h>
int main(int argc, char **argv)
{
if (!bcm2835_init())
return 1;
bcm2835_gpio_fsel(RPI_BPLUS_GPIO_J8_07, BCM2835_GPIO_FSEL_OUTP);
for (;;)
{
bcm2835_gpio_write(RPI_BPLUS_GPIO_J8_07, HIGH);
bcm2835_gpio_write(RPI_BPLUS_GPIO_J8_07, LOW);
}
bcm2835_close();
return 0;
}
Page 62
#define _DEFAULT_SOURCE
#include <bcm2835.h>
#include <stdio.h>
#include <time.h>
int main(int argc, char **argv)
{
if (!bcm2835_init())
return 1;
bcm2835_gpio_fsel(RPI_BPLUS_GPIO_J8_07, BCM2835_GPIO_FSEL_OUTP);
volatile int i;
struct timespec delay = {0, 10 * 1000};
for (;;)
{
bcm2835_gpio_set(RPI_BPLUS_GPIO_J8_07);
nanosleep(&delay, NULL);
bcm2835_gpio_clr(RPI_BPLUS_GPIO_J8_07);
nanosleep(&delay, NULL);
}
bcm2835_close();
return 0;
}
Page 64
#include <bcm2835.h>
#include <stdio.h>
int main(int argc, char **argv)
{
if (!bcm2835_init())
return 1;
bcm2835_gpio_fsel(RPI_BPLUS_GPIO_J8_07, BCM2835_GPIO_FSEL_OUTP);
volatile int i;
int n = 10;
for (;;)
{
for (i = 0; i < n; i++)
{
};
bcm2835_gpio_write(RPI_BPLUS_GPIO_J8_07, HIGH);
for (i = 0; i < n; i++)
{
};
bcm2835_gpio_write(RPI_BPLUS_GPIO_J8_07, LOW);
}
bcm2835_close();
return 0;
}
Page 65
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <bcm2835.h>
#include <time.h>
#define BILLION 1000000000L
int main(int argc, char** argv) {
struct timespec btime, etime;
volatile int i;
clock_gettime(CLOCK_REALTIME, &btime);
for (i = 0; i < 10000000; i++) {
};
clock_gettime(CLOCK_REALTIME, &etime);
double nseconds = (double) ((etime.tv_sec - btime.tv_sec)* BILLION)+(double) (etime.tv_nsec - btime.tv_nsec);
int n = (int) 10 /nseconds * BILLION + 0.5;
printf("time = %f (s) \n \r",nseconds / BILLION);
printf("n= %d \n\r", n);
return (EXIT_SUCCESS);
}
Page 66
#include <bcm2835.h>
#include <stdio.h>
int main(int argc, char **argv)
{
if (!bcm2835_init())
return 1;
bcm2835_gpio_fsel(RPI_BPLUS_GPIO_J8_07,
BCM2835_GPIO_FSEL_OUTP);
for (;;)
{
bcm2835_gpio_set(RPI_BPLUS_GPIO_J8_07);
bcm2835_delayMicroseconds(1);
bcm2835_gpio_clr(RPI_BPLUS_GPIO_J8_07);
bcm2835_delayMicroseconds(1);
}
bcm2835_close();
return 0;
}
Page 67
#include <bcm2835.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
if (!bcm2835_init())
return 1;
bcm2835_gpio_fsel(RPI_BPLUS_GPIO_J8_07,BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_fsel(RPI_BPLUS_GPIO_J8_11,BCM2835_GPIO_FSEL_OUTP);
volatile int i;
for (;;)
{
bcm2835_gpio_write(RPI_BPLUS_GPIO_J8_07, HIGH);
bcm2835_gpio_write(RPI_BPLUS_GPIO_J8_11, HIGH);
bcm2835_gpio_write(RPI_BPLUS_GPIO_J8_07, LOW);
bcm2835_gpio_write(RPI_BPLUS_GPIO_J8_11, LOW);
}
return (EXIT_SUCCESS);
}
Page 68
#include <bcm2835.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
if (!bcm2835_init())
return 1;
bcm2835_gpio_fsel(RPI_BPLUS_GPIO_J8_07, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_fsel(RPI_BPLUS_GPIO_J8_11, BCM2835_GPIO_FSEL_OUTP);
uint32_t mask = (1 << RPI_BPLUS_GPIO_J8_07) | (1 << RPI_BPLUS_GPIO_J8_11);
for (;;)
{
bcm2835_gpio_set_multi(mask);
bcm2835_gpio_clr_multi(mask);
}
return (EXIT_SUCCESS);
}