Where I discuss the various methods of increasing execution speed in Forth and demonstrate the ease in doing so.
In this entry, I showed the relative speeds of board/language combinations from a ARM M0 running CircuitPython to a Pi Pico running C. While execution speed is important in embedded microcontrollers, development speed is equally important along with ease of development. A great attribute of Forth is its ability to combine all three. In this entry, I’ll show how you can start with a simple, fast example and make it significantly faster without great difficulty.
Where I discus debouncing buttons in Flashforth as well as indexing arrays.
Elliott Williams had a great post several years ago as to the best solution to debouncing buttons. The technique worked extremely well in C, and I wanted to attempt it in Forth. It actually works better in Forth, as Forth easily handles the 16-bit data to de-bounce properly. I’ll work through the technique in FlashForth as well as spend some time at the end as to the execution timing of the technique.
Where I describe in detail how to compile a new version of FlashForth.
Its helpful to be able to compile a new version of FlashForth. A simple reason is to increase the baudrate from 38400 to 250000, increasing your productivity. It isn’t difficult, however the tool chain can be a bit complex.
You will need to install both the MPLAB IDE and the XC8 compiler for this exercise. You will also need a hardware programmer such as the Atmel ICE or Microchip SNAP. And finally, you will definitely need the source code of which to compile, Flashforth on github.
UPDATED: This page contains a both a Forth hardware abstraction level (HAL) much like the m328def.inc file for AVR programming in C and an example of debouncing buttons.
This HAL provides the words:
The Arduino pins are defined by bit port so one can use the following commands using the Arduino pin number. This is true for pins D0-D13.
high ( bit port -- ) set a Arduino pin high
low ( bit port -- ) set a Arduino pin low
toggle ( bit port -- ) toggle output value on an Arduino pin
output ( bit port -- ) set Arduino pin as output
input ( bit port -- ) set a Arduino pin as input
pullup ( bit port -- ) set Arduino pin as input_pullup
read ( bit port -- f ) read a Arduino pin, returns bit value
For example:
Why the microcontroller datasheet is so important to programming in Forth.
The ATmega328P datasheet is critical to understanding how to program the 328P. Exploring it with Forth is the joy of Forth as Forth allows you to interactively test commands, ports, timers, ADC’s etc of the 328P. And once you are convinced you have the correct set of commands to make what you want to happen, you can codify it into a word that becomes part of the vocabulary of Forth.
Where I use Forth to develop a better understanding of the ATmega328P PWM capabilities. Revised from original post on April 17, 2021
Pulse Width Modulation (PWM) is a technique used to control analog circuits using digital signals. It is the capability to change either the frequency or duty cycle of a digital signal. The former is the number of times the signal switches from low to high in a given period of time and the latter, is how long it is either high or low.
Where I describe how to use Forth with an Arduino Uno and make it easier to program the Uno in Forth.
The Arduino Uno benefits from an incredible software framework. Between the programs already developed by the Arduino organization, and the hundreds of libraries which were created by others, you can find a program which does what you need. This makes it extremely easy to hit the ground running with a project. FlashForth isn’t like this.
Where I demonstrate your first embedded application, much like the C Language “Hello, World” program, blink.
To begin to understand how to program in Forth, I’ll iterate over several versions of blink.
First, let’s interactively light the built-in LED. On an Uno, we know it is pin 13, which corresponds to Port B, bit 5 on our microcontroller, the ATmega328P.
This is lesson one, all references in Forth, are references to the ATmega328P, which is the microcontroller on the Uno, and not the pins on the Uno. This is why the data sheet for the ATmega328P is so important. I’ll call out the specific references as we go, however, the more you use FlashForth, the more you will come to read and understand the ATmega328P datasheet. As you begin to write more Forth programs, you will be able to create the words necessary to reference the Uno, just as you did with the Arduino framework.
UPDATED: Where I describe how to use FlashForth, a phenomenal version of Forth for the ATmega328P to learn how to use Forth in an embedded system.
This video demonstrates the ease of loading FlashForth on to an Arduino Uno, and replacing it with Optiboot, when you wish to use the Arduino software framework, again.
Where I describe how I develop Forth on my Mac using Sublime Text 3 and Serial, as my Forth IDE.
Make the change in: mecrisp-stellaris-x.y.z/mecrisp-stellaris-source/rp2040-ra/terminal.s lines 165-166 and added the comment below the lines, replacing the comment line 168: