Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

pic programming help needed with nested loops

Status
Not open for further replies.

Ducados

Junior Member level 1
Joined
Aug 14, 2009
Messages
16
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Spain
Activity points
1,434
pickit 2/ 16f690/ mplab ide

I'm having trouble with loops now that I'm trying to write my own code, I want to use decfsz because it doesn't reset any flags, all they examples I see use f as destination but I can only get the loops to function with w as the destination or a mixture of f and w in nested loops.

here's an example of the code, it never leaves loop2 unless I replace the f with w.

mainloop:

movlw b'000001'
movwf 07h
; timing delay >

movlw d'3'
loop2 movwf v1
movlw b'000010'
movwf 07h
; timing delay>
decfsz v1,f
goto loop2
goto mainloop
 

Hi,

Think this might help show how a little delay is done.

Remember that at say 4mhz osc each instruction takes about 1millionth of a second, fosc/4 , so your loop only takes a fraction of a second.
To create a loop for about 1/2 second that you can actually see on a flashing led you need a triple nested loop.
Use a little code calculator like this one


Code:
mainloop:

	movlw b'000001'					; load port c with 000001
	movwf 07h
		; timing delay >

	movlw d'3'						; load W with dec 3

loop2 movwf v1						; move whatever is in W to v1
	movlw b'000010'
	movwf 07h						; load portc with 000010   - this happens after just the odd millisecond
; timing delay>
	decfsz v1,f						; decf v1
	goto loop2						; goto 2 if not zero  - whatever is in W with then go into loop2
	goto mainloop

end



;suggest your code is more list time

		bcf STATUS ,RP0			; ensure bank 0 selected
		bcf	STATUS ,RP1

main	 movlw b'00000001'		; load all 8 bits
  	 	 movwf PORTC				; Turn on bit 0 of PortC   - use the registers names  - its easier than trying to use numbers

		movlw d'255'			; load v1 with 255 to create a little delay
		movwf v1

loop2  decfsz v1,f			; dec v1
	   goto	loop2			; loop 255 times
	   goto main
 

Ducados said:
it never leaves loop2 unless I replace the f with w.
This is because you reset the loop counter with " movwf v1" each time you enter the loop. Move the loop2 label one line down, such that "movwf v1" goes outside of the loop. Also, there is no need to repeat "movlw b'000010' & movwf 07h" lines in the loop. It makes more sense if they are placed outside of the loop.

For this piece of code, your logic of using W as the loop counter also works. But in a more complicated loop, you probably need W for other things, so avoid using W as the loop counter.
 

It was the v1 and v2 variables that were incorrectly declared. now the code works fine, strange how it compiled without warnings. Thanks anyway because the code examples were excellent.
 

It compiled because there is nothing wrong with the instructions or syntax. An assembler sees it as valid code, it's the way the instructions are put together that stopped it working the way you wanted it to.

Brian.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top