+ Reply to Thread
Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2003
    Location
    Australia
    Posts
    1,057

    Default Accessing bits of an array element

    Currently, this doesn't work:
    Arrayelement[0].bit5 = 0

    You can create aliases for every array element
    and access bits from there, but you can't do it
    in a loop because the bit number can't be
    a variable, and when you're accessing aliases,
    neither can the array index.

    One or the other would be an improvement.
    Last edited by Art; - 3rd March 2012 at 05:50.

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,259

    Default Re: Accessing bits of an array element

    Hi Art,
    I think this is what you're looking for:
    Code:
    ArrayElement.0[5] = 0    ' 6th bit in the firts byte
    ArrayElement.0[12] = 1   ' 5th bit in the second byte
    
    'And indexing...
    For i = 0 to 24
      ArrayElement.0[i] = 1
    NEXT
    For more details see Melanies post here. Also see section 7.6 in the PBP3 manual - if that's what you've got.

    /Henrik.

  3. #3
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,235

    Default Re: Accessing bits of an array element

    Quote Originally Posted by Art View Post
    Currently, this doesn't work:
    Arrayelement[0].bit5 = 0

    You can create aliases for every array element
    and access bits from there, but you can't do it
    in a loop because the bit number can't be
    a variable, and when you're accessing aliases,
    neither can the array index.

    One or the other would be an improvement.

    I think this is what Art meant to do:
    Code:
    Temp = Arrayelement[0]
    Temp.5 = 0
    Arrayelement[0] = Temp
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  4. #4
    Join Date
    Aug 2003
    Location
    Australia
    Posts
    1,057

    Default Re: Accessing bits of an array element

    Didn't mean to do represent any particular problem in that example,
    but thanks for the solution!

  5. #5
    Join Date
    Mar 2005
    Location
    CROATIA
    Posts
    40

    Default Re: Accessing bits of an array element

    Hi there,

    I use large array for led display display matrix, and until now, for small matrix, all work great
    but as I increased dimension, I cant break limit for bit access method matrix.0(x), actually
    if i use x variable work only as byte, x<256, if i use constant number works for greater than byte
    my question is, is there way to fix pic 18 lib array part in some way to get matrix.0(word-variable) to work.
    ( i already have split to more array, so splitting into more than current 8 arrays is not an option for me)


    ;************************************************* ***************
    ;* Array routines *
    ;************************************************* ***************

    ;************************************************* ***************
    ;* ARRAYBIT : Translate array to FSR and bit *
    ;* *
    ;* Input : FSR0 = register *
    ;* : Carry, W = bit index (0 - 262) *
    ;* Output : FSR0 = register *
    ;* : R4 = bit mask *
    ;* : Z = bit state *
    ;* *
    ;* Notes : *
    ;************************************************* ***************

    ifdef ARRAYBIT_USED
    LIST
    ARRAYBIT movwf R4 ; Save bit index
    andlw 0f8h ; Clear bottom 3 bits
    rrcf WREG, W ; / 8 (roll C into top)
    rrcf WREG, W
    rrcf WREG, W
    addwf FSR0L, F ; Add /8 to register
    btfsc STATUS, C
    incf FSR0H, F
    call CONVBIT ; Change bit number in R4 to mask
    movwf R4 ; Save it
    andwf INDF0, W ; Bit to Z while we're here
    goto DUNN
    NOLIST
    CONVBIT_USED = 1
    DUNN_USED = 1
    endif


    i mean fixing this, and part below which is needed for handling arrays

    Regards

  6. #6
    Join Date
    Sep 2009
    Posts
    774

    Default Re: Accessing bits of an array element

    Just an idea...
    Code:
    ArrayA VAR BYTE[1000]
    BitNo VAR WORD 
    ByteInArray VAR WORD
    BitInByte VAR BYTE
    TmpByte VAR BYTE
    BitValue VAR BIT
    
    GetBit: 'Input Array,BitNo; Output BitValue
    ByteInArray=BitNo/8
    TmpByte=ArrayA[ByteInArray]
    BitInByte=BitNo//8
    BitValue=TmpByte.0[BitInByte]
    RETURN
    
    SetBit :'Input Array, BitNo,BitValue; Output: Array
    ByteInArray=BitNo/8
    TmpByte=ArrayA[ByteInArray]
    BitInByte=BitNo//8
    TmpByte.0[BitInByte]=BitValue
    ArrayA[ByteInArray]=TmpByte
    RETURN

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