QuickStart 2: Propeller Programming First Steps

Programming your Parallax Propeller is quick and easy.  Start by installing either the Propeller Tool or Brad’s Spin Tool depending on your configuration requirements.   Connect your Propeller to your computer’s USB port.  The P8X32A QuickStart obtains its required power from the USB port as well as establishing the communication connection required by the programming tool.  A quick connection test can be preformed by pressing F7.   A message like the one below should appear, meaning that you are ready to begin programming your Propeller.

Information window confirms Propeller chip is found on a COM port

There are several light emitting diodes (LEDs) on your QuickStart board which provide an excellent starting point for learning how to program your Propeller. 

Type the following program into the large window inside the programming tool.

Abstract: Simple program to light an LED. 
Source code: lesson1.spin

CON

  _clkmode = xtal1 + pll16x
  _xinfreq = 5_000_000

PUB LedOn                           ' Method declaration 
                       
    dira[16] := 1                   ' Set P16 to output

    repeat                          ' Endless loop prevents
                                    ' program from ending.
     
        outa[16] := 1               ' Set P16 high 
        

 

Syntax detection in the programming tool will change the background colors of each program block as you enter code.    Apostrophes are used to make notes or remarks in the code and have no effect on the actual code itself.

The CON (constants) block is used to define constants and settings which will be unchanged over the course of running the program.  This example defines the speed of the Propeller at 80 MHz using these two lines:

  _clkmode = xtal1 + pll16x

  _xinfreq = 5_000_000

With a crystal, the Propeller’s operating speed range is from 4 MHz to 80 MHz depending on the crystal’s value and the speed settings at runtime. The most common configuration is 80 MHz, which is standard with most Parallax products. 

The PUB (public) method called LedOn in the example creates a starting point for our program.  A PUB method can be defined with any name which is not a reserved word in Spin. 

The line dira[16] := 1 configures Propeller I/O pin P16 as output device.  

The repeat command begins a loop in which everything indented below it will be repeated over and over again.   Indention in Spin is important, so the Propeller Tool draws connecting lines showing which commands below will be effected by the command.   You can simply use a few spaces to create the indentation.  There is no set rule to how many spaces are required.

The line outa[16] := 1 configures Propeller I/O pin P16 high, meaning that voltage is turned on, supplying power to light the LED on P16.

In reality, repeating the last line over and over again does not change the state of the output, it simply prevents the program from stopping.

Abstract: Simple program to flash an LED.
Source Code: lesson2.spin

Add the three additional lines to the example code as shown:

CON

  _clkmode = xtal1 + pll16x
  _xinfreq = 5_000_000

PUB LedOnOff                        ' Method declaration 
                       
    dira[16] := 1                   ' Set P16 to output

    repeat                          ' Endless loop prevents
                                    ' program from ending.
     
        outa[16] := 1               ' Set P16 high 
        waitcnt(clkfreq/4 + cnt)    ' Wait a moment
        outa[16] := 0               ' Set P16 low
        waitcnt(clkfreq/4 + cnt)    ' Wait a moment
        

 

The waitcnt command provides a small delay before allowing the loop to continue. 

The line outa[16] := 0 configures Propeller I/O pin P16 low, meaning that voltage in turned off, turning off the connected LED.

The four lines indented below the repeat command are looped over and over again without stopping, causing the LED to flash.

Abstract: Simple program to demonstrate repeat and variable usage.
Source Code: lesson3.spin

CON

  _clkmode = xtal1 + pll16x
  _xinfreq = 5_000_000
  
VAR

 byte LED
  
PUB LedsOn                  ' Method declaration 
                       
    dira[16] := 1           ' Set P16 to output
    dira[17] := 1           ' Set P17 to output
    dira[18] := 1           ' Set P18 to output
    dira[19] := 1           ' Set P19 to output
    dira[20] := 1           ' Set P20 to output
    dira[21] := 1           ' Set P21 to output
    dira[22] := 1           ' Set P22 to output
    dira[23] := 1           ' Set P23 to output
    
    repeat LED from 16 to 23      'Repeat 8 times, counting forward     
     
        outa[LED] := 1            'Set LED# on
        waitcnt(clkfreq/8 + cnt)  'Wait a moment
                

 

This lesson expands on the versatility of the repeat command as well as introducing variables.  The VAR (variable) block is used to define variables which can be accessed or changed from anywhere in the Spin program.  Think of line “byte LED” as a byte-sized, empty box with the word “LED” written on its side. 

A byte can contain a value that represents a single decimal, binary, or hexadecimal number, or even a single character.

All of the QuickStart’s LED I/O pins are configured as outputs with the first eight lines of the PUB LedsOn method.

The line repeat LED from 16 to 23 begins a repeat loop, but not an infinite one as before.  This time the loop will repeat 8 times, incrementing the LED variable at each pass. Each time the LED variable is updated with the new number.

As before, outa will light the LED, but each time the loop repeats, the index number is changed.  Waitcnt provides a small pause, allowing us to see the action.

Abstract: Simplified code using dira selecting multiple I/O.
Source: lesson4.spin

CON

  _clkmode = xtal1 + pll16x
  _xinfreq = 5_000_000
  
VAR

 byte LED
  
PUB LedsOnSimplified             ' Method declaration 
                       
    dira[16..23] := %11111111    ' Set P16 thru P23 to output 
    
    repeat LED from 16 to 23     ' Repeat 8 times, counting forward     
     
        outa[LED] := 1           ' Set LED# on
        waitcnt(clkfreq/8 + cnt) ' Wait a moment
                

 

This shortened version of the program demonstrates the ability of DIRA to include multiple pins.  The program executes as before with fewer lines.  Note the use of the % sign, to indicate a binary value, in the DIRA statement.

Abstract: Display variable in binary using LEDs.
Source Code: lesson5.spin

CON

  _clkmode = xtal1 + pll16x
  _xinfreq = 5_000_000
  
VAR

 byte LED
  
PUB LedCounter                   ' Method declaration 
                       
    dira[16..23] := %1111_1111   ' Set P16 thru P23 to output
    
    repeat LED from 0 to 255     ' Repeat 256 times, counting forward       
     
        outa[16..23] := LED      ' Set display number in binary on LEDs   
        waitcnt(clkfreq/8 + cnt) ' Wait a moment
        

 

An adjustment was made to the DIRA statement, using an underscore.  The use of an underscore makes it easier for us to see how many 1’s are defined without affecting the code itself. 

The repeat command is again used as a counter, incrementing the variable LED from 0 to 255.

Multiple I/O pins can be specified with the OUTA statement just as they can with DIRA.  The line outa[16..23] := LED lights the LEDs as a binary number each time the repeat command increments the number stored in the LED variable.