Programming the Raspberry Pi gpio in basic. Raspberry Pi I/O

Дата канвертавання26.04.2016
Памер22.48 Kb.
Programming the Raspberry Pi GPIO in BASIC.
Raspberry Pi I/O
The GPIO pins provided on P1 header include the following 12 which are straightforward I/O

GPIO 4, GPIO 7, GPIO 8, GPIO 9, GPIO 10, GPIO 11, GPIO 17, GPIO 18, GPIO 22, GPIO 23, GPIO 24 AND GPIO 25

Additionally there are GPIO 0 and GPIO 1 which each have 1K8 pull-up resistors and GPIO 14 and GPIO 15 which boot to UART TXD and RXD, but which can be restored to normal I/O. (ref 1. p2)

Bywater BASIC Interpreter

The Bywater BASIC Interpreter (bwBASIC) implements a large superset of the ANSI Standard for Minimal BASIC (X3.60-1978) and a significant subset of the ANSI Standard for Full BASIC (X3.113-1987) in C. It also offers shell programming facilities as an extension of BASIC. bwBASIC seeks to be as portable as possible.

You can download it at.
BASIC programming of the I/O

  1. Setting up a GPIO pin to be used for inputs or for outputs.

We cannot load the control words directly into the 32 bit ARM registers with 32 bit addresses, as bwBASIC has no POKE and PEEK commands and other versions of BASIC (as far as I know) only handle 8 bit registers with 16 bit addresses with these commands. So we need to export the GPIO pins, so that they exist in a file structure which we can access from basic with the OPEN command.(ref 2)

We need to do this in Linux root.

We need to run BASIC in the root too. First we go to the root, then we load bwbasic into root.

sudo -1

sudo bwbasic

REM Now to export the no4 GPIO pin for example, using a Shell command.
echo “4” > /sys/class/gpio/export
Whilst bwbasic can accommodate shell commands, and we can store a set of these commands (eg. to export a number of GPIO pins at the outset) as numbered statements in a file that can be loaded with the basic command LOAD “filename” and RUN (ref 2), the shell commands have to run as a separate file, as they cannot be run from within, as part of a basic programme.

  1. Now we can access the file containing the pin direction setting from BASIC

We can set GPIO pin 4 to input or to output by OPENing its pin direction file for output and writing “in” or “out” with a PRINT# command. (ref 2 )

10 OPEN ”O”,#1, “/sys/devices/virtual/gpio/gpio4/direction”,2

20 PRINT #1,”out”

30 CLOSE #1

REM closes the open direction file, whereupon the system performs the action of setting the direction to “out”. NB the system only carries out the action as the file is closed.(ref 3)

3. We are now able to control the output of the gpio 4 pin from BASIC
We can set the GPIO 4 pin to 1 or to 0 by OPENing its pin value file for output and writing “1” or “0” with a PRINT# command.

40 OPEN ”O”,#4, “/sys/devices/virtual/gpio/gpio4/value”,1

50 PRINT #4,”1”

60 CLOSE #4

REM turns on the output of GPIO pin 4.
REM similarly we can turn off the output of GPIO pin 4.

OPEN ”O”,#4, “/sys/devices/virtual/gpio/gpio4/value”,1

PRINT #4,”0”


4. Example of an (unstructured) BASIC programme
To read the state of a switch and control the power to two LEDs connected to GPIO pins 8,7 and 4 respectively.
Programme to set 2 pins as outputs and 1 pin as input and to read the input turning on two different combinations of the two outputs (ie output 0,1 or 1,0) depending on the state of the input (1 or 0).
sudo –i

sudo bwbasic

LOAD “export.bas”


REM a set of Shell statements to export the three GPIO pins.

10 echo “4” > /sys/class/gpio/export

20 echo “7” > /sys/class/gpio/export

30 echo “8” > /sys/class/gpio/export

NEW REM clears the export.bas programme from memory


LOAD “demo1.bas”


10 OPEN ”O”,#1, “/sys/devices/virtual/gpio/gpio4/direction”,2

20 OPEN ”O”,#2, “/sys/devices/virtual/gpio/gpio7/direction”,2

30 OPEN ”O”,#3, “/sys/devices/virtual/gpio/gpio8/direction”,2

REM opens the three pin direction files

40 PRINT #1, “out”

50 PRINT #2, “out”

60 PRINT #3, “in”

REM sets GPIO pins 4 and 7 as outputs and GPIO pin 8 as input.

70 CLOSE #1

80 CLOSE #2

90 CLOSE #3

REM closes all open files, allowing the system to perform the direction settings.

100 OPEN ”I”,#8, “/sys/devices/virtual/gpio/gpio8/value”,1

REM opens the GPIO pin 8 value file

110 INPUT #8,x

REM reads the value of the input pin and stores the value in numerical variable x

120 CLOSE #8

REM closes the open file, allowing the system to read the value of the input pin and store the value in numerical variable x.

130 OPEN “O”,#1, “/sys/devices/virtual/gpio/gpio4/value”,1

140 OPEN “O”,#2, “/sys/devices/virtual/gpio/gpio7/value”,1

REM opens the GPIO pins 4 and value files ready for outputting 1s and 0s.

150 IF x<1 THEN GOTO 160 ELSE GOTO 190

REM tests the state of the switch (1 or0) and directs the program to generate the appropriate outputs

160 PRINT #1,”1”

170 PRINT #2,”0”

180 GOTO 210

190 PRINT #1,”0”

200 PRINT #2,”1”

210 CLOSE #1

220 CLOSE #2

REM Closes the files and allows the outputs to light the LED

230 END.
When all is done, we should unexport the GPIO pins, to leave the R-Pi as we found it.(Ref 1.)

LOAD “unexport.bas”


REM a set of Shell statements to unexport the three GPIO pins.

10 echo “4” > /sys/class/gpio/unexport

20 echo “7” > /sys/class/gpio/unexport

30 echo “8” > /sys/class/gpio/unexport

5. A simple circuit to provide the switched input and the two LED outputs.

Ancient Mariner. Dec. 2012

  1. RPi Low-level peripherals.

  1. Ed Beynon.

  1. Arthur Kaletzky. Private communication. 25/10/2012

  1. bwbasic manual.

База данных защищена авторским правом © 2016
звярнуцца да адміністрацыі

    Галоўная старонка