Reading PORTA pin when it is an output

Thread Starter

999alfred

Joined Sep 16, 2010
4
I am having a hard time understanding a problem with PORTA on a PIC16F874.
What I am wanting do is flash an LED on RA0. I order to tell if it is on or of, so I know to bsf or bcf RA0, I do a btfsc PORTA, 0 , it is always true, RA0 clear. However, if I leave the code the same EXCEPT use PORTB, everything works and LED flashes. Here is the code:

bsf STATUS,RP0
movlw 0x00
movwf TRISA
bcf STATUS, RP0

;this code is an interrupt service routine, toggles the LED every time it is enterd

btfsc PORTA, 0
goto LEDoff
bsf PORTA, 0
bsf led_state, 0
goto leave
LEDoff:
bcf PORTA, 0
bcf led_state, 0
leave:

Like I said, if you change port to PORTB and TRISB, it works fine

tj
 

Thread Starter

999alfred

Joined Sep 16, 2010
4
Problem solved.
I thought that you only had to cahnge the ADCON1 register to all digital for INPUT, not output, I was mistaken. writing 0x86 to ADCON1 fixed the problem.

tj
 

thatoneguy

Joined Feb 19, 2009
6,359
Reading ports while set to output can give unreliable results.

The internal structure of the outputs is different for the ports with analog abilities, compared to the digital I/O only ports. The Read/Modify/Write gives different results.

A bit more info

There is also a Microchip App Note on this, I don't recall which one off the top of my head though, they have thousands of extremely helpful notes!
 
Top