PDA

View Full Version : Just need to clarify...



tazntex
- 29th March 2010, 14:02
In the PBP manual under the REPEAT..UNTIL command it shows this example:
i = 0
REPEAT
PORTB.0[i] = 0
i = i + 1
UNTIL i > 7

I understand how it repeats until i is equal to 7 then moves on but what does the [i] in PORTB.0[i] do? That is the only example that I can find in the manual, other than where serial and the [] Versus is shown.

Thanks

Bruce
- 29th March 2010, 14:52
i is the bit index. PORTB.0[i] = 0 is PORTB.BIT[i] = 0. i increments from 0 to 7, so it clears
PORTB.0 to PORTB.7.

tazntex
- 29th March 2010, 15:01
Thanks for the quick reply Bruce,
So if I understand correctly, by
i = 0
REPEAT
PORTB.0[i] = 0
i = i + 1
UNTIL i > 7

i = i + 1 ' increments PORTB.0 thru 7 and
PORTB.0[i] = 0 ' places a 0 in each bit

Well, I'm good for the day, Learned something new.

Thanks again Bruce

Demon
- 29th March 2010, 19:20
i is the bit index. PORTB.0[i] = 0 is PORTB.BIT[i] = 0. i increments from 0 to 7, so it clears
PORTB.0 to PORTB.7.


I'm happy you posted this question tazntex. I never used the REPEAT instruction, but as a programmer I know I would have fallen into one of those see-what-you-want traps, something like this:

i = 0
REPEAT
PORTB.[i] = 0
i = i + 1
UNTIL i > 7

I would have looked at this code for hours before seeing where I went wrong.


(I wished we had a button that allowed us to use the CODE command, I can never remember the syntax.)

Heckler
- 30th March 2010, 03:08
I for one have called the good people over at MElabs and requested(suggested) that they spend more effort on better syntax examples in the manual. Why waste all those clean white pages? (They agreed, by the way;))

As I look at this example it still does not make sense to me.
I would have tried it the same as Robert, and banged my head against the wall for hours...

This one does not look intuitive at all.

Thanks for the heads up...

Dwight

rsocor01
- 30th March 2010, 07:00
Tazntex,

In your first post you said


I understand how it repeats until i is equal to 7 then moves on but what does the [i] in PORTB.0[i] do?

Technically speaking, it repeats until I=8 then it exits the loop. Of course, it won't execute the line PORT[I]=0 for I=8.

Robert

tazntex
- 30th March 2010, 14:22
Thanks goes to all for you help and interest.

MOUNTAIN747
- 30th March 2010, 17:45
While were on the subject of Port.Bits. I would like to use a random generator to set a single bit in an output port while working with an 8x8 dot matrix display. Is there any way to use a VARiable to set a RANDOM single Bit on an output port?

aratti
- 30th March 2010, 17:53
Is there any way to use a VARiable to set a RANDOM single Bit on an output port?

Ran_Word var word

Ini:
RANDOM Ran_Word
pause 100
PortB = Ran_Word.byte0
Pause 100
PortB = Ran_Word.byte1
Goto Ini

This code should suite your need.

Al.

MOUNTAIN747
- 30th March 2010, 18:05
Al, thanks for your replay. I ran your code. it sets the whole port at a random value. I need to figure out how to set a single bit within the port. I want to turn on a single led on the 8x8 display, then PAUSE, then loop to routine. Any idea how to single out one bit randomized?

aratti
- 30th March 2010, 18:18
I want to turn on a single led on the 8x8 display, then PAUSE, then loop to routine. Any idea how to single out one bit randomized?

This has been shown at the begining of the thread. Anyhow in your case:






Ran_Word var word
AA0 var Byte

Ini:
RANDOM Ran_Word
For AA0= 0 to 7
Pause 100
PortB.0[AA0] = Ran_Word.0[AA0]
pause 100
PortC.0[AA0] = Ran_Word.0[AA0 + 8]
Next AA0
goto Ini


In the above example I assume you are using portB and PortC, if not then change them as per your need.

Al.

Bruce
- 30th March 2010, 19:06
Try this;


X VAR WORD
OldVal VAR BYTE
PortVal VAR BYTE

TRISB = 0

Main:
RANDOM X
PortVal = DCD X.HighByte ' get bit #
IF PortVal = 0 THEN Main ' value should always be > 0
IF OldVal = PortVal THEN Main ' value should be different on every pass
PORTB = PortVal
OldVal = PortVal ' record old value for next pass
PAUSE 200
GOTO Main

END

MOUNTAIN747
- 30th March 2010, 19:45
Al, closer but not there yet. Sorry for the delay, I had to adjust your code to invert the Collumn port for this display. Bruce I haven't tested yours yet but I had started in that direction.

Bitgenerator:
Bvar=-0
random Ran_Word
Avar= Ran_Word.byte0
if Avar.0=1 then Bvar=Bvar+1
if Avar.1=1 then Bvar=Bvar+2
if Avar.2=1 then Bvar=Bvar+4 ;isolate a random number 0to7
Cvar=1
Cvar=Cvar<<Bvar ;set a single bit in the byte
Cvar=Cvar^$FF ;invert for a low on one bit
rcol=Cvar ;set port
Bvar=0
random Ran_Word
Avar= Ran_Word.byte0
if Avar.0=1 then Bvar=Bvar+1
if Avar.1=1 then Bvar=Bvar+2
if Avar.2=1 then Bvar=Bvar+4
Cvar=1
Cvar=Cvar<<Bvar
Row=Cvar
pause 100
goto Bitgenerator

(Wierd: code tag nor the editor would accept << in the code)


this method works (sorry you can't see all the code) but gives some repetition in general pattern on the diagonal. I will use the DCD. I'm still trying to understand Al's "Ran_Word.0[AA0 + 8]" . What is this doing to the PortC.0[AA0], is it resetting to a number less than 8?
thanks guys, I'll have to chew on this for a while.

Demon
- 31st March 2010, 01:16
I have 2 suggestions:

Taz, try to put a meaningful title to your thread; like REPEAT-UNTIL or something.

Mountain, you're always best to start a new thread when you have a new question (since this thread is about REPEAT UNTIL syntax).

It may not seem like much but it makes things so much easier to find stuff when you look at the list of threads.

MOUNTAIN747
- 31st March 2010, 15:11
Robert, thanks for the suggestions.