Tutorial 1 help please

Post here to discuss the PIC microcontroller tutorials.

Moderators: Chuckt, Garth, bitfogav

Post Reply [phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable
tgraz34
decided to stick around...
decided to stick around...
Posts: 32
Joined: Tue Jun 08, 2010 12:14 pm
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Tutorial 1 help please

Post by tgraz34 » Tue Aug 03, 2010 7:36 am

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: Select all

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	

User avatar
brad
Site Admin
Site Admin
Posts: 2578
Joined: Fri Mar 26, 2010 10:30 pm
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Post by brad » Tue Aug 03, 2010 6:41 pm

I guess you could look at making the code a little more straight forward / more efficient. Something like this perhaps:

Code: Select all

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
:)

tgraz34
decided to stick around...
decided to stick around...
Posts: 32
Joined: Tue Jun 08, 2010 12:14 pm
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Post by tgraz34 » Fri Aug 06, 2010 4:24 am

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

User avatar
brad
Site Admin
Site Admin
Posts: 2578
Joined: Fri Mar 26, 2010 10:30 pm
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Post by brad » Fri Aug 06, 2010 6:11 am

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: Select all

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?

tgraz34
decided to stick around...
decided to stick around...
Posts: 32
Joined: Tue Jun 08, 2010 12:14 pm
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Post by tgraz34 » Fri Aug 06, 2010 8:20 am

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: Select all

movlw d'255'
   movwf delay_3 

User avatar
brad
Site Admin
Site Admin
Posts: 2578
Joined: Fri Mar 26, 2010 10:30 pm
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Post by brad » Sun Aug 08, 2010 7:36 am

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: Select all

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: Select all

movlw d'255'
   movwf delay_3 
it is because you aren't also adding this to your loop routine:

Code: Select all

decfsz delay_3, f
goto delay_loop
you are loading it up, but then not using it.

tgraz34
decided to stick around...
decided to stick around...
Posts: 32
Joined: Tue Jun 08, 2010 12:14 pm
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Post by tgraz34 » Mon Aug 09, 2010 4:57 am

Im a little confused. Will this code count down from 255, 255 times, and then do it again 255 times?

Code: Select all

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.

User avatar
brad
Site Admin
Site Admin
Posts: 2578
Joined: Fri Mar 26, 2010 10:30 pm
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Post by brad » Wed Aug 11, 2010 12:21 pm

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 :)

Post Reply
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Who is online

Users browsing this forum: No registered users and 8 guests