"Variable already an alias" - is there a way to avoid this?


Closed Thread
Results 1 to 33 of 33

Hybrid View

  1. #1
    Join Date
    Feb 2013
    Posts
    1,078

    Default "Variable already an alias" - is there a way to avoid this?

    Say I want to have following code, when on specific condition, either of subroutines will be called.

    Code:
    '1ST DIGIT
    
    
    DG1:
    AX VAR PORTD.3:BX VAR PORTC.4:CX VAR PORTC.1:DX VAR PORTC.2: EX VAR PORTD.0
    FX VAR PORTD.2: GX VAR PORTC.5
    RETURN
    
    
    '2ND DIGIT
    
    
    DG2:
    AX VAR PORTD.4: BX VAR PORTD.5:CX VAR PORTA.7: DX VAR PORTA.6	
    EX VAR PORTC.0:GX VAR PORTC.6: FX VAR PORTC.7	
    RETURN
    (and so on, for 2 more digits)

    But it won't compile, giving above mentioned error. I can of course avoid this, by introducing additional variables for each digit, like having AX, AX2, AX3, AX4, but this is just waste of memory, I wanted to do a shortcut, but as it seems, that does not works?

  2. #2
    Join Date
    Apr 2014
    Location
    OK
    Posts
    557


    Did you find this post helpful? Yes | No

    Default Re: "Variable already an alias" - is there a way to avoid this?

    Try:
    AX CON PORTD.3

  3. #3
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,520


    Did you find this post helpful? Yes | No

    Default Re: "Variable already an alias" - is there a way to avoid this?

    Variables, aliases and constants are allocated/defined/whatever at compiletime - not runtime. So when the compiler hits the DG2 subroutine it sees you want a variable/alias called AX but it can't do that for you since you allready have a variable/alias with that exact name - and since it's a compile time thing it can't re-define it at runtime.

    I can of course avoid this, by introducing additional variables for each digit, like having AX, AX2, AX3, AX4, but this is just waste of memory,
    No, not in this case since the names AX, AX2, AX3 etc are just aliases/pointers to already existing registers, namely PORTx.y and so on - so no wase of memory in this particular case.

    You can have several aliases pointing to the same address but you can't have one alias pointing to different addresses - how's the compiler gonna know when to use which?

    I suppose you could use an array containing the offset from, lets say PortA.0. The first element would be your AX, second element would be AX1 and so on. In each subroutine you then populate that array differently depending on "where" you want AX, AX1 etc to point.

  4. #4
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

    Default Re: "Variable already an alias" - is there a way to avoid this?

    Yes I see.
    Even this does not works:

    IF X=1 then
    Y var PORTA.0
    else
    Y var PORTA.1

    So this is just compiler limitation, right?

    For the arrays, can be port name in it?

  5. #5
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

    Default Re: "Variable already an alias" - is there a way to avoid this?

    I have 4 piece of 7 segment led displays connected directly to MCU pins, via 220 ohm resistor, common anode going to Vdd. I wrote a routine, which allows to display any number (X) on any digit (Y). But it is taking about 2.7k of memory, mostly because I can not redefine variables on the fly. So my main goal was to shorten this code somehow.

    Code:
    SMART:   'DECODER ROUTINE
    FOR X=0 TO 9
    IF X=0 AND Y=1 THEN GOSUB N10 'displays specific digit for specific segment
    IF X=1 AND Y=1 THEN GOSUB N11
    IF X=2 AND Y=1 THEN GOSUB N12
    IF X=3 AND Y=1 THEN GOSUB N13
    IF X=4 AND Y=1 THEN GOSUB N14
    IF X=5 AND Y=1 THEN GOSUB N15
    IF X=6 AND Y=1 THEN GOSUB N16
    IF X=7 AND Y=1 THEN GOSUB N17
    IF X=8 AND Y=1 THEN GOSUB N18
    IF X=9 AND Y=1 THEN GOSUB N19
    
    IF X=0 AND Y=2 THEN GOSUB N20
    IF X=1 AND Y=2 THEN GOSUB N21
    IF X=2 AND Y=2 THEN GOSUB N22
    IF X=3 AND Y=2 THEN GOSUB N23
    IF X=4 AND Y=2 THEN GOSUB N24
    IF X=5 AND Y=2 THEN GOSUB N25
    IF X=6 AND Y=2 THEN GOSUB N26
    IF X=7 AND Y=2 THEN GOSUB N27
    IF X=8 AND Y=2 THEN GOSUB N28
    IF X=9 AND Y=2 THEN GOSUB N29
    
    IF X=0 AND Y=3 THEN GOSUB N30
    IF X=1 AND Y=3 THEN GOSUB N31
    IF X=2 AND Y=3 THEN GOSUB N32
    IF X=3 AND Y=3 THEN GOSUB N33
    IF X=4 AND Y=3 THEN GOSUB N34
    IF X=5 AND Y=3 THEN GOSUB N35
    IF X=6 AND Y=3 THEN GOSUB N36
    IF X=7 AND Y=3 THEN GOSUB N37
    IF X=8 AND Y=3 THEN GOSUB N38
    IF X=9 AND Y=3 THEN GOSUB N39
    
    IF X=0 AND Y=4 THEN GOSUB N40
    IF X=1 AND Y=4 THEN GOSUB N41
    IF X=2 AND Y=4 THEN GOSUB N42
    IF X=3 AND Y=4 THEN GOSUB N43
    IF X=4 AND Y=4 THEN GOSUB N44
    IF X=5 AND Y=4 THEN GOSUB N45
    IF X=6 AND Y=4 THEN GOSUB N46
    IF X=7 AND Y=4 THEN GOSUB N47
    IF X=8 AND Y=4 THEN GOSUB N48
    IF X=9 AND Y=4 THEN GOSUB N49
    
    PAUSE 300
    NEXT
    Y=Y+1
    IF Y>4 THEN Y=1
    GOTO SMART 
    
    N10: '0 at 1st digit
    GOSUB CLAR1
    LOW A1: LOW B1: LOW C1: LOW D1: LOW E1: LOW F1 '0
    return
    
    N11: '1 AT 1ST DIGIT
    GOSUB CLAR1
    low b1: low c1 '1
    RETURN
    
    N12: 
    GOSUB CLAR1
    low A1: low B1: low g1: low e1: low d1 '2
    RETURN
    
    N13:
    GOSUB CLAR1
    low A1: LOW B1: LOW C1: LOW D1: LOW G1 '3
    RETURN
    
    N14:
    GOSUB CLAR1
    LOW F1: LOW B1: LOW G1: LOW C1 '4
    RETURN
    
    N15:
    GOSUB CLAR1
    LOW A1: LOW F1: LOW G1: LOW C1: LOW D1 '5
    RETURN
    
    N16:
    GOSUB CLAR1
    LOW F1: LOW A1: LOW G1: LOW E1: LOW D1: LOW C1 'C6
    RETURN
    
    N17:
    GOSUB CLAR1
    low A1: low B1: low C1 '7
    RETURN
    
    N18:
    gosub CLAR1
    low A1: low B1: low C1: low f1: low e1: low g1: low d1 '8
    RETURN
    
    N19:
    gosub CLAR1
    low A1: low B1: low C1: low f1: low g1: low d1 '9
    RETURN
    
    CLAR1: 'clear 1ST DIGIT
    high A1: high B1: high C1: high d1: high e1: high f1: high g1
    return
    
    N20: '0 at 1st digit
    GOSUB CLAR2
    LOW A2: LOW B2: LOW C2: LOW D2: LOW E2: LOW F2 '0
    return
    
    N21: '1 AT 1ST DIGIT
    GOSUB CLAR2
    low b2: low c2 '1
    RETURN
    
    N22: 
    GOSUB CLAR2
    low A2: low B2: low g2: low e2: low d2 '2
    RETURN
    
    N23:
    GOSUB CLAR2
    low A2: LOW B2: LOW C2: LOW D2: LOW G2 '3
    RETURN
    
    N24:
    GOSUB CLAR2
    LOW F2: LOW B2: LOW G2: LOW C2 '4
    RETURN
    
    N25:
    GOSUB CLAR2
    LOW A2: LOW F2: LOW G2: LOW C2: LOW D2 '5
    RETURN
    
    N26:
    GOSUB CLAR2
    LOW F2: LOW A2: LOW G2: LOW E2: LOW D2: LOW C2 'C6
    RETURN
    
    N27:
    GOSUB CLAR2
    low A2: low B2: low C2 '7
    RETURN
    
    N28:
    gosub CLAR2
    low A2: low B2: low C2: low f2: low e2: low g2: low d2 '8
    RETURN
    
    N29:
    gosub CLAR2
    low A2: low B2: low C2: low f2: low g2: low d2 '9
    RETURN
    
    CLAR2: 'clear 1ST DIGIT
    high A2: high B2: high C2: high d2: high e2: high f2: high g2
    return
    
    N30: '0 at 1st digit
    GOSUB CLAR3
    LOW A3: LOW B3: LOW C3: LOW D3: LOW E3: LOW F3 '0
    return
    
    N31: '1 AT 1ST DIGIT
    GOSUB CLAR3
    low b3: low c3 '1
    RETURN
    
    N32: 
    GOSUB CLAR3
    low A3: low B3: low g3: low e3: low d3 '2
    RETURN
    
    N33:
    GOSUB CLAR3
    low A3: LOW B3: LOW C3: LOW D3: LOW G3 '3
    RETURN
    
    N34:
    GOSUB CLAR3
    LOW F3: LOW B3: LOW G3: LOW C3 '4
    RETURN
    
    N35:
    GOSUB CLAR3
    LOW A3: LOW F3: LOW G3: LOW C3: LOW D3 '5
    RETURN
    
    N36:
    GOSUB CLAR3
    LOW F3: LOW A3: LOW G3: LOW E3: LOW D3: LOW C3 'C6
    RETURN
    
    N37:
    GOSUB CLAR3
    low A3: low B3: low C3 '7
    RETURN
    
    N38:
    gosub CLAR3
    low A3: low B3: low C3: low f3: low e3: low g3: low d3 '8
    RETURN
    
    N39:
    gosub CLAR3
    low A3: low B3: low C3: low f3: low g3: low d3 '9
    RETURN
    
    CLAR3: 'clear 1ST DIGIT
    high A3: high B3: high C3: high d3: high e3: high f3: high g3
    return
    
    N40: '0 at 1st digit
    GOSUB CLAR4
    LOW A4: LOW B4: LOW C4: LOW D4: LOW E4: LOW F4 '0
    return
    
    N41: '1 AT 1ST DIGIT
    GOSUB CLAR4
    low b4: low c4 '1
    RETURN
    
    N42: 
    GOSUB CLAR4
    low A4: low B4: low g4: low e4: low d4 '2
    RETURN
    
    N43:
    GOSUB CLAR4
    low A4: LOW B4: LOW C4: LOW D4: LOW G4 '3
    RETURN
    
    N44:
    GOSUB CLAR4
    LOW F4: LOW B4: LOW G4: LOW C4 '4
    RETURN
    
    N45:
    GOSUB CLAR4
    LOW A4: LOW F4: LOW G4: LOW C4: LOW D4 '5
    RETURN
    
    N46:
    GOSUB CLAR4
    LOW F4: LOW A4: LOW G4: LOW E4: LOW D4: LOW C4 'C6
    RETURN
    
    N47:
    GOSUB CLAR4
    low A4: low B4: low C4 '7
    RETURN
    
    N48:
    gosub CLAR4
    low A4: low B4: low C4: low F4: low E4: low G4: low D4 '8
    RETURN
    
    N49:
    gosub CLAR4
    low A4: low B4: low C4: low F4: low G4: low D4 '9
    RETURN
    
    CLAR4: 'clear 1ST DIGIT
    high A4: high B4: high C4: high D4: high E4: high F4: high G4
    return

  6. #6
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,520


    Did you find this post helpful? Yes | No

    Default Re: "Variable already an alias" - is there a way to avoid this?

    Yes I see.
    Even this does not works:
    Code:
     IF X=1 then
     Y var PORTA.0
     else
     Y var PORTA.1
    You have to understand that the "code" Y VAR PORTA.0 isn't something that is actually being executed on the PIC itself. It's simply a way to inform the compiler that you, in your code, want bit 0 of PortA to be "named" Y. And since it IS something that the compiler handles when it converts your program from PBP into ASM (which is then assembled by the assembler) it can not later be changed by the running program.

    So this is just compiler limitation, right?
    You can call it a limitation if you like but it's just the way it works.

    For the arrays, can be port name in it?
    No. But you can do this:
    Code:
    idx VAR BYTE
    idx = 9
    PortA.0[idx] = 1
    Here, idx acts as an offset from PortA.0
    If you look at the datasheet for the PIC you'll find that the Port registers are consecutive in the memory map so by counting 9 bits from bit 0 of PortA.0 you'll end up PortB.1.

    So with this method you can use an Array to hold your seven offsets and redifine those offsets at will, something like this (untested and uncompiled, treat it accordingly):
    Code:
    X VAR BYTE[7]     ' Array to hold the bit offset from PortA.0
    idx VAR BYTE       ' Temporary variable needed to actually use the indexing
    
    Main:
    
    GOSUB DG1
      idx = X[2]            ' Since X[2] contains the value 17, so will idx
      PortA.0[idx] = 1    ' Write  to "PortA.17" which in real life will be PortC.1
    
    GOSUB DG2
      idx = X[2]            ' Now that we've changed the values in the X-Array, X[2] contains the value 18 and so will idx
      PortA.0[idx] = 1   ' Write to PortC.2
    
    END
    
    DG1:
      X[0] = 27  ' AX VAR PORTD.3
      X[1] = 20  ' BX VAR PORTC.4
      X[2] = 17  'CX VAR PORTC.1
      X[3] = 18  'DX VAR PORTC.2
      X[4] = 24  'EX VAR PORTD.0
      X[5] = 26  'FX VAR PORTD.2
      X[6] = 21  'GX VAR PORTC.5
    RETURN
    
    DG2:
      X[0] = 28  ' AX VAR PORTD.4
      X[1] = 21  ' BX VAR PORTC.5
      X[2] = 18  'CX VAR PORTC.2
      X[3] = 19  'DX VAR PORTC.3
      X[4] = 25  'EX VAR PORTD.1
      X[5] = 27  'FX VAR PORTD.3
      X[6] = 22  'GX VAR PORTC.6
    RETURN

  7. #7


    Did you find this post helpful? Yes | No

    Default Re: "Variable already an alias" - is there a way to avoid this?

    We aren't looking for fencing on an electronics forum.

  8. #8
    Join Date
    Apr 2014
    Location
    OK
    Posts
    557


    Did you find this post helpful? Yes | No

    Default Re: "Variable already an alias" - is there a way to avoid this?

    Quote Originally Posted by peterdeco1 View Post
    We aren't looking for fencing on an electronics forum.
    He made one other seemingly useless post with his fence company link embedded. I deleted this one. Maybe I should have deleted the other one. At least in the other one his fence link looked like a signature. Had I seen this one first, I would have just got rid of him.

  9. #9
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,802


    Did you find this post helpful? Yes | No

    Default Re: "Variable already an alias" - is there a way to avoid this?

    In the past was not in the annoying side of spams. Checked the previous posts and cannot decide to kick him/her out... We'll keep our eye on..

    Ioannis

  10. #10
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

    Default Re: "Variable already an alias" - is there a way to avoid this?

    By the way, is there a way to arraywrite to predefined area of array?

    arraywrite topline, ["mose"]

    writes to 0 to 3 positions of array, but say I want to write this text from 4 to 7 positions, how should I?
    I can add something like arraywrite topline, [1,2,3,4,"mose"], but this will delete previous content of previous array members, and I want to avoid this. Any way?

  11. #11
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,802


    Did you find this post helpful? Yes | No

    Default Re: "Variable already an alias" - is there a way to avoid this?

    SKIP 4 as a modifier maybe?

    Ioannis

Similar Threads

  1. How to do the "SerIN" and "SerOut " for the usb ?
    By vicce67 in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 13th March 2015, 02:01
  2. Replies: 0
    Last Post: - 14th November 2013, 03:32
  3. Replies: 3
    Last Post: - 15th October 2012, 08:06
  4. AN Question for "word" variable read The serial port
    By redfoen in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 14th December 2007, 17:39
  5. Replies: 1
    Last Post: - 16th February 2005, 20:05

Members who have read this thread : 1

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