It is currently Wed May 22, 2013 4:19 am

All times are UTC + 10 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Tutorial 1 help please
PostPosted: Tue Aug 03, 2010 7:36 am 
Offline
decided to stick around...
decided to stick around...

Joined: Tue Jun 08, 2010 12:14 pm
Posts: 32
Hey everyone, i just started programming about a week ago so don't be surprised if i'm doing something completely wrong.

I'm trying to make a led stay on longer than its stays off and this is my approach.

Please tell me if my approach would work.

Thanks
Anthony

Code:
begin                              
   bsf PORTB, 0         
   call ledon             
   bcf PORTB, 0         
   call ledoff             
   goto begin            
   


ledon                 
   movlw d'255'   
   movwf delay_1            
   movwf delay_2   
   movlw d'255'
   movwf delay_3
   
   
delay_loopon            
   decfsz delay_1, f      
      goto delay_loopon         
   decfsz delay_2, f         
      goto delay_loopon      
   decfsz delay_3, f
      goto delay_loopon
   return                  

ledoff
   movlw d'255'            
   movwf delay_4            
   movwf delay_5

delay_loopoff            
   decfsz delay_4, f      
      goto delay_loopoff      
   decfsz delay_5, f         
      goto delay_loopoff      
   return                  


   end   


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 03, 2010 6:41 pm 
Offline
Site Admin
Site Admin
User avatar

Joined: Fri Mar 26, 2010 10:30 pm
Posts: 1860
I guess you could look at making the code a little more straight forward / more efficient. Something like this perhaps:

Code:
begin
bsf PORTB, 0
movlw d'05'
call delay
bcf PORTB, 0
movlw d'01'
call delay
goto begin

delay
movwf delay_0
movlw d'255'
movwf delay_1
movwf delay_2
delay_loop
decfsz delay_2, f
goto delay_loop
decfsz delay_1, f
goto delay_loop
decfsz delay_0, f
goto delay_loop
return


So you can see above that before we actually call the delay, we prepare a decimal number ready to copy into one of our delay variables (delay_0) the other two variables are then set with 255 (which we actually don't need to do but it helps with the understanding of the code) So really we are counting down from 255, 255 times and then doing it again either once or five times.

The reason that we don't need to copy 255 into delay_1 and delay_2 is because lets say that they are already zero. The instruction decfsz actually first takes one away from the variable and THEN it checks if it is zero (not the other way around) so if we already start from zero and then we take one away - we end up at 255.

I can explain this further but just let me know how you go with it
:)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 06, 2010 4:24 am 
Offline
decided to stick around...
decided to stick around...

Joined: Tue Jun 08, 2010 12:14 pm
Posts: 32
Sorry for the late reply, i've been pretty busy lately.

So i guess i'm confused about the order you place some of your code in. Does it matter if you place movwf delay_2 before movwf delay_1. Also, does it matter if you place decfsz delay_1 before decfsz delay_2.

The reason i ask this question is because in the delay subroutine you put delay_1 before delay_2 and in the delay_loop subroutine you put delay_2 before delay delay_1.

Thanks
Anthony


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 06, 2010 6:11 am 
Offline
Site Admin
Site Admin
User avatar

Joined: Fri Mar 26, 2010 10:30 pm
Posts: 1860
It does not matter at all.

The compiler does not care what we call our variables. It is certainly not looking for a lower number to come first.

Instead of calling our delay variables delay_0 delay_1 and delay_2

we could have called them john bill and fred

To the compiler it is all the same, it is just a 'nickname' or alias for 1byte of ram that we store some particular data in.

I also mentioned before that you didn't need this:

Code:
movlw d'255'
movwf delay_1
movwf delay_2


in your code at all. Have a go at deleting it and see if the LED behaves differently at all. Can you have a guess at why it performs the same?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 06, 2010 8:20 am 
Offline
decided to stick around...
decided to stick around...

Joined: Tue Jun 08, 2010 12:14 pm
Posts: 32
I thought maybe it had something to do with the decfsz command :?

can you please explain why it still looks the same?

Also, how come when i added this code, it doesnt look like led stays on for any longer than it did before?

Code:
movlw d'255'
   movwf delay_3


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 08, 2010 7:36 am 
Offline
Site Admin
Site Admin
User avatar

Joined: Fri Mar 26, 2010 10:30 pm
Posts: 1860
tgraz34 wrote:
I thought maybe it had something to do with the decfsz command :?

can you please explain why it still looks the same?

Also, how come when i added this code, it doesnt look like led stays on for any longer than it did before?

Code:
movlw d'255'
   movwf delay_3


sorry for the late reply, have been away :)

The reason that it performs pretty much exactly the same is because the decfsz command will FIRST decrement from the variable and THEN check if it zero. So think about this:

If we have this as our delay

movlw d'255'
movwf delay_0
movwf delay_1
delay_loop
decfsz delay_0, f
goto delay_loop
decfsz delay_1, f
goto delay_loop
return

you can see that we load the two variables with 255, then we decrement delay_0 by one - so it goes to 254. It is not zero yet so we goto the delay_loop label.

so when delay_0 eventually gets to 1 and we then use the decfsz command, we decrement by one and then check if it is zero. since it will now be zero we don't loop but we goto the command just underneath which is decfsz delay_1 which means it now goes to 254. since delay_1 is not zero, we goto the delay_loop label again. Now just underneath the delay_loop label is again decfsz delay_0, f

remember what what is delay_0? it was zero! because we had already counted down from 255 to zero with this one. but now that we decrement and then check if it zero we actually end up at 255 again (because taking one away from zero will loop you back around to 255)

so in actual fact, we are counting down from 255, 255 times.

as for this code not actually doing anything:

Code:
movlw d'255'
   movwf delay_3


it is because you aren't also adding this to your loop routine:

Code:
decfsz delay_3, f
goto delay_loop


you are loading it up, but then not using it.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 09, 2010 4:57 am 
Offline
decided to stick around...
decided to stick around...

Joined: Tue Jun 08, 2010 12:14 pm
Posts: 32
Im a little confused. Will this code count down from 255, 255 times, and then do it again 255 times?

Code:
ledon                 
   movlw d'255'   
   movwf delay_1           
   movwf delay_2   
   movlw d'255'
   movwf delay_3
   
   
delay_loopon           
   decfsz delay_1, f     
      goto delay_loopon         
   decfsz delay_2, f         
      goto delay_loopon     
   decfsz delay_3, f
      goto delay_loopon
   return


Sorry if i sound like a broken record haha.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 11, 2010 12:21 pm 
Offline
Site Admin
Site Admin
User avatar

Joined: Fri Mar 26, 2010 10:30 pm
Posts: 1860
Sorry buddy, I forgot to reply.

Yes it will count down from 255, then do that again 255 times and do all of that another 255 times.

You will be waiting awhile for it to finish :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC + 10 hours


Who is online

Users browsing this forum: No registered users and 0 guests


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 post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Theme made by Keenen Wheeler