# 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
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
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).

#### gastonanthony

##### New Member
if it resets to zero, why doesn't the program counter skip the goto after the incfsz(skip if 0)?

#### Nigel Goodwin

##### Super Moderator
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).

#### gastonanthony

##### New Member
ok, now I understand it clearly thanks for the quick reply and nice explanation

#### Nigel Goodwin

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

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.

#### gastonanthony

##### New Member
ok, again, thank you

Status
Not open for further replies.

Replies
1
Views
1K
Replies
14
Views
1K
Replies
2
Views
1K
Replies
1
Views
2K
Replies
2
Views
1K