!ARMForth32 on the Raspberry Pi

Home Page
 What is Forth?
 Why use Forth?
Code Extensions
More Information

Examples using !ARMForth32

 This page describes the example software available in the Downloads section on the next page. These are divided into three setions:
1. software which does not use the GPIO pins
2. software which uses the GPIO pins with Tank's "GPIO" module
3. software which uses only Forth to control the GPIO pins
There are a total of 20 example programs in this collection, and a menu-based program may be used for selecting any one of them.
The individual examples are as follows:
1. Non-GPIO examples
1.1. FTPtable - produces a screen display of the correction factors which are applied to air-ionisation chambers as a result of changes in the air temperature and pressure. Written to check the use of the additional floating point module.
1.2. Fast Fourier Transform (FFT) - to demonstrate the usefulness and speed of Forth when applied to a standard scientific/mathematical procedure.
Several mathematcal examples may be invoked automatically by typing one word.
1.3. Simultaneous equations - creation of and solved by 'pivotal elimination'.
1.4. Calculation of the values of 3x3 Determinants by simplification.
1.5. Calculation of the values of 4x4 Determinants by simplification.
1.6. Matrix multiplication.
1.7. Two examples of using recursion (where a Forth word calls itself).
1.8. An example of multitasking using the FFT program and graphical shapes.

2. GPIO examples, using GPIO relocatable module by "Tank"
These example programs require wiring the GPIO lines to various electronic components and motors. Circuit diagrams and photographs are on the website.
2,1, DCMotor - controlling a DC motor from two GPIO pins and L293D chip. Pulse width modulation to control speed is performed in software.
2.2. SPI_3208 - using the SPI bus to read analogue voltages via the MCP3208 Analogue to Digital Convertor.
2.3. DCservo - combining the two programs above to servo the DC motor using a rotary potentiometer connected to its shaft.
2.4. 2Steppers - control of 1 or 2 stepper motors.
2.5. ADS1115c - use of the IIC interface for connection the ADS1115 Analogue to Digital Convertor. This uses the IIC controller of RiscOS and GPIO lines 2 and 3.
2.6. I2C_bitbang - as 2.5., but simulation of the IIC interface in software (termed "bit-banging"). Any two GPIO pins can then be used as an IIC interface
(GPIO 5 and 6 in this example).

3. GPIO examples, using GPIO under Forth control
All the above examples in section 2 above are available when using Forth routines directly to access the GPIO control registers on the microprocessor i.e. no additional software is required in order to control the GPIO lines. This was done as an instructive exercise and demonstrates the power and speed of Forth. The program names are as those above, but with "Forth" on the end.

Notes on some of the software examples

1. Fast Fourier Transform (FFT). The FFT breaks up a signal into its constituent frequencies, so that adding them together again restores the original signal. This project was chosen in order to use the additional code extensions I had written for floating point variables and for complex numbers, as the FFT uses these liberally. The main routine is taken from the example in "Scientific Forth" by Julian V. Noble, amended for !ARMForth's requirements. Also key to understanding the method was aided by a perusal of Chapter 12 of "The Scientist and Engineer's Guide to Digital Signal Processing" by Steven W. Smith. The full code is available in the Support directory of the Downloads. On the Pi, a signal spanning 1024 points takes approximately 4.5 msec to perform an FFT, which seems quite fast to me! The example software also has various test data (sines, cosines etc) to test both the FFT and the inverse FFT (which should recover the initial waveform).

2. Stepper Motors. I am indebted to Jochen Lueg's website (http://roevalley.com) for getting me started with both stepper motor and DC motor control, as I was able to translate and modify his Basic V programs into Forth without much difficulty. Both half-step and full-step programs are available from the Download section here.

3. DC Motors. Although there is a facility on the PI GPIO lines to use some of them for Pulse Width Modulation (PWM) to control the speed of a DC motor, it is also possible to use two of the general-purpose GPIO lines and program the PWM by hand, and this is the approach I used (motivated by Jochen Lueg's examples in Basic V). The Gertboard was used to debug the software, which was then transferred to a breadboard setup using an L293D chip (which can control two DC motors or one stepper). This is described in the Downloads section.

4. Analogue to Digital Conversion (ADC). I have used two ICs to accomplish ADC: an ADS1115 using the IIC bus, and an MCP3208 using the SPI bus - both on the Pi. The use of either data bus requires some delving into the datasheets of the ICs and some understanding of how to set up and use the busses. The software in the Downloads section provides examples of this, with some comments. These were also breadboarded with point-to-point connections to the Pi GPIO lines - circuits and photos come with the downloads.

5. Servo Control of DC Motor. With a stepper motor one only needs to count the pulses from a reference point and use an appropriate scaling factor to be able to calculate the position of the object being moved/rotated, assuming that the motor does not stall. It is also possible to use a potentiometer attached to some moving part to give feedback of the current position. If one then informs the program of a new required position, the motor may be persuaded to move until the value from the potentiometer coincides with the expected value for that position - the motor has been servo'd. This is of more use when using a DC motor, as there is no equivalent to pulse-counting. However, the potentiometer always gives information as to the position of the hardware, even when it has just been switched on. Obviously, this process requires a means to use the analogue voltage from the potentiometer, so the ADC software previously created may then be used for this purpose.

6. I2C "bit-banging". This is software simulation of the control of an I2C bus, normally carried out with specialist routines from the microprocessor. Normally GPIO pins 2 and 3 are used by the microprocessor for data transfer and clock signal respectively. For the bit-banging exercise, pins 5 and 6 were used instead. They were first configured as outputs and set to 0, with pull-up option to 'high' for input. Hence, for example, the data line toggles between output for 0 and input for 1, so that the slave chip can assert a zero or 1 for input to the GPIO pin. The I2C requirements for 'start' and 'stop' and for reading and writing data are then used to access the slave chip (an ADC in this case).