Reading PORTA pin when it is an output

Discussion in 'Embedded Systems and Microcontrollers' started by 999alfred, Dec 1, 2010.

  1. 999alfred

    Thread Starter New Member

    Sep 16, 2010
    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
    bcf PORTA, 0
    bcf led_state, 0

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

  2. 999alfred

    Thread Starter New Member

    Sep 16, 2010
    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.

  3. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    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!
  4. Markd77

    Senior Member

    Sep 7, 2009