Introduction #
The Propeller native SPIN language includes many efficient instructions to solve common programming tasks.
For example, how to switch a group of IO pins simultaneously with a single instruction ?
Here's an example using just two lines of code (one command for setup, and one command to switch the IO pins):
Code Snippet #
dira[8..15] := %11111111 ' Set IO pins from 8 to 15 as outputs
outa[8..15] := %11001100 ' Set IO pins from 8 to 15 as high or low (1 or 0)
The example snippet shows two commands. They both address a range of IO pins simultaneously, instead of operating on one IO pin at a time.
The values inside the square brackets are the IO pin numbers, and this could be any range from 0 to 31. You can write to as many pins at the same time as you like!
The value after the equals sign is assigned to the IO pins. In this example we are using binary values (denoted by the % character). This makes it simpler to visualise, as each binary digit relates to one of the IO pins in the range. You could also use any other base number scheme, such as plain decimal or hexadecimal.
The dira
command is the IO pin direction command. The binary value of 1 makes the corresponding pin an output, whereas setting 0 would make the corresponding pin an input.
The outa
command is the IO pin state command. The binary value of 1 sets the corresponding pin high (3.3V), whereas setting 0 would make the corresponding pin low (0V).
In the example we are setting all pins from 8 to 15 as outputs, and then setting pins 8,9,12,13 to high state, and pins 10,11,14,15 as low state.
Instead of using the binary format, you could also use decimal or hexadecimal numbers. Such as the following two variations, which would both set all the outputs high:
outa[8...15] := 255
' Using plain decimal value
outa[8...15] := $FF
' Using hexadecimal value
Both of those commands have exactly the same result as using the binary instruction:
outa[8...15] := %11111111
' Using binary value
Binary LED diagnostic display #
One very handy use of this one line pin state capability is to output diagnostic messages to a group of connected LEDs (or even a 7-digit segment LED module).
The following code example assumes you have connected an LED to each of the IO pins from 8 to 15, and demonstrates how to update the value of the 8 LEDs, by counting up to 255 in a loop.
CON { Timing }
_clkmode = xtal1 + pll16x ' Standard clock mode * crystal frequency = 80 MHz
_xinfreq = 5_000_000
PUB main | idx
dira[8..15] := %11111111 ' Set direction of IO pins 8 to 15 as output direction
repeat idx from 0 to 255 ' Loop with value of idx variable counting from 0 to 255
outa[8..15] := idx ' Change the state of the IO pins from 8 to 15 so they match the idx value
waitcnt(clkfreq / 2 + cnt) ' Wait for half a second
Another practical use of this technique is to call the outa
command from any point in your code to output a diagnostic value to help during code development.
Project Idea #
Try building an LED clock, and use this method to update the time display.
You could make a traditional numeric clock, or even a binary or hexadecimal clock display!
Ask at our forums for help with your project, and share your results too!
Resources #
Parallax Propeller 1 - Spin Language Documentation