re-naming a CONST array name

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

toyman
Posts: 39
Joined: Tue Feb 18, 2014 8:25 pm

re-naming a CONST array name

Post by toyman » Wed Feb 19, 2014 7:33 pm

Been trying to change the CONST array name within the code using a subroute. here is what I tried but dosen't compile

Code: Select all

SUB a1_data()

    A_Data(0) = Anodes_data(0)    ' THIS IS WHAT I NEED TO HAPPEN BUT FOR ALL THE CONST ARRAYS
    
   
  FOR index = 0 TO bound(anodes_data)     ' CHANGE Anodes_Data() to A_Data()
     Anodes_data(index)= A_Data(index)
  NEXT    
Here is what I am trying to change

Code: Select all

 portc = Anodes_Data(0)     '0
       portb = Cathodes_Data(0)
any suggestions. Here is another experiment but how to use??

Code: Select all

 SUB MySub(BYREF pAnodes_data AS WORD)

END SUB
How to designate present name and desired name

User avatar
David Barker
Swordfish Developer
Posts: 1214
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Re: re-naming a CONST array name

Post by David Barker » Thu Feb 20, 2014 3:28 pm

You can't change a constant value - by it's very nature it is fixed at compile time. I can't work out what you are trying to do with the code you have posted - what about the declarations?

toyman
Posts: 39
Joined: Tue Feb 18, 2014 8:25 pm

Re: re-naming a CONST array name

Post by toyman » Thu Feb 20, 2014 4:42 pm

Here is the short version. I have portc data in CONST arrays but want to rename the Anodes_data to desired CONST array. This code scrolls letters across from right to left but need to change the CONST array used from Anodes_data to desired CONST array data

Code: Select all

DEVICE = 18F2420

CLOCK = 8
   

// import LCD library...

INCLUDE "convert.bas"
INCLUDE "InternalOscillator.bas"
INCLUDE "Utils.bas"       // Include this file when we compile so that we can use keywords like 'setalldigital'
INCLUDE "shift.bas"

// Arrays                                                                                  
// Arrays

' LETTER K 
{
CONST Anodes_Data(9)AS BYTE = (%11111111,
                               %00011000,                            
                               %00100100,
                               %01000010,
                               %00000001,
                               %00000000,
                               %00000000,
                               %00000000,
                               %00000000)
                               
 
 }                              
                       
CONST Cathodes_Data(9)AS BYTE = (%01111111,       ' KEEP THESE AS THEY DON"T CHANGE PER LETTER
                                 %10111111,
                                 %11011111,
                                 %11101111,
                                 %11110111,
                                 %11111011,
                                 %11111101,
                                 %11111110,
                                 %11111111)
                                 
' LETTER A 
                                
CONST A_Data(9)AS BYTE =         (%01111111,		// A
                                  %10010000,
                                  %10010000,
                                  %10010000,
                                  %01111111,
                                  %00000000,
                                  %00000000,
                                  %00000000,
                                  %00000000)
                                  
                                  
' LETTER B
CONST B_Data(9)AS BYTE =         (%01111000, 		// B
                                  %00000000,                               
                                  %01000100,
                                  %01000100,
                                  %01111000,
                                  %01000100,
                                  %01000100,
                                  %01111000,
                                  %00000000)
' LETTER C                                  
CONST C_Data(9)AS BYTE =         (%00000000,		// C
                                  %00111000,
                                  %01000100,
                                  %01000000,
                                  %01000000,
                                  %01000000,
                                  %01000100,
                                  %00111000,
                                  %00000000)

                         

 SUB MySub()
     
  DIM A_data(9) AS BYTE
  
  '  Anodes_data = A_data    Have tried using a for next loop but didn't seem to work.
'This code as posted does grab the data in A-Data CONST array but all mixed up.
      
            
END SUB

// variable declaration                              
DIM x AS BYTE
DIM Anodes_data(9) AS BYTE
DIM index AS BYTE
'dim indexb as byte
'DIM bits AS BYTE
DIM y AS BYTE
DIM b(8) AS BYTE
'DIM Anodes_data(8) AS BYTE                
// Sub Routines

             

// Start Of Program
'portc = 0
'portb = 1
SetAllDigital                       // Make all Pins digital I/O's
TRISC = %00000000                   // Make PORTD all outputs
TRISB = %00000000                   // Make PORTB all outputs
y = 0
// Main Loop
WHILE True()
      MySub
      'a1_data ' SUB ROUTE TO CHANGE CONST ARRAY DATA FOR NEXT LETTER
   //000000000000000000000    
       portc = Anodes_data(0)     ' WANT TO CHANGE TO THE DATA IN A_DATA CONST ARRAY
       portb = Cathodes_Data(0)
       DELAYMS(10)
  //11111111111111111111111111   
       portc = Anodes_data(0)      '0
       portb = Cathodes_Data(1)
       DELAYMS(10)
       portc = Anodes_data(1)      '0
       portb = Cathodes_Data(0)
       DELAYMS(10)

User avatar
David Barker
Swordfish Developer
Posts: 1214
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Re: re-naming a CONST array name

Post by David Barker » Thu Feb 20, 2014 5:05 pm

I'm really sorry, I still don't understand your question. If you mean accessing a constant array, you use something like this

Code: Select all

device = 18F452
clock = 20

include "usart.bas"
include "convert.bas"   
CONST Anodes_Data(9)AS BYTE = (%11111111,
                               %00011000,                           
                               %00100100,
                               %01000010,
                               %00000001,
                               %00000000,
                               %00000000,
                               %00000000,
                               %00000000)
                               
 
dim index as byte
dim value as byte
Usart.SetBaudrate(br19200)
for index = 0 to bound(Anodes_Data)
   value = Anodes_Data(index)
   usart.write("Value = %", BinToStr(value, 8),13,10)
next
which is tested code and will output

Code: Select all

Value = %11111111
Value = %00011000
Value = %00100100
Value = %01000010
Value = %00000001
Value = %00000000
Value = %00000000
Value = %00000000
Value = %00000000

toyman
Posts: 39
Joined: Tue Feb 18, 2014 8:25 pm

Re: re-naming a CONST array name

Post by toyman » Thu Feb 20, 2014 7:12 pm

Trying to explain can be difficult without pictures --lol
In my WHILE WEND loop I have variable called Anodes_data(?)
which calls the CONST array data Anodes_data(?)
What I want to do is rename the Anodes_data variable to address a different CONST array.
I won't have Anodes_data as a CONST array name
portc = Anodes_data(?)
but the variable name Anodes_data is renamed A_data(?) using the data in A-Data CONST array.
the sub route for each new letter changes the variable Anodes_data name to use the CONST array data for each letter. 10 letters then 10 sub routines (one for each letter). Each letter has its own CONST array.
hopefully this explaines what I am trying to do. Rename the CONST array being used.

toyman
Posts: 39
Joined: Tue Feb 18, 2014 8:25 pm

Re: re-naming a CONST array name

Post by toyman » Thu Feb 20, 2014 9:32 pm

Maybe a better explanation?? The WHILE WEND loop has my explanation.

Code: Select all

{
*****************************************************************************
*  Name    : UNTITLED.BAS                                                   *
*  Author  : [select VIEW...EDITOR OPTIONS]                                 *
*  Notice  : Copyright (c) 2014 [select VIEW...EDITOR OPTIONS]              *
*          : All Rights Reserved                                            *
*  Date    : 2/20/2014                                                      *
*  Version : 1.0                                                            *
*  Notes   :                                                                *
*          :                                                                *
*****************************************************************************
}
DEVICE = 18F2420

CLOCK = 8
   

// import LCD library...

INCLUDE "convert.bas"
INCLUDE "InternalOscillator.bas"
INCLUDE "Utils.bas"       // Include this file when we compile so that we can use keywords like 'setalldigital'
INCLUDE "shift.bas"

// Arrays                                                                                  
// Arrays
 
' LETTER K 
CONST Anodes_Data(9)AS BYTE = (%11111111,
                               %00011000,                            
                               %00100100,
                               %01000010,
                               %00000001,
                               %00000000,
                               %00000000,
                               %00000000,
                               %00000000)
                               
 
                               
                       
CONST Cathodes_Data(9)AS BYTE = (%01111111,       ' KEEP THESE AS THEY DON"T CHANGE PER LETTER
                                 %10111111,
                                 %11011111,
                                 %11101111,
                                 %11110111,
                                 %11111011,
                                 %11111101,
                                 %11111110,
                                 %11111111)
                                 
' LETTER A                                 
CONST A_Data(9)AS BYTE =         (%00000000,		// A
                                  %01000100,
                                  %01000100,
                                  %01111100,
                                  %01000100,
                                  %01000100,
                                  %01000100,
                                  %00111000,
                                  %00000000)
                                  
                                  
' LETTER B
CONST B_Data(9)AS BYTE =         (%01111000, 		// B
                                  %00000000,                               
                                  %01000100,
                                  %01000100,
                                  %01111000,
                                  %01000100,
                                  %01000100,
                                  %01111000,
                                  %00000000)

 SUB MySub()
     
 ' DIM index AS BYTE  
  DIM Anodes_Data(9) AS BYTE 
  'FOR index = 0 TO BOUND(Anodes_Data)     ' CHANGE Anodes_Data() to A_Data()
     Anodes_Data= A_Data 
 ' NEXT    

END SUB

                                 

                         
 {
 const ConstArray(2) as byte = (100, 200) // two elements

dim VarArray(2) as byte                  // two elements

VarArray = ConstArray // assign values to variable array

                     
SUB a1_data()

    A_Data(0) = Anodes_data(0)    ' THIS IS WHAT I NEED TO HAPPEN BUT FOR ALL THE CONST ARRAYS
    
   
  FOR index = 0 TO bound(anodes_data)     ' CHANGE Anodes_Data() to A_Data()
     Anodes_data(index)= A_Data(index)
  NEXT    
   }  

                                     
                                                     
                             
// variable declaration                              
DIM x AS BYTE
'dim anodes_data(9) as byte
'DIM Anodes_Data(9) AS BYTE
'DIM index AS BYTE
'DIM bits AS BYTE
'DIM  AS BYTE
'DIM b(8) AS BYTE
'DIM Anodes_data(8) AS BYTE                
// Sub Routines

             

// Start Of Program
'portc = 0
'portb = 1
SetAllDigital                       // Make all Pins digital I/O's
TRISC = %00000000                   // Make PORTD all outputs
TRISB = %00000000                   // Make PORTB all outputs
'y = 0
// Main Loop
WHILE True()
      MySub ' SUB ROUTE TO CHANGE CONST ARRAY DATA FOR NEXT LETTER
   //000000000000000000000    
       portc = Anodes_Data(0)     '[size=150][b]Anodes_Data variable is supposed to be the values in CONST array A_Data[/size][/b]
       portb = Cathodes_Data(0)    ' [size=150]As the sub route MySub is supposed to make the change[/size]
       DELAYMS(1000)
  //11111111111111111111111111   
       portc = Anodes_Data(0)      '0
       portb = Cathodes_Data(1)
       DELAYMS(1000)
       portc = Anodes_Data(1)      '0
       portb = Cathodes_Data(0)
       DELAYMS(1000)
   //2222222222222222222222222 
   WEND   

bitfogav
Registered User
Registered User
Posts: 169
Joined: Sat Oct 09, 2010 1:39 pm
Location: United Kingdom

Re: re-naming a CONST array name

Post by bitfogav » Thu Feb 20, 2014 10:38 pm

I've read this topic a few times and I don't fully understand your question either. :?
You can't rename a const array :?

Is this some kind of LED Matrix that you are trying to display characters on?.

toyman
Posts: 39
Joined: Tue Feb 18, 2014 8:25 pm

Re: re-naming a CONST array name

Post by toyman » Thu Feb 20, 2014 11:25 pm

YES I am scrolling letters across an 8 x 8 array. In the code I wrote I have portc =Anodes_data(?)
I am not going to have a CONST array labeled Anodes_Data(9)
In my sub route I want to have the variable Anodes_Data(?) use the CONST array that the sub route calls for.
I never realized this would be harder to explain than actually getting it to work--lol
EXAMPLE: portc = Anodes_Data(0)
the CONST array I want Anodes_Data to be is A_Data(0)
There will be 26 CONST Arrays, each letter of the alphabet
Sub route 1 would be Anodes_Data(?) = A_Data(?)
Sub route 2 would be Anodes_Data(?) = B_Data(?)
Sub route 3 would be Anodes_Data(?) = C_Data(?)
Sub route 4 would be Anodes_Data(?) = D_Data(?)

I have a sub route that I found code for doing this? in the help file but it display a mixed up display.

Code: Select all

 SUB MySub()
     
 ' DIM index AS BYTE  
  DIM Anodes_Data(9) AS BYTE 
  'FOR index = 0 TO BOUND(Anodes_Data)     ' CHANGE Anodes_Data() to A_Data()
     Anodes_Data= A_Data 
 ' NEXT    

END SUB
I tested to see if everything else is working as planned and all is in order except the display, using this sub route, displays jumbled bits scrolling across instead of letters as hoped.
I have about 20 lines of code with portc=Anodes_Data and without adding 20 lines for each letter I figure just changing the CONST array data being used in the WHILE WEND loop.
Am not changing the CONST array data, just the "pointer" name of which CONST array to use each pass through the sub routing for each desired letter.

User avatar
David Barker
Swordfish Developer
Posts: 1214
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Re: re-naming a CONST array name

Post by David Barker » Fri Feb 21, 2014 8:02 am

Still not sure what you mean - I will give it one last try. You can assign a constant array to a variable array like this:

Code: Select all

Include "usart.bas"
Include "convert.bas"   
Const A_Data(9)As Byte = (%11111111,
                          %00011000,                           
                          %00100100,
                          %01000010,
                          %00000001,
                          %00000000,
                          %00000000,
                          %00000000,
                          %00000000)  
dim Anodes_Data(9) as byte                            
Dim index As Byte

' set baudrate
USART.SetBaudrate(br19200) 

' assign constant array to variable array        
Anodes_Data = A_Data 

' display results...               
For index = 0 To Bound(Anodes_Data)
   USART.Write("Value = %", BinToStr(Anodes_Data(index), 8),13,10)
Next
or you can pass the whole constant array to a subroutine and assign it, like this:

Code: Select all

Include "usart.bas"
Include "convert.bas"   
Const A_Data(9)As Byte = (%11111111,
                          %00011000,                           
                          %00100100,
                          %01000010,
                          %00000001,
                          %00000000,
                          %00000000,
                          %00000000,
                          %00000000)  
                          
sub Assign(byrefConst array() as byte)
   dim index as byte
   dim Anodes_Data(9) as byte  
   Anodes_Data = array
   For index = 0 To Bound(Anodes_Data)
      USART.Write("Value = %", BinToStr(Anodes_Data(index), 8),13,10)
   Next
end sub
                          
' program start...
USART.SetBaudrate(br19200)       
Assign(A_Data) 

SHughes_Fusion
Posts: 219
Joined: Wed Sep 11, 2013 1:27 pm
Location: Chesterfield

Re: re-naming a CONST array name

Post by SHughes_Fusion » Fri Feb 21, 2014 10:30 am

Your code snippets are almost impossible to follow as they don't seem to reflect what you are actually doing.

However, a suggestion from a library I wrote to display fonts on an LCD. Why not have one large Const array for all your character data. You can still enter is in a 'readable' format but when it is all in the same array, all you need to do is calculate the offset of each character and you only have one array to reference.

Mine is slightly more complicated as I allow for variable width characters so I start with a look-up table but as your characters are fixed width you can just do (ASCII value - 65) * 8 to get the offset.

toyman
Posts: 39
Joined: Tue Feb 18, 2014 8:25 pm

Re: re-naming a CONST array name

Post by toyman » Fri Feb 21, 2014 1:17 pm

THANKS David I will try the routine you suggested. If it dosen't output in the right order then will try what SHUGHES FUSION suggested.
I did find that the sub route I have been trying does output the desired CONST array but it is all jumbled in wrong order..
the Sub I tried last night

Code: Select all

 SUB MySub()
     
  DIM index AS BYTE  
  DIM Anodes_Data(9) AS BYTE 
  FOR index = 0 TO BOUND(Anodes_Data)     ' CHANGE Anodes_Data() to A_Data()
     Anodes_Data(index)= A_Data(index) 
  NEXT    

END SUB


Jerry Messina
Swordfish Developer
Posts: 1470
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: re-naming a CONST array name

Post by Jerry Messina » Fri Feb 21, 2014 1:30 pm

Here is the short version.
and here's the long version... http://www.electro-tech-online.com/thre ... ix.139965/

This is probably futile, but try using David's first example.

Code: Select all

dim Anodes_Data(9) as byte                           

' assign constant array to variable array       
Anodes_Data = A_Data
when you want to change the character, just set Anodes_Data again

Code: Select all

' assign constant array to variable array       
Anodes_Data = B_Data
' or
Anodes_Data = C_Data
'etc
There's a good chance that if you try to put it into a sub you'll never get it declared right

toyman
Posts: 39
Joined: Tue Feb 18, 2014 8:25 pm

Re: re-naming a CONST array name

Post by toyman » Fri Feb 21, 2014 4:02 pm

Your right Jerry, when I go to declare the new Const array in place of the Anodes_data it gets fouled up. It is changing the CONST array name but its all mixed up.
Going back to Davids first suggestion and see where I went wrong.
I ran Davids last code and using the Suart it appears to work just fine but won't work when the main body of code is used.

toyman
Posts: 39
Joined: Tue Feb 18, 2014 8:25 pm

Re: re-naming a CONST array name

Post by toyman » Fri Feb 21, 2014 4:19 pm

Somewhere along the line of trying different code I tried Davids and Jerrys suggestion but had something wrong as it works now (think I had it in a sub route?) BUT
need to cycle through
Anodes_data = A_Data
main code
Anodes_data = B_Data
main code
Anodes_data = C_Data
main code


etc. Changing the CONST array that Anodes_data uses.
Tried sub route and a big jumble mess again.
My question is WHY going to a sub route it jumbles the data up??

toyman
Posts: 39
Joined: Tue Feb 18, 2014 8:25 pm

Re: re-naming a CONST array name

Post by toyman » Fri Feb 21, 2014 4:52 pm

Using CASE SELECT I am able to cycle through the CONST array blocks

Code: Select all

WHILE True()
   for x = 0 to 2
   select x
   case 0  Anodes_data = A_data
   case 1  Anodes_data = B_data
   case 2  Anodes_data = C_data
  end select
   
appears to work just fine
Can't believe the simple solution to changing the CONST array name selected. Even harder to explain I think??
LOL

Post Reply