PDA

View Full Version : Nested For Next Loop Demo



Archangel
- 8th June 2009, 08:57
Some of you will find this useful especially when sorting bytes for a scrolling matrix or GLcd.
The only "secret" here is the innermost loop has to execute first and the first must finish last.



'************************************************* ***************
'* Name : Nested ForNextDemo.BAS *
'* Author : [Joe S. ] *
'* Notice : Copyright (c) 2009 [ ] *
'* : All Rights Reserved *
'* Date : 6/7/2009 *
'* Version : 1.0 *
'* Notes :MPASM Assembler, PIC 16F690 *
'* : *
'************************************************* ***************

@ __config _INTOSCIO & _WDT_ON & _PWRTE_ON & _MCLRE_ON & _CP_OFF

DEFINE OSC 4
include "modedefs.bas"
ANSEL = 0
ANSELH = 0
CM1CON0 = 0
CM2CON0 = 0


index var byte
index2 var byte
index3 var byte

value var byte
value2 var byte
value3 var word

array var byte[6]


pause 50
serout PortB.7,T2400,[254,1] 'Cleasr LCD
serout PortB.7,T2400,[254,2,"Nested For Next Demo"]
pause 1000
serout PortB.7,T2400,[254,1] 'Cleasr LCD
loop:
Value = 0
Value2 = 0
Value3 = 0 ' Perform for next counters nested 3 levels deep

for index = 1 to 10 step 1 ' Do whole routine 10 times, count

lookup index,[0,"0123456789"],value
pause 1
serout PortB.7,T2400,[$FE,2,"value ",value]

for index2 = 1 to 8 step 1 'do this count 8 times in each of the 10 loops
lookup index2,[0,"0123456789"],value2
pause 1
serout PortB.7,T2400,[254,$C0,"value2 ",value2]


for index3 = 1 to 5 step 1 'do this 5 times in each of the 80 loops
lookup index3,[0,"0123456789"],value3
pause 2
serout PortB.7,T2400,[254,$94,"value3 ",value3]
pause 100 ' this delay gives you time to see the numbers change
' increase this to slow counters
next index3 ' this next goes first as it was the last to start


next index2 ' started in the middle, it stays in the middle

next index ' "the first will be last and the last will be first" . . . God.

index = 0
for index = 0 to 5 'Index is the pointer used in the array and the counter
array[index] = index + 48 'convert to ASCII & write zero to 5 into index and transfer ASCII to array
serout PortB.7,t2400,[254,$D4,"array is ",array[index]] 'read from array and display on line 4
pause 500
next index

serout PortB.7,T2400,[254,1] 'Cleasr LCD
goto loop ' do for as long as there is power

end