Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

for next question, 1 to far

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-AVR Archive
View previous topic :: View next topic  
Author Message
hast66

Bascom Member



Joined: 18 Aug 2005
Posts: 28

PostPosted: Thu May 24, 2007 6:54 pm    Post subject: for next question, 1 to far Reply with quote

Hi
Code:

Dim X As Byte , Rood As Byte

For X = 0 To 255
Rood = X
Next

End                                     'end program
 

this gives as final result: rood = 255 and X=0

seems not logical to me, X should go to 0 and not further

now I adjust x after the next instruction, but if I use interupts I have a problem since rood<>x

any comments/solutions?
Back to top
View user's profile
rileyesi

Bascom Member



Joined: 19 Dec 2006
Posts: 398

blank.gif
PostPosted: Thu May 24, 2007 7:43 pm    Post subject: Reply with quote

Hi.

You need to define the step size for your FOR NEXT loop as well as tell the program which variable you want to NEXT.

Try this code:

Code:

Dim X As Byte , Rood As Byte

For X = 0 To 255 Step 1
Rood = X
Print "X= " ; X ; "  Rood= " ; Rood
Waitms 10
Next X
 


I like to use PRINT statements to help debug. If you run this, you will see that both X and Rood go from 0 to 255.

Hope this helps.

Regards,

Pete
Back to top
View user's profile
rileyesi

Bascom Member



Joined: 19 Dec 2006
Posts: 398

blank.gif
PostPosted: Thu May 24, 2007 7:53 pm    Post subject: Reply with quote

Hi.

Actually, I ran your code in the simulator (I did not before my last post) and it works fine. Add a print statement and you will see it count up.

Code:

Dim X As Byte , Rood As Byte

For X = 0 To 255
Rood = X
Print X ; Rood
Next
 


Pete[/quote]
Back to top
View user's profile
hast66

Bascom Member



Joined: 18 Aug 2005
Posts: 28

PostPosted: Fri May 25, 2007 10:18 am    Post subject: Reply with quote

thanks for the reaction

I also ran it on the simulator. I monitor the variables (wich updates the display of it, only after a pause or step buttonclick).
Now if you let the code finish and then click pause or step, you should see my results.
After the last time Next is executed, X goes 1 step further.

Also if it goes down: For x = 255 to 0 step -1.
X ends at 255
Back to top
View user's profile
Luciano

Bascom Member



Joined: 29 Nov 2004
Posts: 3149
Location: Italy

blank.gif
PostPosted: Fri May 25, 2007 12:25 pm    Post subject: Reply with quote

kolonelvonklink wrote:
After the last time Next is executed, X goes 1 step further.

Hi,

This is normal, without that, how do you check if it is time to
exit the FOR loop? This is the same for any other programming
language.

* * *


From your first post:
kolonelvonklink wrote:
but if I use interupts I have a problem since rood<>x

Code:
Dim X As Byte , Rood As Byte

For X = 0 To 255
  Rood = X
Next

End  


While the FOR loop is executed:

If the interrupt occurs just before "Rood = X", rood and x will be different.
If the interrupt occurs just after "Next", rood and x will be different.

Best regards,

Luciano
Back to top
View user's profile
oe9vfj

Moderator



Joined: 17 Jun 2004
Posts: 269
Location: Austria, Hard

austria.gif
PostPosted: Fri May 25, 2007 1:34 pm    Post subject: Reply with quote

This effort depends, how FOR ... NEXT work.
In the FOR X = 0 TO 255
x is incremented by 1. If the new value is greater then highest desired value, the FOR ... NEXT loop ends and leaves X with the value of STEP (1) higher then the TO Value.
If you want, that X have the last value in the loop, you can
DECR X
after the NEXT statement

_________________
regards Josef

DOS - File System for BASCOM-AVR on http://members.aon.at/voegel
Back to top
View user's profile Visit poster's website
hast66

Bascom Member



Joined: 18 Aug 2005
Posts: 28

PostPosted: Sat May 26, 2007 6:51 pm    Post subject: Reply with quote

Still this seems contra-intuitive to me. I would write a compiler to leave the loop when x equals the TO value so after the loop x still equals the TO value and not one more (or less).
isn't this some kind of bug Confused

PS the interrupt problems could easely be solved by disable en enable this interrupt in the loop
Back to top
View user's profile
lpa

Bascom Member



Joined: 12 Dec 2006
Posts: 27
Location: Oreokastro, Greece

greece.gif
PostPosted: Sun May 27, 2007 7:26 am    Post subject: Reply with quote

you can use the following code if you want to preserve the value of X

Code:
Dim X As Byte , Rood As Byte, Limit as Byte

Limit=255

For X = 0 To Limit

   Rood = X
   If X=Limit then  
      exit For
   end if
Next

End
Back to top
View user's profile Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-AVR Archive All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum