# Help Initializing PORTA PIC16f887

#### The_Fleertz

Joined Apr 23, 2012
8
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.
Rich (BB code):
	ANSEL=0x00;					//This and next line initialize   PORTB to digital
ANSELH=0x00;					//
//	CM1CON0=0x07;
//	OSCCON=0b11110101;
ADCON1=0x06;

#### ErnieM

Joined Apr 24, 2011
8,184
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.

#### The_Fleertz

Joined Apr 23, 2012
8
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.

Rich (BB code):
	ANSEL=0x00;					//This and next line initialize PORTB and PORTA to digital
ANSELH=0x00;					//

PORTB=0;
PORTA=0;
TRISB=0b00000000;			//PORTB all outputs for lights
TRISA=0b11111111;			//PORTA all inputs

while(1) {
PORTB=PORTA;
}

#### MrChips

Joined Oct 2, 2009
24,452
How are the switches connected to PORTA?
You may need pull-up resistors on all eight lines of PORTA.

#### ErnieM

Joined Apr 24, 2011
8,184
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.

#### t06afre

Joined May 11, 2009
5,934
@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