PDA

View Full Version : Simple Array Demo



Archangel
- 28th May 2008, 08:41
Hello,
this little example just cycles through a 26 element array and displays it's value. I wrote it to
teach myself array function, and with help from Jerson it works as expected. I am posting it here to assist noobies trying to grasp array function.


'************************************************* ***************
'* Name : ARRAY DEMO.BAS *
'* Author : Joe S. with Thanks to Jerson in Bombay *
'* Notice : Copyright (c) Joe S. *
'* : No Rights Reserved, Use as you see fit *
'* Date : 5/21/08 *
'* Version : 1.0 *
'* Notes :Demo Array Counter reads array 1 BYTE at a time *
'* and displays serially on backpack LCD *
'* :PBP VER 2.50 *
'DEMONSTRATES USE OF FOR NEXT LOOP TO READ ARRAY * *
'THIS PROGRAM PROVES SERIN AND SERIN2 WILL WORK ON THE SAME PIN *
'WITHIN A SINGLE PROGRAM. DEMONSTRATES USE OF STR FUNCTION WITH *
'SERIN2.COMPILE WITH MPASM ASSEMBLER.COMPILES TO 419 WORDS IN *
'16F628A *
'************************************************* ***************
@ __config _HS_OSC & _WDT_OFF & _PWRTE_ON & _LVP_OFF & _BODEN_OFF
DEFINE OSC 20
include "modedefs.bas"
'
LETTERS VAR BYTE ' VARIABLE TO HOLD CHARACTERS IN F/N LOOP
'
CHAR var byte[26]' ARRAY VARIABLE NAMED CHAR 26 ELEMENTS
'
serout PortB.6,T9600,[254,128,"ARRAY DEMO"] ' SHOW US YOU ARE ALIVE
'
pause 500 ' TIME TO VIEW LCD SPLASH STATEMENT
'
serout PortB.6,T9600,[$FE,1]'CLEAR LCD PRIOR TO WRITING
'
'* * * * * * LOAD ARRAY WITH STRING VALUE
CHAR[0]= "A"
CHAR[1]= "B"
CHAR[2]= "C"
CHAR[3]= "D"
CHAR[4]= "E"
CHAR[5]= "F"
CHAR[6]= "G"
CHAR[7]= "H"
CHAR[8]= "I"
CHAR[9]= "J"
CHAR[10]="K"
CHAR[11]="L"
CHAR[12]="M"
CHAR[13]="N"
CHAR[14]="O"
CHAR[15]="P"
CHAR[16]="Q"
CHAR[17]="R"
CHAR[18]="S"
CHAR[19]="T"
CHAR[20]="U"
CHAR[21]="V"
CHAR[22]="W"
CHAR[23]="X"
CHAR[24]="Y"
CHAR[25]="Z"
'* * * * * * * DISPLAY ARRAY ELEMENTS VALUES SEQUENTIALY
loop:
FOR LETTERS = 0 to 25 'CYCLE FROM A TO Z
'
serout PortB.6,T9600,[254,128,"CHAR is ",(CHAR[LETTERS])]
pause 500 'TIME TO VIEW LETTER
NEXT LETTERS

FOR LETTERS=25 TO 0 STEP -1 'CYCLE THEM IN REVERSE Z TO A
serout PortB.6,T9600,[254,128,"CHAR is ",(CHAR[LETTERS])]
PAUSE 500 ' TIME TO VIEW LETTERS
NEXT LETTERS
'
PAUSE 500 'TIME TO VIEW LAST LETTER
goto LOOP2
'* * * * * * * * * * DISPLAY ENTIRE ARRAY VALUE AT ONCE
LOOP2:
serout2 portb.6,84,[$FE,1] 'CLEAR LCD PRIOR TO WRITING TO IT
'
PAUSE 50 'TIME FOR LCD TO SETTLE, DROPS 4 CHARS WITHOUT THIS
'
serout2 PortB.6,84,10,[str char\26]
' PACE 10 ALLOWS SEROUT2 TO QUICKLY WRITE ARRAY'S FULL VALUE TO LCD
'
pause 2000 ' TIME TO VIEW FULL ARRAY'S CONTENT
'
serout2 portb.6,84,100,[$FE,1] 'CLEAR LCD BEFORE LOOP
goto loop
'
end
I realise there are likely easier ways to load the ARRAY . . .Baby Steps
Anyone wishing to post more or better examples of ARRAY functions are invited to do so on this post.
Thank You,
JS

Archangel
- 11th May 2009, 23:37
Still playing with and learning about arrays, mostly by trying to solve newbies problems. Here is a demo to write to and read from a 24LC01B I2C EEPROM using a 12F675. It outputs to a serial LCD.


' I2CREAD and I2WRITE Commands with External EEPROM on 12-bit core
'
' Write to the first 36 locations of External I2C EEPROM
' Read from eeprom and output sequentaly then as strings
' Tested on 12F675 using 24LC01B EEPROM
@ __config _INTRC_OSC_NOCLKOUT & _PWRTE_ON & _WDT_ON & _MCLRE_ON & _CP_OFF & _BODEN_OFF
DEFINE OSC 4

DEFINE DEBUG_REG GPIO
DEFINE DEBUG_BIT 2
DEFINE DEBUGIN_BIT 5
DEFINE DEBUG_BAUD 2400
DEFINE DEBUG_MODE 0
DEFINE SHIFT_PAUSEUS 1000

EECON1 = 7 'Probably not needed here, enables internal eeprom
CMCON = 7 'Disable comparators
ADCON0.0 = 0 'Disable A/D converters
ANSEL = 0 'Disables Analog Select
DPIN var GPIO.0 'Assign Data pin
CPIN var GPIO.1 'Assign Clock pin
' a nice place to store things
B0 var byte
B1 var byte
B2 var byte
B3 VAR BYTE

GPIO = 0 ' set port latches low
TRISIO = 0 ' set ports as outputs

pause 1000 ' Waste time while serial LCD initiailizes

For B2 = 0 To 35 ' counter for lookup table
lookup b2,["A","B","C","D","E","F","G","H","I","J","K","L","M",_
"N","O","P","Q","R","S","T","U","V","W","X","Y","Z","0","1","2",_
"3","4","5","6","7","8","9"],B0 ' store each char in b0 1 at a time

I2CWRITE DPIN,CPIN,$A0,B2,[ b0] 'write to 24C01 eeprom 1 character
Pause 20
Next B2 'next character write to eeprom
DEBUG 254,1 'clear LCD of any leftover data
loop:
DEBUG 254,1 'clears away the leftovers each loop

For B3 = 0 To 35 ' counter for reading 35 characters
I2CREAD DPIN,CPIN,$A0,b3,[B1] ' read each character 1 at a time
pause 500 ' time to see each character on the LCD


DEBUG 254,2,"CHAR ",B1," " ' display the characters 1 at a time
'
Next B3 ' get next character

I2CREAD DPIN,CPIN,$A0,0,[STR B1\20] ' read a string of 20 characters
DEBUG 254,2,STR B1\20 ' display those 20 characters
I2CREAD DPIN,CPIN,$A0,16,[STR B1\16]' get the last 16 characters
DEBUG 254,$C0,STR B1\16," " ' display those 16 characters
DEBUG 254,$94,"I2C EEPROM DEMO" ' display this program's purpose
DEBUG 254,$D4,"USING ARRAY & STRING"
PAUSE 1500 ' time to read the LCD
'NEXT B3 ' go do it all again
Goto loop
end
POKECODE @$3FF, $4C 'read your PIC before erasing and put the cal. data here

sougata
- 31st May 2009, 10:37
Hi,

"A,B,C..." all have an ASCII value. For example "A"=65 so an iteration like this can help



For I = 0 to 25
char[I] = I + 65
Next I


P.S. PBP Manual has the ascii chart

Archangel
- 14th February 2010, 19:09
Hi,

"A,B,C..." all have an ASCII value. For example "A"=65 so an iteration like this can help



For I = 0 to 25
char[I] = I + 65
Next I


P.S. PBP Manual has the ascii chart
As I look at this a year later, I now understand what this snippet does.
"I realise there are likely easier ways to load the ARRAY . . .Baby Steps" It loads the variable CHAR with the Alphabet,<font color = BLUE> A Belated Thanks, Sougata .</font color>
This response is to point that fact out to anyone new to arrays. Attention Newbies :D

Bruce
- 14th February 2010, 19:41
Joe,

Did you know you can do this?



Dat VAR BYTE
Alphabet VAR BYTE[26]

Main:
FOR Dat = "A" to "Z"
Alphabet[Dat-65]=Dat ' load Alphabet array 0-25 with A-Z
HSEROUT [Alphabet[Dat-65],13,10]
PAUSE 50
NEXT Dat
PAUSE 5000
GOTO Main

END

Archangel
- 15th February 2010, 05:46
Joe,

Did you know you can do this?



Dat VAR BYTE
Alphabet VAR BYTE[26]

Main:
FOR Dat = "A" to "Z"
Alphabet[Dat-65]=Dat ' load Alphabet array 0-25 with A-Z
HSEROUT [Alphabet[Dat-65],13,10]
PAUSE 50
NEXT Dat
PAUSE 5000
GOTO Main

END
Woo Hoo, another tidbit of knowledge! I did not know that.
Thank You Bruce.
JS