Table of Contents

Objectives

To build a binary counter using LEDs and a raspberry pi

Prerequisites

raspberry pie,monitor, keyboard, data cable, hdmi to usb cable, network cable, LEDs

Background

The Raspberry Pi is a credit-card-sized single-board computer developed in the UK by the Raspberry Pi Foundation with the intention of promoting the teaching of basic computer science in schools.[7][8][9][10][11]

The Raspberry Pi is manufactured through licensed manufacturing deals with Newark element14 (Premier Farnell), RS Components and Egoman. All of these companies sell the Raspberry Pi online.[12] Egoman produces a version for distribution solely in China and Taiwan, which can be distinguished from other Pis by their red coloring and lack of FCC/CE marks. The hardware is the same across all manufacturers.

Code

#include "rpi.h"
#include <stdlib.h>
#include <stdio.h>

void clear()
{
    GPIO_CLR = 1 << 23;
    GPIO_CLR = 1 << 11;
    GPIO_CLR = 1 << 10;
    GPIO_CLR = 1 << 7; 
    sleep(1); 
}


int main()
{

int i;
char place = 0, counter = 0, *bin;
bin = (char *)malloc(sizeof(char) +4); 


  if(map_peripheral(&gpio) == -1) 
  {
    printf("Failed to map the physical GPIO registers into the virtual memory space.\n");
    return -1;
  }

  // Define pin 7 as output
    INP_GPIO(25);
  OUT_GPIO(23);

  INP_GPIO(8);//red
  OUT_GPIO(11);

  INP_GPIO(24);//blue second in
  OUT_GPIO(10);

  INP_GPIO(22);//yellow far right
  OUT_GPIO(7);


	int pins[4];
	pins[0] = 23;
	pins[1] = 11;
	pins[2] = 10;
	pins[3] = 7;




    while(1)
    {   
        place = counter;

        if((place - 8) >= 0)
        {   
            *(bin + 0)  = 1;
         place = place - 8;
        }   
        else
            *(bin + 0) = 0;

        if((place - 4) >= 0)
        {   
            *(bin + 1) = 1;
            place = place - 4;
        }   
        else
            *(bin + 1) = 0;

        if((place - 2) >= 0)
        {   
            *(bin + 2) = 1;
            place = place - 2;
        }   
        else
            *(bin + 2) = 0;

        if(place == 1)
        {   
            *(bin + 3) = 1;
        }   
        else
            *(bin + 3) = 0;

        for(i = 0; i <= 3; i++)
        {   
            if( *(bin + i) == 1)
            {   
                GPIO_SET = 1 << pins[i]; 
            }   
            else
                GPIO_CLR = 1 << pins[i]; 
        }   
		printf( "%d", *(bin + 0));
		printf( "%d", *(bin + 1));
		printf( "%d", *(bin + 2));
		printf( "%d\n", *(bin + 3));
		
        counter++;

        if(counter > 15) 
        {   
            counter = 0;
        
		}
        
		usleep(1000000);   
}

clear();

  return 0; 
}

Reflection

this counter has the same results as the original but this one uses arrays