' ' 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[NumberOfElements] ' - Example: Array[10] has 10 elements: Array[0] thru Array[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 ' ││ ' │ ARRAY VALUES: │ ' │ 0123456789 │ ' │ LARGEST VALUE:9 │ ' │ │ ' ││ ' '********************************************************************************** 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[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[I] := J J := J + 1 ' PRI FindLargestValue | I ' "I" is a local variable Largest := Array[0] 'Start the compares with the first element Repeat I From 1 to 9 'Find the largest value in an array If Array[I] > Largest Largest := Array[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[I],1) 'Display all array values ' (Array[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. '********************************************************************************** 'ADDITIONAL INFORMATION: ' -- With some languages, a declaration of Array[10] produces 11 locations: ' Array[0],[1],[2],[3],[4],[5],[6],[7],[8],9],[10] ' -- With Spin, a declaration of Array[10] produces exactly 10 locations: ' A[0],A[1],A[2],A[3],A[4],A[5],A[6],A7],A[8],A[9]. ' -- MEMORY USAGE ' Program: 308 Longs ' Variables: 22 Longs ' Stack/Free: 7,858 Longs