Need Help! PIC16F877A PortA Initialization

Discussion in 'Embedded Systems and Microcontrollers' started by gai_man_de, Jul 5, 2010.

  1. gai_man_de

    Thread Starter New Member

    Jul 4, 2010
    1
    0
    I use PIC 16f877A and choose PORTA as input of my switchs and have pull-up resistor 10K. So the default state of pins in PORTA is high or logic '1'. But when I use MPlab to write code ASM for my MCU, after I set trisc for PORTA as input, the state of PORTA is '00000000' that cause my switchs in PORTA are always in LOW that active state and my ciruit run automaticly as i press all that switchs.
    I tried to set trisc PORTA as ouput first, initialize its state as '11111111' and set trisc PORTA as input. But value of PORTA is only changed to '01000000', that still cause wrong when I conplie and simulate in Proteus.
    I tried to put my switchs to PORTC and try âgain. It has no problem as in PORTA, state of PORTC after I set trisc for PORTC as input is '11111111' that make my ciruit run exactly.
    Can anyone explain and help me to solve that problem? Thanks.
    My code is:

    BANKSEL TRISA
    CLRF TRISA
    BANKSEL PORTA
    MOVLW 0FF
    MOVWF PORTA
    BANSEL TRISA
    MOVLW 0FF
    MOVWF TRISA

    value of PORTA and TRISA after my code is:
    TRISA : 11111111
    PORTA : 01000000
     
  2. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Have you downloaded the data sheet. You will find a lot useful examples here. Here is how to setup PORTA correct for your chip. It looks like you forgot to set the ADCON1 register correct. If you write the value 0x6 to ADCON1 register your program should work. From the manual
    Code ( (Unknown Language)):
    1.  
    2. [LEFT]Banksel PORTA
    3. CLRF PORTA ; Initialize PORTA by
    4. ; clearing output
    5. ; data latches
    6. Banksel ADCON1; Select Bank 1
    7. MOVLW 0x06 ; Configure all pins
    8. MOVWF ADCON1 ; as digital inputs
    9. MOVLW 0xfF ; Value used to
    10. ; initialize data
    11. ; direction
    12. MOVWF TRISA ; all pins as inputs
    13. ; TRISA<7:6>are always
    14. ; read as '0'.[/LEFT]
    15.  
     
    Last edited: Jul 5, 2010
Loading...