PDA

View Full Version : Looping through MyVar.n - will this work?



kjavrd
- 15th November 2012, 22:44
Hello all.

I dug around a bit and couldn't find a solid answer. I'm hoping this is a quick answer from someone.

Can I loop through the bit position in a variable by using another variable to represent the bit position?

For example,

I know I can do this:


MyVar.0 = 1
MyVar.1 = 1
MyVar.2 = 0 ... etc



I want do this:


n=0

MyVar.n = 1

... later in the loop, n is 5, so

MyVar.n = 0

etc.


I've compiled it and PBP doesn't seem to like it.

Much appreciated.

andywpg
- 15th November 2012, 23:07
Hello all.

I dug around a bit and couldn't find a solid answer. I'm hoping this is a quick answer from someone.

Can I loop through the bit position in a variable by using another variable to represent the bit position?

For example,

I know I can do this:


MyVar.0 = 1
MyVar.1 = 1
MyVar.2 = 0 ... etc



I want do this:


n=0

MyVar.n = 1

... later in the loop, n is 5, so

MyVar.n = 0

etc.


I've compiled it and PBP doesn't seem to like it.

Much appreciated.

I had much the same question the other day - only with an I/O port. I think this will do it:


MyVar.0[n] = 0

As always, I stand to be corrected

kjavrd
- 15th November 2012, 23:47
I found the answer, as usual thanks to Melanie.

http://www.picbasic.co.uk/forum/showthread.php?t=544&p=2017#post2017

she says.....


1. How can I reference a BIT in a BYTE?

We know that MyByte.0 references Bit 0 of MyByte, all the way up to MyByte.7 referencing Bit 7 of MyByte… but there’s another way of accessing the bits in a Byte (MyVar is a BYTE variable)…

MyByte.0(MyVar)

When MyVar is in the range 0-7, it will access Bits 0-7 of MyByte.

Example to display all eight BITS of a BYTE sequentially, Bit at a time…

For MyVar=0 to 7
MyBit=MyByte.0(MyVar)
LCDOut $FE,1,"Bit ",#MyVar,"=",#MyBit
Pause 2000
Next MyVar



Thanks. :-)