PDA

View Full Version : Bit variables... typo?



Tomexx
- 26th November 2004, 15:04
This snippet below is taken from MElabs website:
http://www.melabs.com/resources/articles/pinaccess.htm

----------------------------------------------------------------------------------
Specifying bits with variables

Sometimes it is desirable to specify the bit with a variable in your program. This allows you to write to different bits, based on some condition in your code.

Let's write a code snippet which will count 0 to 7 and make the corresponding bit on PORTB high for each count. To do this, we will use a bit offset.
----------------------------------------------------------------------------------



i var byte ' i will hold our count
TRISB = %00000000 'set all PORTB pins as output

For i = 0 To 7 'loop for 8 counts

PORTB = %00000000 'make all pins low

PORTB.0[i] = 1 'make pin number i high

Pause 500 'pause for half a second

Next i 'go to the next count


Notice the PORTB.0[i] line is that a typo?
If not why is the "[i]" located next to ".0"?
Please explain how it works

Thanks,
Tom

mister_e
- 29th November 2004, 00:07
Notice the PORTB.0[i] line is that a typo?
If not why is the "[i]" located next to ".0"?
Please explain how it works


one things is stupid frome MELABS, is not allowing to write...

PORTA.i

anyway, we can consider PORTB.0[i] as a kinda typo.

Tomexx
- 29th November 2004, 00:36
Too bad...I was hoping to use it in an array.

Anyway, so if we can't use an array here then what's the point of the example?

Thanks,
Tom

mister_e
- 29th November 2004, 00:39
Depending of what you want to do with an array... what do you plan to do?

Tomexx
- 29th November 2004, 03:21
Nothing particular in this case. Just that the example said that you can specify the bit with a variable in your program which I didn't see in the manual. I thought that would be usefull sometimes. But I didn't understand the syntax.


PORTB.0[i] = 1


Going back to the example they probably meant this:


PORTB.[i] = 1

...so I guess you can use it in an array otherwise they wouldn't show it, right?

Anyway, I'm pretty new to the PBP and try to pick up new things when I see them. Also PBP is very limited compared to PC languages so every little trick helps.

mister_e
- 29th November 2004, 03:33
Ok, i'm not sure what you know and what you don't so here's fulll explanation.

PORTB.0=1

high level state to PORTB bit 0 (RB0). This is doing the same thing as HIGH PORTB.0

PORTB.0[i]=1

high level state to PORTB bit specify by i variable

So let's say i=4, this will place PORTB bit 4 (RB4) to high level.

i don't see lots of appliance for this function since i always use specific name to access an PORT pin.

hope this put some lights

regards

Melanie
- 29th November 2004, 18:11
It's all here if you search for it...

http://www.picbasic.co.uk/forum/showthread.php?s=&threadid=544