Parallax Propeller Book - LED Control

Controlling Outputs

Prerequisites:


Equipment:


In this Chapter we will discuss some simple but very useful Spin programming principles for programming of Ports for I/O (Input / Output) that can be incorporated into many Objects.

In this Chapter we will use Parallax Propeller Quick-Start Board (QSB) for demonstration and will control the on Board 8 LEDs that are wired to Ports P16 to P23.

The 8 LEDs are driven through a buffer (74HC541), so they do not load the I/O Ports.

However, keep in mind that the LEDs are only a "representative" example of any possible device that can be controlled by HI signal from Parallax Propeller I/O Ports either directly, if the device accepts +3.3V DC Input or through either Solid State or Electro-mechanical Relay powered via suitable relay driver.

So you can imagine the LED to represent a high Voltage or high power incandescent lamp, a Motor, or even an Input into another completely independent micro controller.

Simply put, any device that can be controlled with a signal can be for learning, research and program code development purposes represented by the LED.


LEDs on with Direction and Output Register Bits

The 24-1_LedOnP23.spin Object shown below, has a Method named LedOn, with commands that instruct a Cog in the Propeller chip to set its I/O Port P23 to Output High (HI).

This in turn causes the LED in the circuit connected to P23 to emit light.

Load 24-1_LedOnP23.spin into RAM by clicking:
Run → Compile Current → Load RAM (or press F10).


http://www.mirox.us/Parallax/Propeller/Book/Book-Ch24/24-1_LedOnP23.spin


''  24-1_LedOnP23.spin

PUB LedOn               'Public Method declaration
        dira[23] := 1           ' Set P23 to output
        outa[23] := 1           ' Set P23 high  (HI=ON)

        repeat          ' Endless loop prevents program from ending

How LedOnP23.spin Works

The first line in the program is a documentation comment.

Single line documentation comments are denoted by two apostrophes (not a quotation mark) to the left of the documentation text.

In this instaqnce the documentation comment is the file name of the Spin program.

PUB LedOn is a method block declaration with a public (PUB) access rule and the name LedOn.

The dira register is one of several special purpose registers in Cog RAM; you can read and write to the dira register, which stores I/O Port directions for each I/O Port.

A "1" in a given dira register bit sets that I/O Port to Output (OUT);
a "0" sets it to Input (IN).

The symbol “:=” is the assignment operator; the command:
dira[23] := 1
assigns the value 1 to the dira register’s Bit 23, which makes Port P23 an Output (OUT).

When an I/O Port is set to Output, the value of its bit in the outa register either sets the I/O Port high (HI = 3.3 V) with a 1, or low (LO = 0 V) with a 0.

The command:
outa[23] := 1
Sets I/O Port P23 high (HI).

Since the P23 LED circuit terminates at ground, the result is that the LED emits light.

The repeat command is one of the Spin language’s conditional commands.

It can cause a block of commands to execute repeatedly based on various conditions.

For repeat to affect a certain block of commands, they have to be below it and indented further by at least one space or tab.

The next command that is not indented further than repeat is not part of the block, and will be the next command executed after the repeat loop is done.

Since in this case there is nothing else below the repeat command in the 24-1_LedOnP23.spin object, it just repeats itself over and over again.

This command is necessary to prevent the Propeller chip from automatically going into low power mode after it runs out of commands to execute.

If the repeat command was not there, the P23 LED would turn on too briefly to see, and then the chip would go into low power mode. To our eyes it would appear as if nothing has happened.



Spinning Spin

More than one assignment can be made on one line:

• Replace this:

dira[23] := 1
outa[23] := 1

• ... with this:

dira[23] := outa[23] := 1


http://www.mirox.us/Parallax/Propeller/Book/Book-Ch24/24-2_LedOnP23.spin

'' File: 24-2_LedOnP23.spin   

PUB LedOn               ' Public Method declaration

        dira[23] := outa[23] := 1     ' Set P23 to output; Set P23 high  (HI=ON)  

        repeat          ' Endless loop prevents program from ending

Turning LED Off

Of course using a microcontroller just to turn LED ON and leaving it ON indefinitely would be rather wasteful, same thing can be achieved with a piece of wire or any switch.

However, if you need to turn the LED off after some condition occurs, be it input signal or a time delay, then the use of microcontroller for such purpose is ideal since the turn ON and turn OFF conditions can be controlled via software.

There are two ways of achieving this OFF condition:

Object below will flash the LED, the state will change every second using the first option.


''  24-3_LedFlashP23.spin

PUB LedFlash               ' Public Method declaration

        dira[23] := outa[23] := 1     ' Set P23 to output; Set P23 high  (HI=ON)  

        repeat          ' Endless loop prevents program from ending
        
                waitcnt(clkfreq + cnt)  ' wait for one second.
                outa[23] := 0           ' Set P23 low  (LO=OFF)
                waitcnt(clkfreq + cnt)  ' wait for one second.
                outa[23] := 1           ' Set P23  high  (HI=ON)

If the repeat command was not the last command in the Method, the LEDs would turn back off again so quickly that it could not be visually discerned as ON for any amount of time.

Only an oscilloscope or certain external circuits would be able to catch the very brief “ON” state.


I/O Pin Group Operations

The Spin language has provisions for assigning values to groups of bits in the dira and outa registers.

Instead of using a single digit between the brackets next to the outa command, two values separated by two dots can be used to denote a contiguous group of bits.

The binary number indicator % provides a convenient way of defining the bit patterns that get assigned to the group of bits in the outa or dira registers.

For example:
dira[17..22] := %111111
will set bits 17 through 22 in the dira register to Output.

Another example:
outa[17..22] := %101010
sets P17, clears P18, sets P19, and so on.

The result should be that the LEDs connected to P17, P19, and P21 turn on while the others stay off.



'' http://www.mirox.us/Parallax/Propeller/Book/Book-Ch24/24-4_GroupIoSet.spin



'' File: 24-4_GroupIoSet.spin

PUB LedsOn

	dira[17..22] := %111111
	outa[17..22] := %101010

	repeat

Here is another Object using 8 Bit pattern



'' http://www.mirox.us/Parallax/Propeller/Book/Book-Ch24/24-5_GroupIoSet.spin


'' File: 24-5_GroupIoSet.spin

PUB LedsOn                             ' Method declaration 

        dira[16..23] := %11111111        ' Set P16 to P23 to Output 
        outa[16..23] := %10101010        ' Set P16 to P23 to Output Pattern    									'(HI/LO/HI/LO/HI/LO/HI/LO)  

        repeat                         ' Endless loop prevents program from ending
        

Spinning Spin

Notice that outa[16..23] := %10101010 causes the state of the outa register’s Bit 16 to be set (to 1), Bit 17 cleared (to 0), and so on.

If the Port group’s start and end values are swapped, the same bit pattern will cause Bit 23 to be set, Bit 22 to be cleared, and so on…

• Replace this:

outa[16..23] := %10101010

• ... with this:

outa[23..16] := %10101010



'' http://www.mirox.us/Parallax/Propeller/Book/Book-Ch24/24-6_GroupIoSet.spin



'' File: 24-6_GroupIoSet.spin  

PUB LedsOn                             ' Method declaration 

        dira[16..23] := %11111111        ' Set P16 to P23 to Output 
        outa[23..16] := %10101010        ' Set P23 to P16 to Output pattern 										'(HI/LO/HI/LO/HI/LO/HI/LO)  

        repeat                         ' Endless loop prevents program from ending   


It doesn’t matter what value is in an outa register bit if its dira register bit is zero.

That’s because the I/O Port functions as an Input instead of an Output when its dira register bit is cleared (set to Zero).

An I/O Port functioning as an Input detects high and low signals instead of sending them.

While a Port configured to function as an Output, it either transmits 3.3 V (HI) or 0 V (LO).

Port configured to Input doesn’t transmit at all, because it is instead monitoring the voltage that is applied to the Port.

An I/O Port set to Output High (HI) connected to an LED circuit turns the light ON when it applies 3.3 V to the LED circuit. Since the other end of the LED circuit is connected to ground (0 V), the electrical pressure across the LED circuit causes current to flow through the circuit, which turns the light on.

An I/O Port set to Output Low turns the light off because it applies 0 V to the LED circuit. With 0 V at both ends of the circuit, there is no electrical pressure across the circuit, so no current flows through it, and the light stays off.

The LED also stays off when the I/O Port is set to Input, but for a different reason. An I/O Port set to Input doesn’t apply any voltage at all because it is instead sensing voltage applied to it by the circuit. The result is the same, the LED stays off. Since an I/O Port set to input doesn’t apply any voltage to a circuit, it doesn’t matter what value is in the corresponding outa register bit. The LED circuit connected to that Port will remain off.

Here is an example that sets all the bits in outa[16..23] but not all the bits in dira[16..23].

The LEDs connected to P16, P17, P20 and P21 will not turn on because their I/O Ports have been set to Input with zeros in the dira register.


• Set all the outa[16..23] bits.

outa[16..23] := %11111111

• Clear bits 16, 17, 20 and 21 in dira[16..23].

dira[16..23] := %00110011

• Load the modified program into the Propeller chip’s RAM and verify that the 1’s in the outa[16], outa[17], dira[20] and dira[21] bits cannot turn on the P16, P17, P20 and P21 LEDs because their I/O Ports have been set to inputs with zeros in dira[16], dira[17], dira[20] and dira[21].



'' http://www.mirox.us/Parallax/Propeller/Book/Book-Ch24/24-7_GroupIoSet.spin


'' File: 24-7_GroupIoSet.spin  

PUB LedsOn                             ' Public Method declaration 

        dira[16..23] := %00110011        ' Set P16 to P23  Output/Input Pattern 
        outa[16..23] := %11111111        ' Set P16 to P23 to Output Pattern  

        repeat                         ' Endless loop prevents program from ending 


Under Construction
Currently this page is still under development, so please check back periodically for new links to pages as we add them to this list.

Links to related Webpages

Click the link in the list below to navigate to a detailed webpage about the listed subject.


Parallax Propeller Book - Table fo Content