PDA

View Full Version : Accessing bits of an array element



Art
- 3rd March 2012, 06:47
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.

HenrikOlsson
- 3rd March 2012, 10:43
Hi Art,
I think this is what you're looking for:


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 (http://www.picbasic.co.uk/forum/showthread.php?t=544). Also see section 7.6 in the PBP3 manual - if that's what you've got.

/Henrik.

sayzer
- 3rd March 2012, 11:24
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:


Temp = Arrayelement[0]
Temp.5 = 0
Arrayelement[0] = Temp

Art
- 3rd March 2012, 11:48
Didn't mean to do represent any particular problem in that example,
but thanks for the solution! :)

ShoKre
- 13th June 2014, 14:07
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

pedja089
- 14th June 2014, 13:11
Just an idea...

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