Skip to content

Instantly share code, notes, and snippets.

@itrobotics
Last active June 7, 2018 16:33
Show Gist options
  • Save itrobotics/c02e1f0d666af2bc3b72934476415f63 to your computer and use it in GitHub Desktop.
Save itrobotics/c02e1f0d666af2bc3b72934476415f63 to your computer and use it in GitHub Desktop.
// blink.c
//
// Example program for bcm2835 library
// Blinks a pin on an off every 0.5 secs
//
// After installing bcm2835, you can build this
// with something like:
// gcc -o blink blink.c -l bcm2835
// sudo ./blink
//
// Or you can test it before installing with:
// gcc -o blink -I ../../src ../../src/bcm2835.c blink.c
// sudo ./blink
//
// Author: Mike McCauley
// Copyright (C) 2011 Mike McCauley
// $Id: RF22.h,v 1.21 2012/05/30 01:51:25 mikem Exp $
#include <bcm2835.h>
#include <stdio.h>
// Blinks on RPi Plug P1 pin 11 (which is GPIO pin 17)
#define LED1 5
#define LED2 6
#define LED3 13
#define LED4 26
#define LED5 12
#define COM 22
#define RELAY 27
#define PWM0 18
#define PWM1 19
#define IR_TX 25
#define IR_RX 17
#define DIP1 20
#define DIP2 21
#define BUTTON1 24
#define BUTTON2 23
#define BUZZER 16
int main(int argc, char **argv)
{
// If you call this, it will not actually access the GPIO
// Use for testing
// bcm2835_set_debug(1);
int i;
int value1,value2,value3,value4;
int prev_state=0;
int prev_state_relay=0;
int led_array[]={LED2,LED4,LED3,LED5};
int n=sizeof(led_array)/sizeof(led_array[0]);
if (!bcm2835_init())
return 1;
// Set the p5in to be an output
bcm2835_gpio_fsel(COM, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_write(COM, HIGH);
bcm2835_gpio_fsel(LED1, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_fsel(BUZZER, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_write(BUZZER, LOW);
bcm2835_gpio_fsel(RELAY, BCM2835_GPIO_FSEL_OUTP);
//DIP. button setup as input
bcm2835_gpio_fsel(DIP1, BCM2835_GPIO_FSEL_INPT);
bcm2835_gpio_fsel(DIP2, BCM2835_GPIO_FSEL_INPT);
bcm2835_gpio_fsel(BUTTON1, BCM2835_GPIO_FSEL_INPT);
bcm2835_gpio_fsel(BUTTON2, BCM2835_GPIO_FSEL_INPT);
//set pwm0, pwm1
bcm2835_gpio_fsel(PWM0,BCM2835_GPIO_FSEL_ALT5); //set gpio for PWM0
bcm2835_pwm_set_clock(1920); //set min frequence =19.2M/1920
bcm2835_pwm_set_mode(0,1,1); //(channel,PWM0 MSEN enable , PWM0 enable)
bcm2835_pwm_set_range(0,200); //set period=20ms
for (i=0;i<n;i++) {
bcm2835_gpio_fsel(led_array[i], BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_write(led_array[i], LOW);
}
i=0;
while (1)
{
value1= bcm2835_gpio_lev(DIP1);
value2= bcm2835_gpio_lev(DIP2);
value3= bcm2835_gpio_lev(BUTTON1);
value4= bcm2835_gpio_lev(BUTTON2);
printf("DIP1=%d,DIP2=%d,BUTTON1=%d,BUTTON2=%d\n", value1,value2,value3,value4);
// led blinking
bcm2835_gpio_write(led_array[i], HIGH);
bcm2835_delay(100);
bcm2835_gpio_write(led_array[i], LOW);
//check led1
if (value1==0 && value2==0) {
i=(i+1)%n;
bcm2835_gpio_write(LED1, HIGH);
} else {
i=(i-1)%n; if (i<0) i=n-1;
bcm2835_gpio_write(LED1, LOW);
}
//check buzzer
if (value3==0 && value4==0) {
bcm2835_gpio_write(BUZZER, HIGH);
} else {
bcm2835_gpio_write(BUZZER, LOW);
}
//check relay
if (value4==0) {
bcm2835_gpio_write(LED1, HIGH);
bcm2835_gpio_write(RELAY, LOW);
} else if (value4==1) {
bcm2835_gpio_write(LED1, LOW);
bcm2835_gpio_write(RELAY, HIGH);
}
//check pwm0
if (value3==0 && prev_state!=1) {
bcm2835_pwm_set_mode(0,1,1);
prev_state=1;
bcm2835_pwm_set_data(0,20); //set pulse width
} else if (value3==1 && prev_state!=0) {
bcm2835_pwm_set_mode(0,1,1);
bcm2835_pwm_set_data(0,5); //set pulse width
prev_state=0;
} else {
bcm2835_pwm_set_mode(0,1,0);
}
bcm2835_delay(100);
}
bcm2835_close();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment