Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

weird PORTA

Status
Not open for further replies.

gastonanthony

New Member
HI, the code that I wrote is to make the PORTB count from 0 to 255, and after that, to make the PORTA count from 0 to 31, and back to incrementing PORTB 255 and so on. but it didn't work that way while i'm simulating it on mplab, after incrementing portb to 255, and PORTA TO 31, it continued incrementing porta from 0-31-0-31... and never escaped the loop. Can someone please try to simulate it on their mplab and see for them selves what's happening, and maybe give a possible explanation :D
Code:
PROCESSOR 16F84a
#include "P16f84a.inc"

start
		
		bsf	STATUS,5
		movlw	00h
		movwf	TRISA
		movwf	TRISB
		bcf	STATUS,5
		movlw	00h
		movwf	PORTA
		movwf	PORTB
count	
		incfsz	PORTB
		goto	count
countt	
		incfsz	PORTA
		goto	countt
		goto	start
		end
 

Nigel Goodwin

Super Moderator
Most Helpful Member
No need to - the answer is simple!.

RA4 is an open collector output, so it can't go high unless you have a pullup resistor - so as soon as your count reaches RA4 it's reset to zero (as RA4 can't go high).
 

Nigel Goodwin

Super Moderator
Most Helpful Member
gastonanthony said:
if it resets to zero, why doesn't the program counter skip the goto after the incfsz(skip if 0)?

Because it's not going to zero on the WRITE, but on the READ instead, so it READS zero back from the port, increments it, then WRITES it back - THEN it checks for zero (which it never will be, as it can never reach 255).
 

Nigel Goodwin

Super Moderator
Most Helpful Member
gastonanthony said:
ok, now I understand it clearly:D thanks for the quick reply and nice explanation :D

No problem! - one point though, it's not considered good practice to directly increment the ports (I think you perhaps now know why?), you should increment a GPR (General Purpose Register) and then move it to the port. If you'd done it that way, your program would have worked.
 
Status
Not open for further replies.

EE World Online Articles

Loading
Top