Binary Counter

In this project, I created a binary counter using the GPIO pins on my raspi.

The program:

  1 #include "rpi.h"
  2 #include <stdlib.h>
  3 
  4 int main()
  5 {
  6 
  7     char *binary, count = 0, holder = 0; // array to hold bit values, variable to check the counter, variable to compare values
  8 
  9     int i; // variable to control loop that sets pins on or off
 10 
 11     binary = (char *)malloc(sizeof(char)*4); // array for binary counter
 12 
 13     if(map_peripheral(&gpio) == -1)
 14     {
 15         printf("Failed to map the physical GPIO registers into the virtual memory space.\n");
 16         return -1;
 17     }
 18 
 19     // Define pins 7, 8, 9, and 10 as output
 20 
 21 
 22     OUT_GPIO(7);
 23 
 24 
 25     OUT_GPIO(8);
 26 
 27 
 28     OUT_GPIO(9);
 29 
 30 
 31     OUT_GPIO(10);
 32 
 33     while(1) // main bulk of the program. Checks value of holder (count) and assigns values to array accordingly
 34     {
 35         holder = count;
 36 
 37         if((holder - 8) >= 0) // assigns (or does not) first binary bit to array if holder is greater than or equal to 8 (up to 15)
 38         {
 39             *(binary + 0)  = 1;
 40             holder = holder - 8; // subtracts 8 from holder and moves to the next if block
 41         }
 42         else
 43             *(binary + 0) = 0; // if holder not 8 or greater, sets first bit equal to 0
 44 
 45         if((holder - 4) >= 0) // does the same as the previous except considering a value of 4
 46         {
 47             *(binary + 1) = 1;
 48             holder = holder - 4; // subtracts 4 from holder
 49         }
 50         else
 51             *(binary + 1) = 0; // if holder not 4 or greater, sets second bit equal to 0
 52 
 53         if((holder - 2) >= 0) // same as previous, considers value of 2
 54         {
 55             *(binary + 2) = 1;
 56             holder = holder - 2; // subtracts 2 from holder
 57         }
 58         else
 59             *(binary + 2) = 0; // if holder not 2 or greater, sets third bit equal to 0
 60 
 61         if(holder == 1) // pretty self explanatory
 62         {
 63             *(binary + 3) = 1; // sets fourth and last bit to 1 (on)
 64         }
 65         else
 66             *(binary + 3) = 0; // sets fourth and last bit to 0 (off)
 67 
 68         for(i = 0; i <= 3; i++) // loop which turns on/off GPIO pins according to the values in the array
 69         {
 70             if( *(binary + i) == 1)
 71             {
 72                 GPIO_SET = 1 << (i + 7); // sets specific GPIO pin on if value in the memory block of array is 1
 73             }
 74             else
 75                 GPIO_CLR = 1 << (i + 7); // sets specific GPIO pin off if value in the memory block of array is 0
 76         }
 77 
 78         count++; // increment count
 79 
 80         if(count > 15) // if block to set value back to 0
 81         {
 82             count = 0;
 83         }
 84 
 85         usleep(1000000); // sleep for one second
 86     }
 87 
 88     return 0;

It successfully runs a binary counter which sets the LED's on or off depending on the value stored in the corresponding location of data in the array.