SPIN CODE EXAMPLES FOR THE BEGINNER (Public Version) - Page 5 — Parallax Forums

# SPIN CODE EXAMPLES FOR THE BEGINNER (Public Version)

• Posts: 160
edited 2006-04-25 17:52

Example 14

PROPELLER FREQUENCY COUNTER: External Frequency Source
```'                                       EXAMPLE 14

'               PROPELLER FREQUENCY COUNTER: External Frequency Source
'**********************************************************************************
'IMPORTANT: This example may require an understanding of Example: 13
'**********************************************************************************
'WHAT'S NEW IN THIS EXAMPLE:
'          SPECIAL PURPOSE PROPELLER REGISTERS: CTRA, FRQA, AND PHSA.  Using these
'          registers, an incoming frequency source is sampled for one second. The
'          result is then displayed on a Parallax LCD.
'**********************************************************************************
'DIFFICULTY LEVEL: Intermediate
'**********************************************************************************
'PURPOSE: The purpose of this example is to show how to create a frequency counter
'          using the Propeller's special purpose registers and display a frequency
'          count in Hz on an LCD.
'Submitted by Dave Scanlan, April 25, 2006
'File: Example14_FreqCtrLCD_ExternalSource.spin
'**********************************************************************************
'CORRECT OUTPUT: After sampling an incoming digital wave, the result will be
'                displayed as below:
'                                      4x20 LCD Module
'                               &#9474;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#9474;
'                               &#9474;         PROPELLER        &#9474;
'                               &#9474;     FREQUENCY COUNTER    &#9474;
'                               &#9474;                          &#9474;
'                               &#9474;     FREQ:XXXXXXXXXXXHz   &#9474;
'                               &#9474;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#9474;
'**********************************************************************************
CON
_clkmode      = xtal1 + pll16x
_xinfreq      = 5_000_000
LCD_Pin   = 0                           'The LCD is connected to pin A0.
LCD_Baud  = 19_200                      'Baud
LCD_Lines = 4                           'The number of lines on this LCD
In = %0
On = 1
Off = 0
'
VAR
Long Frequency
'
OBJ
LCD: "debug_lcd"                          'Creates the object LCD from "debug_lcd"
'
PUB Start
Repeat
MeasureFrequency
DisplayFrequency
'
PUB MeasureFrequency | Pin
Pin := 5                                  'Pulses are sampled on this pin.
DirA[noparse][[/noparse]Pin] := In
CTRA := 0                                 'Clear settings
CTRA := (%01010 << 26 ) | (%001 << 23) | (0 << 9) | (Pin) 'Trigger to count rising
'                                                            edge on Pin A5
FRQA := 1000                              'Count 1000 pulses on each trigger
PHSA := 0                                 'Clear accumulated value
WaitCNT( 80_000_000 + CNT )               'Wait for 1 second
Frequency := PHSA / 1000                  'Calculate Freq based on the sampling
'                                            duration, 1000 ms (1 second)
PUB DisplayFrequency
' If LCD.Start(0,       19_200,   4        )
If LCD.Start(LCD_Pin, LCD_Baud, LCD_Lines) 'Initialize the LCD object
LCD.Cursor(Off)                          'Set cursor off
LCD.Backlight(On)                        'Set backlight on
LCD.Cls                                  'Clear the LCD screen
LCD.Str(string("      PROPELLER"))       'Display string on LCD
LCD.Newline
LCD.Str(string("  FREQUENCY COUNTER"))   'Display string on LCD
LCD.Gotoxy(2,3)                          'Gotoxy(Col,Row)
LCD.Str(string("FREQ:"))                 'Display string on LCD
LCD.Gotoxy(7,3)
LCD.DecF(Frequency, 8)                   'Display Freq. as a decimal.
LCD.Gotoxy(15,3)
LCD.Str(string("Hz"))                    'Display string on LCD
'**********************************************************************************
'  --  The code for sampling an incoming wave is an adaptation from
'      Dr. Martin Hebel's original code in the object, BS2.Function.spin.
'  --  The results of this counter agree closely with the readings from an HP 5314A
'      frequency counter with deviations of 0 to 6 Hz at 2MHz.
'  --  For this example, the amplitude was 3 volts. Until, the max input voltage,
'      has been "firmly" established for the Propeller, I would proceed with
'      caution.  Remember, this processor is a 3.3VDC device.
'  --  For analog signals, a pre-conditioning Schmitt trigger is suggested.
'      Resource: [url=http://en.wikipedia.org/wiki/Schmitt_trigger]http://en.wikipedia.org/wiki/Schmitt_trigger[/url]
'FORMAT FOR: LCD.DecF(Frequency, 8)
' LCD.DecF(Value, Field_Width)
'  Prints signed decimal value in fixed-width field
```

```'                      FEWER COMMENTS (EASIER TO READ)
'
'                                EXAMPLE 14
'
'               PROPELLER FREQUENCY COUNTER: External Frequency Source
'**********************************************************************************
'IMPORTANT: This example may require an understanding of Example: 13
'**********************************************************************************
'DIFFICULTY LEVEL: Intermediate
'**********************************************************************************
'CORRECT OUTPUT: After sampling an incoming digital wave, the result will be
'                displayed as below:
'                                      4x20 LCD Module
'                               &#9474;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#9474;
'                               &#9474;         PROPELLER        &#9474;
'                               &#9474;     FREQUENCY COUNTER    &#9474;
'                               &#9474;                          &#9474;
'                               &#9474;     FREQ:XXXXXXXXXXXHz   &#9474;
'                               &#9474;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#9474;
'Submitted by Dave Scanlan, April 25, 2006
'File: Example14_FreqCtrLCD_ExternalSource.spin
'**********************************************************************************
CON
_clkmode      = xtal1 + pll16x
_xinfreq      = 5_000_000
LCD_Pin   = 0
LCD_Baud  = 19_200
LCD_Lines = 4
In = %0
On = 1
Off = 0
'
VAR
Long Frequency
'
OBJ
LCD: "debug_lcd"
'
PUB Start
Repeat
MeasureFrequency
DisplayFrequency
'
PUB MeasureFrequency | Pin
Pin := 5
DirA[noparse][[/noparse]Pin] := In
CTRA := 0
CTRA := (%01010 << 26 ) | (%001 << 23) | (0 << 9) | (Pin)
FRQA := 1000
PHSA := 0
WaitCNT( 80_000_000 + CNT )
Frequency := PHSA / 1000
'
PUB DisplayFrequency
If LCD.Start(LCD_Pin, LCD_Baud, LCD_Lines)
LCD.Cursor(Off)
LCD.Backlight(On)
LCD.Cls
LCD.Str(string("      PROPELLER"))
LCD.Newline
LCD.Str(string("  FREQUENCY COUNTER"))
LCD.Gotoxy(2,3)
LCD.Str(string("FREQ:"))
LCD.Gotoxy(7,3)
LCD.DecF(Frequency, 8)
LCD.Gotoxy(15,3)
LCD.Str(string("Hz"))
'**********************************************************************************
'  --  The code for sampling an incoming wave is an adaptation from
'      Dr. Martin Hebel's original code in the object, BS2.Function.spin.

```

Post Edited (Dave Scanlan) : 4/25/2006 6:37:57 PM GMT
• Posts: 375
edited 2006-04-25 18:23
Jon:

What about a example in Assembly to run at 115200?
• Posts: 160
edited 2006-04-25 18:45
El Paisa said...

Jon:

What about a example in Assembly to run at 115200?

El Paisa, it seems that the demand for assembly is rising.· How about starting a thread for it? ·This thread is for Spin code.
Dave
• Posts: 6,491
edited 2006-04-25 18:49
As Dave points out, he started this thread to focus on Spin code -- and I have previously posted examples that use the FullDuplex.spin object which is written in assembly (by Chip) and can to buffered rx/tx over 400K.
El Paisa said...

Jon:

What about a example in Assembly to run at 115200?

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
• Posts: 375
edited 2006-04-25 18:55
Dave:

I am all for it.

Two file folders one for assembly and the other for spin.

That will be great.
• Posts: 160
edited 2006-04-25 19:04
El Paisa,

I assume you will start the thread, and others will add to it.· I don't want to start it, because there are only so many hours in each day.· Go for it...·Let's try to focus on Spin in this·thread.

Dave

El Paisa said...

Dave:

I am all for it.

Two file folders one for assembly and the other for spin.

That will be great.

• Parallax Engineering Posts: 14,406
edited 2006-04-26 01:38
Dave,

·· Once again, very nice.· Good examples!

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com
• Posts: 160
edited 2006-04-26 02:09
Chris Savage (Parallax) said...

Dave,

·· Once again, very nice.· Good examples!

Thanks, Chris...more to come.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
··
• Posts: 160
edited 2006-04-26 17:15

·(This frequency reading is exactly the same reading using an HP 5314A)

·····························································
EXAMPLE 15

·····TWO-PROCESSOR FREQUENCY COUNTER: Internal Frequency Source····
```'                                       EXAMPLE 15
'
'         TWO-PROCESSOR FREQUENCY COUNTER: Internal Frequency Source
```

```
'*****************************************************************************
'IMPORTANT: This example may require an understanding of examples 5, 6, 13, 14.
'*****************************************************************************
'WHAT'S NEW IN THIS EXAMPLE:
'
'  INTERNAL FREQUENCY GENERATOR: Generates digital pulses at a rate of
'                                of 27,933Hz.  One cog (procssor) is
'                                used for this; that is, this Cog has
'                                no other function but to generate pulses.
'*****************************************************************************
'DIFFICULTY LEVEL: Intermediate
'*****************************************************************************
'PURPOSE: This example shows how to use two Cogs (processors). One processor
'         generates digital pulses at a frequency of 27,933Hz and the second
'         processor measures the pulses coming from the first processor and
'         displays the frequency of these pulses on a Parallax serial LCD.
'
'         Example 14 vs. Example 15
'         -- Example 14 used one processor; and the frequency source was from
'            an EXTERNAL source, such as a function generator.
'         -- Example 15 uses two processors, and the frequency source is from
'            an INTERNAL source; that is, from one of the two Cogs (processors).
'         Pin connections: Besides connecting the serial LCD, you must
'                          connect pin A6(Pulse output) to pin A5(Frequency
'                          counter input). Also, see Example 13 for the serial
'                          LCD connections to the Propeller.
'
'Submitted by Dave Scanlan, April 26, 2006
'File: Example15_FreqCtrLCD_InternalSource.spin
'*****************************************************************************
'CORRECT OUTPUT: After sampling the digital wave coming from one of the Cogs,
'                the result will be displayed as below:
'                                      4x20 LCD Module
'                               &#9474;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#9474;
'                               &#9474;         PROPELLER        &#9474;
'                               &#9474;     FREQUENCY COUNTER    &#9474;
'                               &#9474;                          &#9474;
'                               &#9474;     FREQ:   27933Hz      &#9474;
'                               &#9474;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#9474;
'*****************************************************************************
CON
_clkmode  = xtal1 + pll16x
_xinfreq  = 5_000_000
LCD_Pin   = 0                        'The LCD is connected to pin A0.
LCD_Baud  = 19_200                   'Baud
LCD_Lines = 4                        'The number of lines on this LCD
Out = %1
In = %0
On = 1
Off = 0
High = 1
Low = 0
'
VAR
Long Stack1[noparse][[/noparse]30]                        'Sets up a stack space for a one Cog
Long Stack2[noparse][[/noparse]40]                        'Sets up a stack space for a second Cog
'
OBJ
LCD: "debug_lcd"                       'Creates the object LCD. The file
'                                        "bebug_lcd" can be found on the Parallax
'                                         [url=http://www.parallax.com/propeller/object.asp]http://www.parallax.com/propeller/object.asp[/url]
PUB Start
CogNew(FrequencyOut_PinA6, @Stack1)
Cognew(MeasureFrequency_PinA5, @Stack2)
'
PRI MeasureFrequency_PinA5 | Pin, Frequency
' If LCD.Start(0,       19_200,   4        )
If LCD.Start(LCD_Pin, LCD_Baud, LCD_Lines) 'Initialize the LCD object
LCD.Cursor(Off)                          'Set cursor off
LCD.Backlight(On)                        'Set backlight on
LCD.Cls                                  'Clear the LCD screen
LCD.Str(string("      PROPELLER"))       'Displays string on LCD
LCD.Newline
LCD.Str(string("  FREQUENCY COUNTER"))   'Displays string on LCD
LCD.Gotoxy(2,3)                          'Gotoxy(Col,Row)
LCD.Str(string("FREQ:"))                 'Displays string on LCD
Repeat
Pin := 5
DirA[noparse][[/noparse]Pin] := %0
```

```    CTRA := 0                                'Clear CTRA settings
CTRA := (%01010 << 26 ) | (%001 << 23) | (0 << 9) | (Pin) 'Trigger to count
'                                                              rising edge on A5
FRQA := 1000                              'Count 1000 each trigger
PHSA := 0                                 'Clear accumulated value
WaitCNT( 80_000_000 + CNT )               'Wait for 1 second
Frequency := PHSA / 1000                  'Calculate Freq based on duration,
'                                              1000 ms (1 second)
LCD.Gotoxy(7,3)
LCD.DecF(Frequency, 8)                    'Displays freq in an 8 char field.
LCD.Gotoxy(15,3)                          'Start on the next line
LCD.Str(string("Hz"))                     'Displays string on LCD
```

```'Generates a frequency of 27,933Hz. This seems to be the
' "top" speed for Spin, not so for assembly.
'Try other settings: WaitCNT(4_000_000 + CNT)is 10Hz; WaitCNT(400_000+ CNT) is 100Hz.
PRI FrequencyOut_PinA6 | Pin
Pin := 6                                   'Output pin for 27,933Hz signal.
DirA[noparse][[/noparse]Pin] := Out
Repeat
OutA[noparse][[/noparse]Pin] := High
WaitCNT(400 + CNT)
OutA[noparse][[/noparse]Pin] := Low
WaitCNT(400 + CNT)
'**********************************************************************************
'  --  The code for sampling an incoming wave is an adaptation from
'      Dr. Martin Hebel's original code in the object BS2.Function.spin.
'  --  The results of this example agree perfectly with the readings from an HP 5314A
'      frequency counter at a frequency of 27,933Hz.
'
'FORMAT FOR: LCD.DecF(Frequency, 8)
' LCD.DecF(Value, Field_Width)
'  Prints signed decimal value in fixed-width field
'
'PIN CONNECTIONS:
' (Input)PIN  A5&#8226;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9532;
' (Output)PIN A6&#8226;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9532; Jumper wire from Pin A5 to Pin A6
' See Example 13 for LCD-to-Propeller pin connections.
'
'MEMORY USAGE:
'  Program: 324 Longs
'  Variables: 89 Longs
'  Stack/Free: 7,775 Longs
```

```'                        FEWER COMMENTS (EASIER TO READ)
'
'                                   EXAMPLE 15
'
'         TWO-PROCESSOR FREQUENCY COUNTER: Internal Frequency Source
'*****************************************************************************
'IMPORTANT: This example may require an understanding of examples 5, 6, 13, 14.
'*****************************************************************************
'DIFFICULTY LEVEL: Intermediate
'*****************************************************************************
'CORRECT OUTPUT: After sampling the digital wave coming from one of the Cogs,
'                the result will be displayed as below:
'                                      4x20 LCD Module
'                               &#9474;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#9474;
'                               &#9474;         PROPELLER        &#9474;
'                               &#9474;     FREQUENCY COUNTER    &#9474;
'                               &#9474;                          &#9474;
'                               &#9474;     FREQ:   27933Hz      &#9474;
'                               &#9474;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#9474;
'Submitted by Dave Scanlan, April 26, 2006
'File: Example15_FreqCtrLCD_InternalSource.spin
'*****************************************************************************
CON
_clkmode  = xtal1 + pll16x
_xinfreq  = 5_000_000
LCD_Pin   = 0
LCD_Baud  = 19_200
LCD_Lines = 4
Out = %1
In = %0
On = 1
Off = 0
High = 1
Low = 0
'
VAR
Long Stack1[noparse][[/noparse]30]
Long Stack2[noparse][[/noparse]40]
'
OBJ
LCD: "debug_lcd"
'
PUB Start
CogNew(FrequencyOut_PinA6, @Stack1)
Cognew(MeasureFrequency_PinA5, @Stack2)
'
PRI MeasureFrequency_PinA5 | Pin, Frequency
' If LCD.Start(0,       19_200,   4        )
If LCD.Start(LCD_Pin, LCD_Baud, LCD_Lines)
LCD.Cursor(Off)
LCD.Backlight(On)
LCD.Cls
LCD.Str(string("      PROPELLER"))
LCD.Newline
LCD.Str(string("  FREQUENCY COUNTER"))
LCD.Gotoxy(2,3)
LCD.Str(string("FREQ:"))
Repeat
Pin := 5
DirA[noparse][[/noparse]Pin] := %0
CTRA := 0
CTRA := (%01010 << 26 ) | (%001 << 23) | (0 << 9) | (Pin)
FRQA := 1000
PHSA := 0
WaitCNT( 80_000_000 + CNT )
Frequency := PHSA / 1000
LCD.Gotoxy(7,3)
LCD.DecF(Frequency, 8)
LCD.Gotoxy(15,3)
LCD.Str(string("Hz"))
'
'Try other settings: WaitCNT(4_000_000 + CNT)is 10Hz; WaitCNT(400_000+ CNT) is 100Hz.
PRI FrequencyOut_PinA6 | Pin
Pin := 6
DirA[noparse][[/noparse]Pin] := Out
Repeat
OutA[noparse][[/noparse]Pin] := High
WaitCNT(400 + CNT)
OutA[noparse][[/noparse]Pin] := Low
WaitCNT(400 + CNT)
'**********************************************************************************
'  --  The code for sampling an incoming wave is an adaptation from
'      Dr. Martin Hebel's original code in the object BS2.Function.spin.
```

Post Edited (Dave Scanlan) : 4/27/2006 1:39:14 AM GMT
• Posts: 147
edited 2006-04-26 18:49
Dave,

·· Great stuff.· Here is a PDF with all your examples to date, in one place.

Charlie

PDF - Updated·16 June 2006

Post Edited (dy8coke) : 6/16/2006 5:39:50 PM GMT
• Posts: 160
edited 2006-04-26 19:00
Charlie,

Thank you, very much! It is hard to believe that there are 34 PDF pages of these examples.
Many Propeller users will appreciate these pages.

Thanks again,

Dave

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
··

Post Edited (Dave Scanlan) : 4/26/2006 7:42:32 PM GMT
• Posts: 160
edited 2006-04-27 13:59
dy8coke said...
Dave,

·· Great stuff.· Here is a PDF with all your examples to date, in one place.

Charlie
Hi Charlie,
If it is ok with you, I would like to place access to·your PDF file on the very first part of this thread.· Also, on that first·part, I will·post a description of the·all the authors'·examples and their locations.
Thanks again for your PDF file,
Dave

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
··

Post Edited (Dave Scanlan) : 4/27/2006 2:38:40 PM GMT
• Posts: 147
edited 2006-04-27 15:18
Dave,

Go ahead. I will edit the PDF to include a TOC if you like, and will update it with each new example.

Charlie
• Posts: 160
edited 2006-04-27 15:31
Sounds good to me.· Thanks!

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
··
• Posts: 375
edited 2006-04-27 15:33
I tried to separate the files and cut & paste to other program (NOTEPAD).

The formating is completely screwed-up.

I only get two long,long, long, long ·lines of text.

The indentations are gone.

I am using version 7.0 of Acrobat Reader.
• Posts: 375
edited 2006-04-27 15:42
Does SPIN differentiates between low caps and up caps?
• Posts: 160
edited 2006-04-27 15:48
El Paisa said...
I tried to separate the files and cut & paste to other program (NOTEPAD).

The formating is completely screwed-up.

I only get two long,long, long, long ·lines of text.

The indentations are gone.

I am using version 7.0 of Acrobat Reader.
Hi El Paisa,
·····
····· When you get the Propeller editor, you will be able to cut and paste with no problems.· It is
······always best to download the files from the examples.· That way the formatting will work
····· just fine in the Propeller editor when it is released.

····· them·from the screen-shots?· It could be that the screen-shots are causing the problem.

······Now, off to my job that pays the bills.

······Dave

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
··

Post Edited (Dave Scanlan) : 4/27/2006 5:22:11 PM GMT
• Posts: 160
edited 2006-04-27 17:21
Hi Paisa,

No, Spin does not care if you use upper or lower case letters.

Dave
El Paisa said...
Does SPIN differentiates between low caps and up caps?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
··
• Posts: 160
edited 2006-04-28 13:48
dy8coke said...
Dave,

Go ahead. I will edit the PDF to include a TOC if you like, and will update it with each new example.

Charlie
Hi Charlie,

Thanks again,

Dave

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
··
• Posts: 160
edited 2006-04-29 10:29

EXAMPLE 16

·ARRAYS: Using an array and displaying array values on a serial LCD
```'
'                                     EXAMPLE 16
'
'    ARRAYS: Using an array and displaying array values on an LCD
'
'**********************************************************************************
'IMPORTANT: This example may require an understanding of example 6
'**********************************************************************************
'WHAT'S NEW IN THIS EXAMPLE:
'
'   Array
'     - How to declare an array: ArrayName[noparse][[/noparse]NumberOfElements]
'     - Example: Array[noparse][[/noparse]10] has 10 elements:  Array[noparse][[/noparse]0] thru Array[noparse][[/noparse]9]
'
'**********************************************************************************
'DIFFICULTY LEVEL: Easy
'
'PURPOSE: The purpose of this example is to show how to declare an array,
'         place values into an array, access values from an array, and
'         display these value on an LCD.
'
'Submitted by Dave Scanlan,  April 29, 2006
'File: Example16_ArrayLCD.spin
'**********************************************************************************
'CORRECT OUTPUT: The LCD will display the result below:
'
'                                     4x20 LCD Module
'                             &#9474;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#9474;
'                             &#9474;  ARRAY VALUES:           &#9474;
'                             &#9474;  0123456789              &#9474;
'                             &#9474;  LARGEST VALUE:9         &#9474;
'                             &#9474;                          &#9474;
'                             &#9474;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#9474;
'
'**********************************************************************************
CON
_clkmode      = xtal1 + pll16x
_xinfreq      = 5_000_000
```

```  LCD_Pin   = 0                        'The LCD is connected to pin A0.
LCD_Baud  = 19_200                   'Baud
LCD_Lines = 4                        'The number of lines on this LCD
On = 1
Off = 0
'
VAR
Byte Array[noparse][[/noparse]10]                        'All 10 elements are global
Byte Largest                          'Largest is global
'
OBJ
LCD: "Debug_LCD"                      'Create the LCD object
'                                        Be sure this program can find Debug_LCD.spin
PUB Start
FillArray
FindLargestValue
DisplayValuesOnLCD
'
PRI FillArray | I, J                    ' "I" and "J" are local variables
J := 0
Repeat  I From 0 To 9                 'Fill array positions from 0 thru 9
Array[noparse][[/noparse]I] := J
J := J + 1
'
PRI FindLargestValue | I                ' "I" is a local variable
Largest := Array[noparse][[/noparse]0]                   'Start the compares with the first element
Repeat I From 1 to 9                  'Find the largest value in an array
If Array[noparse][[/noparse]I] > Largest
Largest := Array[noparse][[/noparse]I]
'
PRI DisplayValuesOnLCD | I
' If LCD.Start(0,       19_200,   4        )
If LCD.Start(LCD_Pin, LCD_Baud, LCD_Lines) 'Initialize LCD
LCD.Cursor(Off)
LCD.Backlight(On)
LCD.Cls
LCD.Str(string("ARRAY VALUES:"))    'Display string on LCD
LCD.NewLine
'
Repeat I From 0 To 9
LCD.DecF(Array[noparse][[/noparse]I],1)              'Display all array values
'                                        (Array[noparse][[/noparse]I],1): The "1" is the field width.
LCD.NewLine
LCD.Str(string("LARGEST VALUE:"))
LCD.Gotoxy(14,2)                    'LCD.Gotoxy(Col,Row)
LCD.DecF(Largest,1)                 'Display the Largest value
'                                        (Largest,1): The "1" is the field width.
'**********************************************************************************
'  --  With some languages, a declaration of Array[noparse][[/noparse]10] produces 11 locations:
'      Array[noparse][[/noparse]0],[noparse][[/noparse]1],[noparse][[/noparse]2],[noparse][[/noparse]3],[noparse][[/noparse]4],[noparse][[/noparse]5],[noparse][[/noparse]6],[noparse][[/noparse]7],[noparse][[/noparse]8],9],[noparse][[/noparse]10]
'  --  With Spin, a declaration of Array[noparse][[/noparse]10] produces exactly 10 locations:
'      A[noparse][[/noparse]0],A[noparse][[/noparse]1],A[noparse][[/noparse]2],A[noparse][[/noparse]3],A[noparse][[/noparse]4],A[noparse][[/noparse]5],A[noparse][[/noparse]6],A7],A[noparse][[/noparse]8],A[noparse][[/noparse]9].
'  --  MEMORY USAGE
'        Program: 308 Longs
'        Variables: 22 Longs
'        Stack/Free: 7,858 Longs
```

```'                          FEWER COMMENTS (EASIER TO READ)
'
'                                     EXAMPLE 16
'
'       ARRAYS: Using an array and displaying array values on a serial LCD
'
'**********************************************************************************
'IMPORTANT: This example may require an understanding of example 6
'**********************************************************************************
'DIFFICULTY LEVEL: Easy
'**********************************************************************************
'CORRECT OUTPUT: The LCD will display the results below:
'
'                                       4x20 LCD Module
'                               &#9474;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#9474;
'                               &#9474;  ARRAY VALUES:           &#9474;
'                               &#9474;  0123456789              &#9474;
'                               &#9474;  LARGEST VALUE:9         &#9474;
'                               &#9474;                          &#9474;
'                               &#9474;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#9474;
'Submitted by Dave Scanlan, April 29, 2006
'File: Example16_ArrayLCD.spin
'**********************************************************************************
CON
_clkmode      = xtal1 + pll16x
_xinfreq      = 5_000_000
LCD_Pin   = 0
LCD_Baud  = 19_200
LCD_Lines = 4
On = 1
Off = 0
'
VAR
Byte Array[noparse][[/noparse]10]
Byte Largest
'
OBJ
LCD: "Debug_LCD"
'
PUB Start
FillArray
FindLargestValue
DisplayValuesOnLCD
'
PRI FillArray | I, J
J := 0
Repeat  I From 0 To 9
Array[noparse][[/noparse]I] := J
J := J + 1
'
PRI FindLargestValue | I
Largest := Array[noparse][[/noparse]0]
Repeat I From 1 to 9
If Array[noparse][[/noparse]I] > Largest
Largest := Array[noparse][[/noparse]I]
'
PRI DisplayValuesOnLCD | I
' If LCD.Start(0,       19_200,   4        )
If LCD.Start(LCD_Pin, LCD_Baud, LCD_Lines) 'Initialize LCD
LCD.Cursor(Off)
LCD.Backlight(On)
LCD.Cls
'
LCD.Str(string("ARRAY VALUES:"))
LCD.NewLine
'
Repeat I From 0 To 9
LCD.DecF(Array[noparse][[/noparse]I],1)
'
LCD.NewLine
LCD.Str(string("LARGEST VALUE:"))
LCD.Gotoxy(14,2)
LCD.DecF(Largest,1)
'**********************************************************************************
'  --  With Spin, a declaration of Array[noparse][[/noparse]10] produces exactly 10 locations:
'      A[noparse][[/noparse]0],A[noparse][[/noparse]1],A[noparse][[/noparse]2],A[noparse][[/noparse]3],A[noparse][[/noparse]4],A[noparse][[/noparse]5],A[noparse][[/noparse]6],A7],A[noparse][[/noparse]8],A[noparse][[/noparse]9].
'  --  MEMORY USAGE
'        Program: 308 Longs
'        Variables: 22 Longs
'        Stack/Free: 7,858 Longs
```

Post Edited (Dave Scanlan) : 4/29/2006 11:21:27 AM GMT
• Posts: 160
edited 2006-04-29 16:31
dy8coke said...
Dave,

·· Great stuff.· Here is a PDF with all your examples to date, in one place.

Charlie
Hi Charlie,

I placed your TOC document-file on the very first part of page 1.· I tried to copy the document into that page, but the formatting would not hold.· Thus, it is there in the form of a PDF attachment.

Today I added another example: Example 16.· It is about array processing.

Thanks again,

Dave

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
··

Post Edited (Dave Scanlan) : 4/29/2006 4:34:12 PM GMT
• Posts: 1,115
edited 2006-04-29 22:39
In Example 16, in the FillArray procdure why not use Array[noparse][[/noparse]I] = I instead of using another variable J?
• Posts: 160
edited 2006-04-30 21:17
Hi John,

Initially I was going to put values in the array in reverse order (9 to 0) from "I" because I was planning to sort them back into order (0 to·9).··Then, I changed my mind, and did a simple·search for the highest value in the array.· Yes, you are correct; I could have·used just·"I" for·the index and the array value.

Thanks,

Dave
John Abshier said...
In Example 16, in the FillArray procdure why not use Array[noparse][[/noparse]I] = I instead of using another variable J?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
··

Post Edited (Dave Scanlan) : 4/30/2006 9:50:31 PM GMT
• Posts: 160
edited 2006-05-02 03:13
dy8coke said...
Dave,

·· Great stuff.· Here is a PDF with all your examples to date, in one place.

Charlie

PDF - Updated 1 May 2006
Hi Charlie,
I have placed your latest Spin Code PDF file of all examples at the very first part of page 1 on this thread.
The latest version has contributions from all authors: Tracy, Jon, Beau, and Dave.

NOW THAT OTHERS HAVE THE PROPELLER DOCUMENTATION AND THE CHIP, I HOPE THESE USERS WILL POST HERE ALSO.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
··
• Posts: 160
edited 2006-05-03 17:49

EXAMPLE 17

····ONE WAY TO PASS A VALUE FROM ONE COG TO ANOTHER COG
··AND INSUFFICIENT STACK SPACE

```
'                                     EXAMPLE 17
'
'                    ONE WAY TO PASS A VALUE FROM ONE COG TO ANOTHER COG
'                              AND INSUFFICIENT STACK SPACE

'*****************************************************************************
'IMPORTANT: This example may require an understanding of examples 6 and 14
'*****************************************************************************
'WHAT'S NEW IN THIS EXAMPLE:
'
'   PASS A VALUE BETWEEN COGS
'     - In this example, the value of the variable X is passed from one Cog
'       to another Cog.
'   THE IMPORTANCE OF SUFFICIENT STACK SPACE FOR A COG.
'     - Without sufficient stack space for a Cog, the Cog will not function
'       correctly.

'*****************************************************************************
'DIFFICULTY LEVEL: Easy
'*****************************************************************************
'
'PURPOSE: The purpose of this example is to illustrate a procedure for passing
'         a value from one Cog to another Cog.  In this example the value of
'         variable X is passed from Cog0 to Cog1. These two Cogs will
'         The location is X and it is a global variable.
'         In the code below you will reduce the stack space of a Cog by
'         10 longs and observe the results: The program will not run correctly.
'Submitted by Dave Scanlan,  May 3, 2006
'File: Example17_PassDataBetweenCogs.spin
'*****************************************************************************
'CORRECT OUTPUT: The LCD will display the result below:
'
'                                     4x20 LCD Module
'                             &#9474;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#9474;
'                             &#9474; PASS X BETWEEN GOGS:     &#9474;
'                             &#9474; X = 10                   &#9474;
'                             &#9474;                          &#9474;
'                             &#9474;                          &#9474;
'                             &#9474;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#9474;

'*****************************************************************************
CON
_clkmode      = xtal1 + pll16x
_xinfreq      = 5_000_000
LCD_Pin   = 0
LCD_Baud  = 19_200
LCD_Lines = 4
Off = 0
On = 1

VAR
Long Stack0[noparse][[/noparse]20]
Long Stack1[noparse][[/noparse]30]
'  Long Stack1[noparse][[/noparse]20]                               If you use only 20 longs the
'                                                program will not run correctly.
'                                                CLEAR THE LCD, THEN TRY
'                                                Stack1[noparse][[/noparse]20].
Long X                                        'Location to be shared between
'                                                 two cogs.
OBJ
LCD: "Debug_LCD"
PUB Start
CogNew(SendsValue, @Stack0)
CogNew(RecievesValueAndDisplaysIt, @Stack1)

PRI SendsValue                                   'Passes a value
X := 10
PRI RecievesValueAndDisplaysIt                   'Recieves a value and displays
'                                                 it on an LCD panel.
If LCD.Start(0,       19_200,   4        )
If LCD.Start(LCD_Pin, LCD_Baud, LCD_Lines)     'Initialize LCD
LCD.Cursor(Off)
LCD.Backlight(On)
LCD.Cls
LCD.Str(string("PASS X BETWEEN COGS:"))
LCD.Gotoxy(0,1)                         'LCD.Gotoxy(FirstCol,SecondRow)
LCD.Str(string("X ="))
LCD.Gotoxy(4,1)                         'LCD.Gotoxy(FifthCol,SecondRow)
LCD.DecF(X,2)                           'Displays value: X

'  -- Note that the locations on an LCD start with 0; that is, HOME is 0,0.
'  -- Several months ago, when I was beta testing the Propeller, I had trouble
'     getting the Cogs to communicate with each other. After some "wasted"
'     time I realized that I did not have enough stack space for one of my
'     cogs.  I hope this example will allow you to "skip" this error.
'MEMORY USAGE:
'  Program: 295 Longs
'  Variables: 70 Longs
'  Stack/Free: 7,823 Longs
```

Post Edited (Dave Scanlan) : 5/3/2006 5:55:32 PM GMT
• Posts: 829
edited 2006-05-05 13:10
A quick observation that might help anyone new to the Propeller, this includes me!

Was playing with Beau's IR-Decoder code and could not get it to work. This is what helped me:

1. Unpacked the *.zip to a temp directory

2. Made a directory and copied over the two custom files (IR_Test.spin and IR_JVC_RMV715U.spin to a folder in my:

·· C:\Program Files\Parallax Inc\Propeller Tool v0.95\Examples directory.

3. Connected IR hardware to Propeller board

4. Ran F10 (Load RAM + RUN)

5. Received an error of: "Expected "(" and the editor highlighted the term.start line. Changed this to term.start(3) I'm assuming that the libraries were recently updated as of V.095 Propeller Tool and the TV.SPIN now requires that you set the MODE.

6. Ran F10 again and it works flawlessly.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Mike
• Posts: 6,466
edited 2006-05-05 15:06
Mike,

What you mention is an excellent point about the archiving encapsulation option.
The object "tv_terminal" has changed slightly since I wrote the IR demo program,
but because I saved it as an archive, ALL of the necessary files are
there. What you need to do, is "unzip" the archive with all of the files in a specific
directory of your choice. When you run the Propeller Tool, go to that directory and
run the "top" file. The "top" file can be determined by looking at the "_README_" file
within the archive.

The program you mention was just an example, that ONLY works if you have a JVC_RMV715U remote.
If you have another remote, you will need to have an object specifically for that remote.

I would however be interested if "other" JVC or SONY remotes would work with this object.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Beau Schwabe

IC Layout Engineer
Parallax, Inc.

Post Edited (Beau Schwabe (Parallax)) : 5/5/2006 3:11:31 PM GMT
• Posts: 829
edited 2006-05-05 15:46
Beau,

I tried that, unzipping to a directory and then setting the Top File, but I must have done something wrong because I could get it to display until I did the procedure as described in my post. I'll go back and try again tonight when I get home. It's quite possible that I may have skipped a step.

As for remotes, I used a JVC remote from an old VCR that I no longer have. I did have to go reassign some of the DAT statements to each key that I was decoding, but other than that, worked like a champ! I suspect that you could use a universal remote and just pick a JVC item. Now I need to figure out how to do an overlay using a LM1881 chip!

Good job on you IR Decode program!

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Mike
• Posts: 6,466
edited 2006-05-05 16:04
Thanks Mike!

The only other thing I can think of... Check what the 'pins' under the 'tvparams' has for it's value between the 'tv_terminal' objects.
My board might be different than the released version of the Demo Board.

When you hit F8, everything compiles correctly right? ...you just don't see anything unless you use the "newer" 'tv_terminal' object. Right?

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Beau Schwabe

IC Layout Engineer
Parallax, Inc.

Post Edited (Beau Schwabe (Parallax)) : 5/5/2006 4:07:02 PM GMT
• Posts: 160
edited 2006-05-06 15:17

·······
EXAMPLE 18

··············· MENU-DRIVEN, SHIFT-RIGHT AND SHIFT-LEFT DEMO:
··································· USES PS/2 KEYBOARD AND LCD PANEL
·························

```'                                    EXAMPLE 18
'
'                  MENU-DRIVEN, SHIFT-RIGHT AND SHIFT-LEFT DEMO:
'                        USES PS/2 KEYBOARD AND LCD PANEL
'*****************************************************************************
'IMPORTANT: This example may require an understanding of examples 8 and 14
'*****************************************************************************
'WHAT'S NEW IN THIS EXAMPLE:
'     - In this example, an LCD will be used to display two menus.
'   KEYBOARD USED TO SELECT MENU CHOICES
'     - Using the keyboard, the user will be able to select menu choices.
'   SHIFT LEFT AND SHIFT RIGHT
'     - The bits in the variable X will be shifted left or right.
'     - X is only 8 bits to keep things simple.
'     - The shifting process will be displayed on the LCD in real time.
'   REPEAT-UNTIL and CASE
'     - Menu input is often constucted from these two control structures.
'*****************************************************************************
'DIFFICULTY LEVEL: Easy-to-intermediate for a beginner.
'*****************************************************************************
'PURPOSE: The purpose of this example is to illustrate:
'          1.  How to set up menus in Spin on an LCD.
'          2.  How to use the keyboard to make menu choices.
'          3.  How to shift bits in a variable while watching the process
'              in real time. The user can select how many bits to shift.
'Submitted by Dave Scanlan,  May 6, 2006
'File: Example18_ShiftLeftRight.spin
'*****************************************************************************
'CORRECT OUTPUT: The LCD will display the Menus below:
'
'                                  4x20 LCD Module
'                           &#9474;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#9474;
'                           &#9474;      BIT SHIFT DEMO      &#9474;
'                           &#9474; R - Shift Right          &#9474;
'                           &#9474; L - Shift Left           &#9474;
'                           &#9474;>R<                       &#9474;
'                           &#9474;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#9474;
'
'                                 4x20 LCD Module
'                           &#9474;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#9474;
'                           &#9474;HOW MANY BITS? >7<        &#9474;
'                           &#9474; BINARY X: 00000001       &#9474;
'                           &#9474; DECIMAL X: 1             &#9474;
'                           &#9474;Restart: Esc Key          &#9474;
'                           &#9474;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#9474;
'*****************************************************************************
'
CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000
'
'KEYBOARD SETUP
PS2ToProperllerPins_Setup = 6  'See number (1) at the bottom.
KeyLocks_Setup = %0_000_010    'CapsLock ON: See number (2) at the bottom.
' KeyLocks_Setup = %0_000_000    'CapsLock OFF: See number (2) at the bottom.
AutoRepeat_Setup = %01_01000   'See number (3) at the bottom.
'
'LCD SETUP
LCD_Pin   = 0
LCD_Baud  = 19_200
LCD_Lines = 4
'
Off = 0
On = 1
R = 82
L = 76
'
VAR
Byte X
Byte NumOfBits                  'Number of bits to shift
Byte ASCII_Key                  'Holds ASCII value from Keyboard
Byte Right
Byte Left
'
OBJ
KB : "Keyboard_iso"             'Creates Keyboard (KB) object
LCD: "Debug_LCD"                'Creates LCD object
'
PUB Start
Initialize_KeyBoardAndLCD
Input_RightOrLeftChoice
Repeat
Input_NumOfBitsToShift
ShiftBitsInVariableX
'
'INITIALIZES KEYBOARD AND LCD
PRI Initialize_KeyBoardAndLCD
KB.Startx(PS2ToProperllerPins_Setup, KeyLocks_Setup, AutoRepeat_Setup)
WaitCnt(50_000_000 + Cnt)     'YOU MUST HAVE THIS DELAY TO INITIALIZE KB
' LCD.Start(0,       19_200,   4        )
LCD.Start(LCD_Pin, LCD_Baud, LCD_Lines)
'
'DISPLAYS MENU FOR LEFT OR RIGHT SHIFT CHOICE
LCD.Cursor(On)
LCD.Backlight(On)
LCD.Cls
LCD.Str(string("   BIT SHIFT DEMO"))
LCD.Gotoxy(1,1)
LCD.Str(string("R - Shift Right"))
LCD.Gotoxy(1,2)
LCD.Str(string("L - Shift Left"))
LCD.Gotoxy(0,3)
LCD.Str(string("> <"))
'
'INPUTS MENU DATA: (RIGHT OR LEFT SHIFT CHOICE)
PRI Input_RightOrLeftChoice
REPEAT
LCD.Gotoxy(1,3)             'LCD.Gotoxy(Col,Row)
ASCII_Key := KB.GetKey
Case ASCII_Key
"R" : LCD.Str(string("R"))
Right := True
Left := False
"L" : LCD.Str(string("L"))
Left := True
Right := False
UNTIL ASCII_Key == R OR ASCII_Key == L
WaitCNT(10_000_000 + CNT)
'
'DISPLAYS MENU FOR CHOOSING NUMBER OF SHIFT BITS
WaitCNT(80_000_000 + CNT)     'Delay while user reads display
LCD.Cls
LCD.Str(String("HOW MANY BITS? > <"))
LCD.Gotoxy(1,1)
LCD.Str(String("BINARY X:"))
LCD.Gotoxy(1,2)
LCD.Str(String("DECIMAL X:"))
LCD.Gotoxy(0,3)
LCD.Str(String("Restart: Esc Key"))
'
'INPUT MENU DATA: (NUMBER OF SHIFT BITS)
PRI Input_NumOfBitsToShift
LCD.Gotoxy(16,0)
ASCII_Key := KB.GetKey
If ASCII_Key == \$CB           'Looks for Esc Key (\$CB)
Start                     'Restarts program
Case ASCII_Key
"0" : LCD.Str(string("0"))
NumOfBits := 0
"1" : LCD.Str(string("1"))
NumOfBits := 1
"2" : LCD.Str(string("2"))
NumOfBits := 2
"3" : LCD.Str(string("3"))
NumOfBits := 3
"4" : LCD.Str(string("4"))
NumOfBits := 4
"5" : LCD.Str(string("5"))
NumOfBits := 5
"6" : LCD.Str(string("6"))
NumOfBits := 6
"7" : LCD.Str(string("7"))
NumOfBits := 7
"8" : LCD.Str(string("8"))
NumOfBits := 8
Other: Input_NumOfBitsToShift
'
'DISPLAYS SHIFTING BITS AND CHANGING DECIMAL VALUE
PRI ShiftBitsInVariableX
X := %1111_1111
LCD.Gotoxy(11,1)
LCD.Bin(X,8)
LCD.Gotoxy(12,2)
LCD.DecF(X,3)
LCD.ClrLn(3)
WaitCNT(80_000_000 + CNT)    '1 second delay while user reads display
Repeat NumOfBits
If Right
X := X >> 1              'Shift bits in X to the right.
If Left
X := X << 1              'Shift bits in X to the left.
LCD.Gotoxy(11,1)
LCD.Bin(X,8)
LCD.Gotoxy(12,2)
LCD.DecF(X,3)              'Decimal with a fixed field of size 3.
WaitCNT(80_000_000 + CNT)  '1 second delay while user reads display
WaitCNT(80_000_000 + CNT)    '1 second delay while user reads display
'*****************************************************************************
'  --  This example will help a beginning programmer visualize what happens
'      when bits are shifted.
'  --  You will need a PS/2 keyboard and a serial LCD (4x20)
'  --  The code was written to make the application easy to understand.
'
'KEYBOARD SETTINGS EXPLAINED (SEE CONSTANT SECTION ABOVE.)
'(1) 6 is a setup code that will properly match the pins on a PS/2 male
'    conector with a PS/2 female connector that is connected to the
'    the Propeller.  See Keyboard_iso.spin for more pin setup codes.
'(2) %0_000_010 is a setup code that will set CapsLock ON (bit 1). See
'    Keyboard_iso for more KeyLock setup settings, such as NumLock and
'    ScrollLock.
'(3) %01_01000 is a setup code that will produce a .5sec (bit 5) delay before
'    auto repeat starts and a 15cps repeat rate (bit 3). See Keyboard_iso
'    for more setup settings.
'
'MEMORY USAGE:
'  Program: 798 Longs
'  Variables: 42 Longs
'  Stack/Free: 7,348 Longs
```

```'
'                               EXAMPLE 18
'
'                  MENU-DRIVEN, SHIFT-RIGHT AND SHIFT-LEFT DEMO:
'                        USES PS/2 KEYBOARD AND LCD PANEL
'*****************************************************************************
'IMPORTANT: This example may require an understanding of examples 8 and 14
'*****************************************************************************
'DIFFICULTY LEVEL: Easy to intermediate for a beginner.
'*****************************************************************************
'CORRECT OUTPUT: The LCD will display the Menus below:
'
'                                  4x20 LCD Module
'                           &#9474;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#9474;
'                           &#9474;      BIT SHIFT DEMO      &#9474;
'                           &#9474; R - Shift Right          &#9474;
'                           &#9474; L - Shift Left           &#9474;
'                           &#9474;>R<                       &#9474;
'                           &#9474;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#9474;
'
'                                 4x20 LCD Module
'                           &#9474;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#61574;&#9474;
'                           &#9474;HOW MANY BITS? >7<        &#9474;
'                           &#9474; BINARY X: 00000001       &#9474;
'                           &#9474; DECIMAL X: 1             &#9474;
'                           &#9474;Restart: Esc Key          &#9474;
'                           &#9474;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#61569;&#9474;
'Submitted by Dave Scanlan,  May 6, 2006
'File: Example18_ShiftLeftRight.spin
'*****************************************************************************
'
CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000
'
'KEYBOARD SETUP
PS2ToProperllerPins_Setup = 6
KeyLocks_Setup = %0_000_010
' KeyLocks_Setup = %0_000_000
AutoRepeat_Setup = %01_01000
'
'LCD SETUP
LCD_Pin   = 0
LCD_Baud  = 19_200
LCD_Lines = 4
'
Off = 0
On = 1
R = 82
L = 76
'
VAR
Byte X
Byte NumOfBits
Byte ASCII_Key                  '
Byte Right
Byte Left
'
OBJ
KB : "Keyboard_iso"
LCD: "Debug_LCD"
'
PUB Start
Initialize_KeyBoardAndLCD
Input_RightOrLeftChoice
Repeat
Input_NumOfBitsToShift
ShiftBitsInVariableX
'
'INITIALIZES KEYBOARD AND LCD
PRI Initialize_KeyBoardAndLCD
KB.Startx(PS2ToProperllerPins_Setup, KeyLocks_Setup, AutoRepeat_Setup)
WaitCnt(50_000_000 + Cnt)     'YOU MUST HAVE THIS DELAY TO INITIALIZE KB
' LCD.Start(0,       19_200,   4        )
LCD.Start(LCD_Pin, LCD_Baud, LCD_Lines)
'
'DISPLAYS MENU FOR LEFT OR RIGHT SHIFT CHOICE
LCD.Cursor(On)
LCD.Backlight(On)
LCD.Cls
LCD.Str(string("   BIT SHIFT DEMO"))
LCD.Gotoxy(1,1)
LCD.Str(string("R - Shift Right"))
LCD.Gotoxy(1,2)
LCD.Str(string("L - Shift Left"))
LCD.Gotoxy(0,3)
LCD.Str(string("> <"))
'
'INPUTS MENU DATA: (RIGHT OR LEFT SHIFT CHOICE)
PRI Input_RightOrLeftChoice
REPEAT
LCD.Gotoxy(1,3)
ASCII_Key := KB.GetKey
Case ASCII_Key
"R" : LCD.Str(string("R"))
Right := True
Left := False
"L" : LCD.Str(string("L"))
Left := True
Right := False
UNTIL ASCII_Key == R OR ASCII_Key == L
WaitCNT(10_000_000 + CNT)

'
'DISPLAYS MENU FOR CHOOSING NUMBER OF SHIFT BITS
WaitCNT(80_000_000 + CNT)
LCD.Cls
LCD.Str(String("HOW MANY BITS? > <"))
LCD.Gotoxy(1,1)
LCD.Str(String("BINARY X:"))
LCD.Gotoxy(1,2)
LCD.Str(String("DECIMAL X:"))
LCD.Gotoxy(0,3)
LCD.Str(String("Restart: Esc Key"))
'
'INPUT MENU DATA: (NUMBER OF SHIFT BITS)
PRI Input_NumOfBitsToShift
LCD.Gotoxy(16,0)
ASCII_Key := KB.GetKey
If ASCII_Key == \$CB           'Looks for Esc Key (\$CB)
Start                     'Restarts program
Case ASCII_Key
"0" : LCD.Str(string("0"))
NumOfBits := 0
"1" : LCD.Str(string("1"))
NumOfBits := 1
"2" : LCD.Str(string("2"))
NumOfBits := 2
"3" : LCD.Str(string("3"))
NumOfBits := 3
"4" : LCD.Str(string("4"))
NumOfBits := 4
"5" : LCD.Str(string("5"))
NumOfBits := 5
"6" : LCD.Str(string("6"))
NumOfBits := 6
"7" : LCD.Str(string("7"))
NumOfBits := 7
"8" : LCD.Str(string("8"))
NumOfBits := 8
Other: Input_NumOfBitsToShift
'
'DISPLAYS SHIFTING BITS AND CHANGING DECIMAL VALUE
PRI ShiftBitsInVariableX
X := %1111_1111
LCD.Gotoxy(11,1)
LCD.Bin(X,8)
LCD.Gotoxy(12,2)
LCD.DecF(X,3)
LCD.ClrLn(3)
WaitCNT(80_000_000 + CNT)
Repeat NumOfBits
If Right
X := X >> 1              'Shift bits in X to the right.
If Left
X := X << 1              'Shift bits in X to the left.
LCD.Gotoxy(11,1)
LCD.Bin(X,8)
LCD.Gotoxy(12,2)
LCD.DecF(X,3)
WaitCNT(80_000_000 + CNT)
WaitCNT(80_000_000 + CNT)

'*****************************************************************************
'  --  This example will help a beginning programmer visualize what happens
'      when bits are shifted.
'  --  You will need a PS/2 keyboard and a serial LCD (4x20)
'  --  The code was written to make the application easy to understand.
'
'MEMORY USAGE:
'  Program: 798 Longs
'  Variables: 42 Longs
'  Stack/Free: 7,348 Longs
```

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
··

Post Edited (Dave Scanlan) : 5/6/2006 7:07:03 PM GMT