PDA

View Full Version : How to write port names into array ?



CuriousOne
- 11th December 2013, 12:47
Hello.

I have a code, where port names are assigned to specific variables, like this:



AC5 var PORTC.5
AC4 var PORTC.4
AC3 var PORTB.3
AC2 var PORTB.1
AC1 var PORTB.5

AR1 var PORTB.6
AR2 var PORTC.7
AR3 var PORTB.2
AR4 var PORTB.0
AR5 var PORTB.4
AR6 var PORTB.7
AR7 var PORTC.6


I want to modify code in te way, to store this data into array, so I can read sequentally with FOR-NEXT. How should I add port names into array members?

Say my array name is ROW, code should look like:

ARRAYWRITE ROW, [what maxlength?, what label?]

Amoque
- 12th December 2013, 03:55
There is good info here (http://www.picbasic.co.uk/forum/showthread.php?t=544) addressing your question, both better written and more correct than anything I may post. It explains how to read and write bits in bytes, bytes in words, bits in arrays, etc.

CuriousOne
- 12th December 2013, 05:34
Thanks, but it does not answers.

In fact, another memeber there asks exact same question, but gets no answer.

Demon
- 12th December 2013, 05:50
Does this help?

http://www.picbasic.co.uk/forum/showthread.php?t=3753

Robert

CuriousOne
- 12th December 2013, 08:03
Sure, this is way better, but still not exactly what I want. Since it utilizes loop and constant subroutine calls. I want to have defined the array in the beginning of the code, and access it as needed.

Archangel
- 12th December 2013, 08:10
So are you looking to . . .
AC5 var PORTC.5
AC4 var PORTC.4
AC3 var PORTB.3
AC2 var PORTB.1
AC1 var PORTB.5

AC = %00010101
and then have the listed ports go high according to the declaration above?
If SO try AC = ~~%00010101

Amoque
- 12th December 2013, 12:53
Perhaps I misunderstood your inquiry; it is difficult with so little explanation...

Perhaps you mean:

'DEFINE ARRAY
AC var bit[7]

'ASSIGN ALIAS
SYMBOL AC[5] = PORTC.5
SYMBOL AC[4] = PORTC.4
.
SYMBOL AC[1] = PORTB.5

'LOOP THRU VALUES

FOR LOOP = 5 to 1 STEP -1
AC[LOOP] = Some assigned bit value
Next LOOP

Of course, the bit array may be expanded to include as many elements as you need and you may SYMBOL each element to any port or single bit variable you like to access them through a loop - which need not begin at the first element nor continue through the last.

You mention ARRAYWRITE, but if the answer was there you would certainly have seen that in the manual, yes?

CuriousOne
- 13th December 2013, 04:15
Yes, I want to put different port names into array, so when needed, I can read or address them sequentally.

CuriousOne
- 13th December 2013, 08:19
So what I want to do, I'll write a code in some imaginary basic, a bit short code that will do a running led light:

DIM LEDS(3) 'define array with 3 entries
LET LEDS(1)=PORTA.2
LET LEDS(2)=PORTB.3
LET LEDS(3)=PORTC.1 'enter data into array

FOR A=1 TO 3
HIGH LEDS(A)' make pin high
pause 500 ' wait some time
LOW LEDS(A) 'make pin low
NEXT A

CuriousOne
- 13th December 2013, 09:45
So I wrote it in this way, but it does not works:



AC VAR BIT[5]
AR VAR BIT[7]


AC[5]=PORTC.5
AC[4]=PORTC.4
AC[3]=PORTB.3
AC[2]=PORTB.1
AC[1]=PORTB.5

AR[1]=PORTB.6
AR[2]=PORTC.7
AR[3]=PORTB.2
AR[4]=PORTB.0
AR[5]=PORTB.4
AR[6]=PORTB.7
AR[7]=PORTC.6

'make all leds connected to ports on.

low ac[1]
low ac[2]
low ac[3]
low ac[4]
low ac[5]
high ar[1]
high ar[2]
high ar[3]
high ar[4]
high ar[5]
high ar[6]
high ar[7]

'but it does not works
'but it works with code below
low PORTB.3
HIGH PORTC.7



end

HenrikOlsson
- 13th December 2013, 10:02
Hi
It doesn't work that way, it won't work that way and it can't work that way.

When you do AR VAR BIT[7] you are reserving space in RAM for 7 consecutive bits. You can't them simultanously mirror each bit in RAM to a ANOTHER adress (which the ports are) - it won't and can't work.

Aliases must all point to the SAME location, like

AR VAR BIT[7]
myBit VAR AR[2]


In the above case AR[2] and myBit are the one and same memory location, ie. two names for the same location, you can't do it the other way around. Ie you can't have one name for two locations which is basically what you're trying to do.

When you do

AR VAR BIT[7]

AR[1]=PORTB.6

You are reading the state of PORTB.6 and store the result of that in AR[1].

The only way I know of that is similar to what you're trying is to use a port offsets and a lookup table.

/Henrik.

CuriousOne
- 13th December 2013, 10:04
So,

PORTB.6=AR[1] won't work?

CuriousOne
- 13th December 2013, 10:08
Quite interesting, why it can't ?

The code example with LET & DIM is from Sinclair Basic, written in 1982 and running on Z80. It can handle such things, and picbasic pro - can't ?

HenrikOlsson
- 13th December 2013, 10:17
Hi,
Well, yes, it'll work.
It'll set the state of PortB.6 to what is stored in AR[1], either 1 or 0 at the time that the instruction executes.
What it will NOT do is create a "mirror" where the state of PortB.6 automatically changes whenever AR[1] changes - which I suspect is what you're looking for.

/Henrik.

CuriousOne
- 13th December 2013, 11:26
well,

say I can have

ledpin1 var PORTB.1

then when I issue

high ledpin1

it will make ledpin1 high, right?

so why I can't have array member aliased as port, so when I'll issue

high array[1]

since this previously was set to as alias of portb.1

portb.1 become high?

More simple.

I can have port name aliased to variable, and then I can change the value of variable and this change will change port state, too.

So, I can't have port name aliased to array variable?

HenrikOlsson
- 13th December 2013, 11:47
Hi,

well,
say I can have
ledpin1 var PORTB.1
then when I issue
high ledpin1
it will make ledpin1 high, right?
Yes, that's correct, it'll make PortB.1 high because "all" you've done is assigned the "name" or "identifier" ledpin1 to bit 1 of the register PortB. So when you do HIGH ledpin1 it'll "translate" to HIGH PortB.1 - see, ledpin1 and PortB.1 are one and the same.



so why I can't have array member aliased as port, so when I'll issue
high array[1]
since this previously was set to as alias of portb.1
portb.1 become high?
Beacuse PortB.1 is at one location in the memory map and Array[1] is at another - they are not one and the same.
When you create an array you are actually allocating space in RAM for that array as opposed to the previous example where you simply assigned a different "name" to an already existing register. Array[1] and PortB.1 are two different locations.

If you're example would work then one should be able to do
PortB VAR PortA
PortC VAR PortB
PortD VAR PortC
PortA = 0
and expect all ports to clear - it doesn't work that way because PortA is not the same register as PortB just as Array[1] not the same PortB.1 - they are two different things. On the other hand
myPort VAR PortA
myPort = 0
Will clear PortA because all you've done is to create an alternative name or identifier for an already existing register.

You can have multiple names for a single location.
You can not have one name for multiple location.

Does that make sense?

/Henrik.

CuriousOne
- 13th December 2013, 12:05
Ok, I understood the limitations. But why they are present here, and aren't present elsewhere?

CuriousOne
- 13th December 2013, 12:19
And is there a way to overcome them?

HenrikOlsson
- 13th December 2013, 13:08
Hi,

Ok, I understood the limitations. But why they are present here, and aren't present elsewhere?
Where are they not present? Can you show me an example in Sinclair BASIC that does what you want?


And is there a way to overcome them?
Yes and no.
There is, to my knowledge, no way to have a pin or port or any other register for that matter aliased into an array in a way that makes the pin/port automagically change when you write to the array. You need to write code that iterates thru the array and writes each port/pin individually and then call that routine each time you change the array. There might be a way (by specifying the absolute adress) to create a BIT array "overlaying" the port register adress space but then all the pins will be consecutive within the array, ie if Array[0] is PortB.0 then Array[1] will be PortB.1 and so on.

If you could explain a little bit what the purpose of this operation is perhaps someone will provide a workable solution.

/Henrik.

Amoque
- 13th December 2013, 13:31
Clearly I have erred, first in drawing assumptions and, second by posting untested code based on those assumptions. I dislike being wrong, but I dislike being ignorant even more. Ha! Is this not true for others as well?

So perhaps, it is germane to the discussion, will someone help me to understand my error? In the PBP manual, the statement SYMBOL is used to assign a second name to an existing variable - the process is described as aliasing; I presumed this meant then that both variable names would create pointers to the same RAM address - and be resolved during the assembly process. This misunderstanding was reinforced in the book PIC Basic Projects: 30 Projects Using PicBasic and PicBasic Pro by Dogan Ibrahim where he asserts on page 84:

"In order to make programs more readable, we can assign meaningful names to variables..." He uses the example: SYMBOL Count = B0. The author goes on to note: "This statement does not occupy any location in microcontroller RAM memory".

I accept Henrik's explanation; yet, I struggle to understand the purpose of SYMBOL and how it is used if not in this way.

As an aside, I also note that Curiousone did not make the SYMBOL assignments I suggested in the code he posted.

CuriousOne
- 13th December 2013, 13:54
SYMBOL AC[5] = PORTC.5

This statement is invalid, according to PBPpro.

HenrikOlsson
- 13th December 2013, 15:34
Hi,

SYMBOL is a left over from the BASIC Stamp and is not recommended.
In the BASIC Stamp there are a fixed number of pre-declared variables for you to use. B0 is one of those (a byte variable) and W0 is another (a word variable).

The statement SYMBOL Count = B0 simply creates an alias or a meaningful name to an already declared variable (the exact same thing as Count VAR B0 would) It's correct that it doesn't occupy any (new or more) space in RAM since it refers to an already declared variable (B0). What's important to remember here is that Count and B0 would be the exact same location RAM, translated to an absolute memory adress at compile time. (Well, yes the Basic Stamp is interpreted but anyway...)

(By the way, Count is a reserved word in PBP (and in the BasicStamp programming language) and has been for as long as I can remember so that book doesn't seem very well written but that's a side note).

Now, you can very well create an alias to a port, like myPort VAR PortB or to a pin, like myLED VAR PortB.0 - this works and there are no issues. The problem comes when you try to "alias" two different locations in memory to each other. I mean, I think it's pretty obvious that something like this wouldn't work right:

PortB.0 VAR PortC.0
PortB.0 = 1 ' Set both PortB.0 and PortC.0....Nope, won't work.

Again, AC VAR BIT[8] creates an array of 8 bits in memory (RAM). You can then, if you want, create meaningful names for the individual bits in that array by aliasing: AC_0 VAR AC[0] (which is not a very meaningful name but you get the idea.) In this case AC_0 refers to the exact same bit in memory as AC[0] does.

What you can not do is say AC[0] VAR PortB.0 or anything like that because (just as with PortB.0 VAR PortC.0 above) both the AC and PortB are already existing - at different memory locations in the PIC. They are two different entites, at two different adresses.

/Henrik.

If you think about it really is quite logical.

CuriousOne
- 13th December 2013, 16:04
OK.

Speaking simply, PORTB.1 is an alias, which refers to particular register or whatsoever, say at address F9 HEX.

So can I make such hex adresses into array, so when I write 1 to array member, which corresponds to F9 HEX, the associated hardware port becomes high?

HenrikOlsson
- 13th December 2013, 23:38
Hi,


Speaking simply, PORTB.1 is an alias, which refers to particular register or whatsoever, say at address F9 HEX.
Yes, PortB is simply an identifier which translates into an absolute adress, on the 16F877 PortB is at adress 0106h for example.


So can I make such hex adresses into array, so when I write 1 to array member, which corresponds to F9 HEX, the associated hardware port becomes high?
I've beem tryimg to explain why but I don't seem to get the point across so I'm going to revert to the simple answer of NO, you can't.

Again, please show me an example in Sinclair BASIC which does what you want.

/Henrik.

CuriousOne
- 14th December 2013, 04:44
I'm referring to portb.1 or whatsoever as a hardware endpoint, you - as program statement, this is the main difference.

Ok, here's example in sinclair basic, just consider that there's no HIGH/LOW statements, thres OUT statement, with syntax OUT X,Y, where X=portnumber, Y=portstate

Let's say, I have leds connected to ports 3,8,12,9, and I want to light them up in listed sequence.




DIM LEDS(4) 'declare array with 4 members
LET LEDS(1)=3
LET LEDS(2)=8
LET LEDS(3)=12
LET LEDS(4)=9 ' put port numbers into array

FOR A=1 to 4 ' make increment
OUT LEDS(A), 1' set high port number, stored in specific cell of LEDS array.
PAUSE 1 'wait some time, in sinclair basic, pause is in seconds, not ms
OUT LEDS(A), 0' turn led off
NEXT A ' loop

richard
- 14th December 2013, 08:39
var byte LEDS[4] 'declare array with 4 members
LEDS[0]=3
LEDS[1]=8
LEDS[2]=12
LEDS[3]=9 ' put port numbers into array

FOR A=0 to 3 ' make increment
high leds[A] 'set high port number, stored in specific cell of LEDS array. could try high portb.0[leds[a]]
PAUSE 1000 'wait some time,
low LEDS[A] ' turn led off might be low leds[A]
PAUSE 1000
NEXT


see port indexing this forum just a few days ago or page 30 of pbp manual



you could also use lookup

HenrikOlsson
- 14th December 2013, 08:45
Hi,

I'm referring to portb.1 or whatsoever as a hardware endpoint, you - as program statement, this is the main difference.
No, I'm referring to PortB.1 or whatever as an adress in "memory".

OK, that Sinclair example stores the adress of the port you're setting in the array and is not the same thing as you doing HIGH Array[1] in PBP even if it had worked.



myArray VAR BYTE[4]
myArray[0] = 2 ' Offset from PortA.0 to PortA.2
myArray[1] = 8 ' ....PortB.0
myArray[2] = 9 ' ....PortB.1
myArray[3] = 18 ' ....PortC.2

i VAR BYTE

Main:
for i = 0 to 3
PortA.0[myArray[i]] = 1
PAUSE 500
NEXT

FOR i = 0 to 3
PortA.0[myArray[i]] = 0
PAUSE 500
NEXT
Goto Main

/Henrik.

CuriousOne
- 14th December 2013, 08:48
Yes, it stores address, and then when reading from array, sets apropriate numbered port HIGH, by statement OUT.

OUT LEDS(A), 1

reads value stored in LEDS(A) array part, and then outputs "1" into port with that #.

CuriousOne
- 14th December 2013, 08:48
Thanks Richard, your example seems to do the thing I want to do. But I don't have PBP at hands, will have to check it on monday.