Help Initializing PORTA PIC16f887

Discussion in 'Embedded Systems and Microcontrollers' started by The_Fleertz, May 3, 2012.

  1. The_Fleertz

    Thread Starter New Member

    Apr 23, 2012
    8
    0
    Hey I've been trying to figure this out for hours now and i can't get it. I try to initialize porta as inputs but every time i use those inputs I get multiple bits lighting up. I've tried everything I've found to fix it but nothing works.
    Code ( (Unknown Language)):
    1.  
    2.     ANSEL=0x00;                 //This and next line initialize   PORTB to digital
    3.     ANSELH=0x00;                    //
    4. //  CM1CON0=0x07;
    5. //  OSCCON=0b11110101;
    6.     ADCON0=0x00;
    7.     ADCON1=0x06;
     
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,388
    1,605
    Setting ANSEL to zero makes the pin work as digital (shuts off the analog) so that looks good.

    I don't see you setting TRISA to inputs. All the PICs I've seen (this one too) used 1 for 1nput, and 0 for 0utput.

    MPLAB's simulator can work well to test the code for inputs. Some things MPLAB doesn't do so well but this one is solid.
     
  3. The_Fleertz

    Thread Starter New Member

    Apr 23, 2012
    8
    0
    Yeah I just posted that part of the initialize statements. I do the TRIS later. What I have is switches on PORTA and when I flip one PORTB should light up the corresponding led. For some reason it will light up several at a time.

    Code ( (Unknown Language)):
    1.  
    2.     ANSEL=0x00;                 //This and next line initialize PORTB and PORTA to digital
    3.     ANSELH=0x00;                    //
    4.    
    5.     PORTB=0;
    6.     PORTA=0;
    7.     TRISB=0b00000000;           //PORTB all outputs for lights
    8.     TRISA=0b11111111;           //PORTA all inputs
    9.  
    10.  
    11.     while(1) {
    12.             PORTB=PORTA;
    13.         }
    14.  
     
  4. MrChips

    Moderator

    Oct 2, 2009
    12,442
    3,361
    How are the switches connected to PORTA?
    You may need pull-up resistors on all eight lines of PORTA.
     
  5. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,388
    1,605
    Again, the MPLAB simulator will demonstrate if the code is correct. That could lead to the conclusion it must be a hardware issue.

    I would suggest you post the entire program (after removing all those excessive tabs so it doesn't disappear off the right side) and your schematic too.
     
  6. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    @The_Fleertz
    I would strongly advice you post all your code then you ask for help here. That way you will get help quicker. Just a tip
     
Loading...