User Manual

19
In the diagram in Figure 12, black circles show which pins are being connected, and black lines
between two pins indicate that jumpers (if they are adjacent) or straps (if they are further apart) are
used to connect them.
Buttons Test in C
The code specific to the buttons test is buttons.c. In the main routine, the connections
required for this test are firstly printed to the terminal (a text description of the wiring diagram above).
When the user verifies that the connections are correct, setup_io (described on page 12) is called
to get everything ready.
setup_gpio is then called, which gets GPIO pins 1 to 3 ready to be used as pushbutton inputs. It
does this by first using the macro INP_GPIO(n) (where n is the GPIO pin number) to select these 3
pins for input.
Then pins are required to be pulled high: the buttons work by dropping the voltage down to 0V when
the button is pressed, so it needs to be high when the button is not pressed. This is done by setting
GPIO_PULL to 2, the code for pull-up. Should it ever be required, the code for pull-down is 1. The
code for no pull is 0; this will allows this pin to be used for output after it has been used as a
pushbutton input. To apply this code to the desired pins, set GPIO_PULLCCLK0 = 0X03800000.
This hexadecimal number has bits 23, 24, and 25 set to 1 and all the rest set to 0. This means that the
pull code is applied to GPIO pins 23, 24, and 25. A short_wait allows time for this to take effect,
and then GPIO_PULL and GPIO_PULLCLK0 are set back to 0.
Back in the main routine, a loop is entered in which the button states are read (using macro
GPIO_IN0), grabbing bits 23, 24, and 25 using a shift and mask logical operations, and, if the button
state is different from before, it is printed out in binary: up (high) is printed as „1‟ and down (low) is
printed as „0‟. This loop executes until a sufficient number of button state changes have occurred.
After the loop, unpull_pins is called, which undoes the pull-up on the pins, then call
restore_io in gb_common.c to clean up.
Buttons Test in Python
This program (buttons-rg.py) is only available using the RPi.GPIO package at the moment
because the pull-up facility in WiringPi for Python is not yet available. The buttons cannot be used
without this facility.
First the program imports the RPi.GPIO module it needs to handle GPIO control. Then the command
GPIO.setmode(GPIO.BCM)sets up the BCM numbering scheme for the pins. The result of this is
that the pin numbers in the Python code are the numbers that the BCM2835 (the Raspberry Pi
processor) uses to refer to the pins. Otherwise, the numbers in the Python code will refer to the pin
numbers in the J1 header (which is the same as their placement in the P1 header on the RPi).
The next two lines set up ports 23-25 with pull-up:
for i in range(23,26):
GPIO.setup(i, GPIO.IN, pull_up_down=GPIO.PUD_UP)