Introduction #
Each of the multicore Propeller 1's eight processors (cogs) has two independent hardware counter modules. Each counter is a configurable state machine for sensing or generating repetitive signals, potentially on every clock cycle.
The counter modules are flexible subsystems that can often take the place of dedicated cogs or peripheral hardware, reducing code complexity and component count in an application.
This handy tip shows how to use one of the counters in a cog to blink an LED. You can start this code in any (or all!) of the cogs, and attached to different LEDs, you can quickly see if one of your cogs stops operating for any reason.
As the counters run in the background, this code will not take up any resources from the rest of your code, and will not interfere with any critical real-time or high speed code loops in the way that traditional threaded processors might.
Here's an example of running the heartbeat in your main cog. You could add the same subroutine to any of your other cogs and call it with a different LED pin to watch as many cogs as you need.
Code Snippet #
CON
_XINFREQ = 5_000_000
_CLKMODE = XTAL1 + PLL16X
LED_PIN27 = 27
PUB start
Enable_HeartBeat(LED_PIN27)
repeat
' loop forever
' do something useful here!
PRI Enable_HeartBeat(led_pin) ' Runs in background via counter A; toggles led_pin
''' Configure background counter; set mode and APIN (BPIN is ignored)
''' The shorthand code to configure the counter is (for reference):
' ctra := %00100_000 << 23 + 1 << 9 + led_pin
''' The step-by-step way to configure the counter is (as used in this example):
ctra[30..26] := %00100 ' Mode
ctra[25..23] := %000 ' Divider
ctra[14.. 9] := -1 ' Pin B (Set -1 to ignore)
ctra[ 5.. 0] := led_pin
frqa := 27 ' Set FRQA so that PHSA[31] toggles the led pin approximately every half second
' - frqa = maximum value of a 32-bit register,
' divided by current clock speed, divided by 2
'
' = 2^32 / clkfreq / 2
' = 4,294,967,296 / 80,000,000 / 2
' = 26.8435456 (Round up to 27)
'
' Note: The divide by 2 is because the LED will toggle each clock cycle,
' so we want one cycle to be approximately half a second,
' so that the led will toggle on/off in approximately 1 second.
dira[led_pin] := 1 ' Ensure the led pin direction is configured as an output
For more information about using the Propeller counters, including all the modes and configuration options, check out the links in the Resources below.
Leave your comments here, and ask any questions at our community forums: https://forums.parallax.com
Resources #
https://www.parallax.com/package/an001-propeller-p8x32a-counters