Simple Array Demo


Closed Thread
Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818

    Default Simple Array Demo

    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.
    Code:
    '****************************************************************
    '*  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
    Last edited by Archangel; - 10th April 2011 at 04:16. Reason: make code more readable.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  2. #2
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default Array and external EEPROM Demo

    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.
    Code:
    ' 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
    Last edited by Archangel; - 11th May 2009 at 23:43.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  3. #3
    Join Date
    Feb 2005
    Location
    Kolkata-India
    Posts
    563


    Did you find this post helpful? Yes | No

    Default A For-Next Loop

    Hi,

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

    Code:
    For I = 0 to 25
    char[I] = I + 65
    Next I
    P.S. PBP Manual has the ascii chart
    Attached Images Attached Images  
    Regards

    Sougata

  4. #4
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by sougata View Post
    Hi,

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

    Code:
    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
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  5. #5
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    Joe,

    Did you know you can do this?

    Code:
    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
    Last edited by Bruce; - 14th February 2010 at 19:56. Reason: Better example
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  6. #6
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default Nope !

    Quote Originally Posted by Bruce View Post
    Joe,

    Did you know you can do this?

    Code:
    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
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

Similar Threads

  1. Bits, Bytes Words and Arrays
    By Melanie in forum FAQ - Frequently Asked Questions
    Replies: 24
    Last Post: - 14th June 2016, 08:55
  2. 16f887 44 pin demo board code problem?
    By jessey in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 7th December 2008, 15:17
  3. Schematic for simple demo
    By iandonahue in forum USB
    Replies: 7
    Last Post: - 4th March 2008, 16:18
  4. RS232 receive an array (packet)
    By ELCouz in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 12th February 2008, 06:02
  5. Array to simple Variable converting
    By mrx23 in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 2nd September 2006, 17:44

Members who have read this thread : 2

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts