Projects: Working Forth Programs

Note

All my projects are licensed under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later versions see http://www.gnu.org/licenses/. . Projects by other contributors are covered by the licence terms in their code.

Note

All project code is CMSIS-SVD compliant for easy code reuse.

  • You will need to load memory map words for all registers used. See Svd2forth-v2 for a easy way to generate a memmory mapped file for any of the projects below.

Window Comparator

  • Hardware: STM32F0 Discovery Board using a STM32F0xx MCU, but any STM32F0 with a Comparator should work
  • Clock: 8 Mhz using the stm32f0 target chip internal RC clock as default with Mecrisp-Stellaris
  • This program demonstrates Comparator 1 & 2 in ‘Window’ mode by lighting the BLUE and GREEN LEDS depending on voltage (potentiometer position), and gives a colored voltage status on the terminal.
  • standalone, no other files needed

DOWNLOAD: f051-comparator-tests.fs

A Sunrise and Sunset Calculator

By Andrew Palm. Requires his maths library be loaded first.

DOWNLOAD: sunrise-sunset-v2.fs

Note

Usage:

  1. set-zenith-code.
zenith code type of sunrise/sunset
0 Official sunrise and sunset
1 civil twilight
2 nautical twilight
3 astronomical calculations
  1. Run “sunrise-sunset-utc” with inputs lat, long, year, month, day.
  2. To see the local sunrise and sunset times, plus hours of daylight and local solar noon, use the word “show-local-times” with input the integer utc offset for the time zone.

Examples

#include sunrise-sunset-v2.fs

0 set-zenith-code
-33,750000 150,700000  2018 02 25  sunrise-sunset-utc cr
11 show-local-times

1 set-zenith-code
-33,750000 150,700000  2018 02 25  sunrise-sunset-utc cr
11 show-local-times

2 set-zenith-code
-33,750000 150,700000  2018 02 25  sunrise-sunset-utc cr
11 show-local-times

3 set-zenith-code
-33,750000 150,700000  2018 02 25  sunrise-sunset-utc cr
11 show-local-times

\ 0 set-zenith-code  ok.
\ -33,750000 150,700000  2018 02 25  sunrise-sunset-utc cr
\  ok.
\ 11 show-local-times
\ Sunrise:        06:41:07
\ Sunset:         19:39:14
\ Daylight:       12:58:07
\ Solar noon:     13:10:10
\  ok.
\
\ 1 set-zenith-code  ok.
\ -33,750000 150,700000  2018 02 25  sunrise-sunset-utc cr
\  ok.
\ 11 show-local-times
\ Sunrise:        06:15:32
\ Sunset:         20:04:47
\ Daylight:       13:49:15
\ Solar noon:     13:10:10
\  ok.
\
\ 2 set-zenith-code  ok.
\ -33,750000 150,700000  2018 02 25  sunrise-sunset-utc cr
\  ok.
\ 11 show-local-times
\ Sunrise:        05:45:13
\ Sunset:         20:35:04
\ Daylight:       14:49:50
\ Solar noon:     13:10:08
\  ok.
\
\ 3 set-zenith-code  ok.
\ -33,750000 150,700000  2018 02 25  sunrise-sunset-utc cr
\  ok.
\ 11 show-local-times
\ Sunrise:        05:13:59
\ Sunset:         21:06:15
\ Daylight:       15:52:16
\ Solar noon:     13:10:07
\  ok.

Australian Metrological published times, Penrith, Sydney, Australia for 2018 02 25
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
\ 06:16 EDT   first light
\ 06:41 EDT   sunrise
\ 19:39 EDT   sunset
\ 20:05 EDT   last light

Benchmark Your Forth MCU ?

  • Hardware: STM32F0 Discovery Board using a STM32F0xx MCU
  • Clock: 8 Mhz
  • This program uses a Greatest Common Divisor algorithm to benchmark CPU speed, and measures the time with a Interrupt driven Sysclock. A list of other system results is included in the source file.
  • Instructions are in the sourcecode

Download: Benchmark

Push button switch debounce demo

  • Hardware: STM32F0 Discovery Board using a STM32F0xx MCU
  • Clock: 8 Mhz using the stm32f0 target chip internal RC clock as default with Mecrisp-Stellaris
  • Must be used with the e4thcom Terminal. This is interactive, press return at each pause.
  • This program demonstrates the effect of increasing the debounce delay while counting USER push button presses.
  • The MCU just spins to provide the debounce delay

Download: Push button switch debounce demo

Square Wave Generator

  • Hardware: STM32F0 Discovery Board using a STM32F0xx MCU
  • Clock: 8 Mhz using the stm32f0 target chip internal RC clock as default with Mecrisp-Stellaris
  • Duty Cycle: 50 %
  • Frequency Range: 123 Hz to 350 KHz
  • Waveform: Square wave at 3.0 Volts
  • Output: on pin PB-1
  • Registers required: RCC, GPIOB, TIM14
  • Files required: freq-gen-memmap.txt and freq-gen.txt
  • Upload to your F0 Mecrisp-Stellaris Forth MCU in this order: freq-gen-memmap.txt, freq-gen.txt and you should see a 5000 Hz square wave appear on PB-4
  • Enter “<freq> hz” at the Forth prompt to change the frequency. “<freq>” must be between 123 and 350000 or PB-1 will just go to a High level.

Download: freq-gen-memmap.txt Download: freq-gen.txt

Screenshot:-
Forth> 20000 hz
-> 20000 hz
<- for a output of 20000 Hz (CLOCK = 8MHz, Prescaler = 0) TIM14_ARR is $00000190
<- ok.

100 Microsecond Systick

  • STM32F0 Discovery Board using a STM32F0xx MCU
  • Utilises the 8MHz MCO crystal controlled frequency from the SWD programmer
  • Overclocked to 96 MHz using the crystal derived 8 MHz MCO clock from the SWD programmer.
  • GPIOA_8 may be monitored to confirm the overclock is working. Look for 48MHz (1/2 clock) on this pin.
  • A marker pulse on GPIOB_2 is generated every Systick for external measurement. Period = 100us, Width = 800 nS
  • Flashes the BLUE LED (on GPIOC_8) at a rate of 1 second

Download: 100 Microsecond Systick

Overclock to 96 MHz

  • STM32F0 Discovery Board using a STM32F0xx MCU
  • Utilises the 8MHz MCO crystal controlled frequency from the SWD programmer
  • Increases the default Mecrisp-Stellaris speed by 12 times
  • Optional MCO frequency monitor pin on GPIOA-8
  • Power usage STM32F051: 20mA (3mA @ 8MHz)
  • May not program Flash memory reliably, but seems to run from Ram without any problems.

Download: Overclock to 96 MHz

F0 Discovery 48 MHz System Clock

See here<f0disco-48mhz>

Delay using the interrupt driven 1 millisecond Systick

  • STM32F0 Discovery Board using a STM32F0xx MCU
  • Default Mecrisp-Stellaris 8Mhz RC system clock
  • Blue LED blinks at one second Interval
  • 1mS marker pulse is available on PB2 for scope fine tuning
  • ‘msdelay’ word provides a accurate time delay via the systick
  • By Terry Porter based on code by kfoltman

Download: msdelay

Timer, 1mS Interrupt Systick

  • STM32F0 Discovery Board using a STM32F0xx MCU
  • Default Mecrisp-Stellaris 8Mhz RC system clock
  • Interrupt driven with 1mS Systick
  • Blue LED blinks at one second Interval
  • 1mS marker pulse is available on PB2 for scope fine tuning
  • Use to time software or hardware. ‘delay’ example included
  • Standalone code, no other files needed
  • By Terry Porter based on code by kfoltman

Download: timer.fs

Analog To digital 19ch scanner

  • STM32F0 Discovery Board using a STM32F0xx MCU
  • Default Mecrisp-Stellaris 8Mhz RC system clock
  • No DMA or interrupts used
  • Scans all 19 available channels of the ADC: PA0-7, PB0-1, PC0-5, VREF, VBAT and Temperature sensor in Degrees C
  • It works by scanning each channel and pushing the result on the Stack.
  • The End Of Conversion Flag is checked by a spinner.
  • After the sequence of 19 channel conversions is finished, it pops the results off the stack, scales and displays them along with the number of spins.
  • By Terry Porter

Download: Analog To digital 19ch scanner

Morse code generator

  • STM32F0 Discovery Board using a STM32F0xx MCU
  • Default Mecrisp-Stellaris 8Mhz RC system clock
  • LEDS blink for DASH and DOT’s
  • PA4 drives a piezo loudspeaker to hear the Morse Code
  • By quaak.haak, Terry Porter

Download: Morse code generator