Jump to content
Sign in to follow this  

Pic16f877a + Using Port A As Input (help Please)

Recommended Posts


I need some advice on how to set port A of PIC16F877A as input correctly..

Below is a code that when input RA0 is HIGH, LED on RD0 will turn on (at least that is what I plan for it to do). When RA1 is high, LED on RD1 will turn on etc..

The input I have is from RA0 to RA4..


But the code doesn't seem to work.. It doesn't recognise the input at port A..

I have already set the trisa to input but still no result..

Can somebody please advise.. thanks..


#include <system.h>

//Target PIC16F877 configuration word

//Set clock frequency
#pragma CLOCK_FREQ	4000000

// Variable declaration
int Code;	// the Code that is received (eg 10000 is mode)
int Direction = 0;
int AutoMode = 0;
int AutoCount = 0;
int Loop = 0;

void main( void )
//Configure port A
trisa = 0xFF;
//Configure port B
trisb = 0x00;
//Configure port C
trisc = 0xFF;
//Configure port D
trisd = 0x00;
//Configure port E
trise = 0x00;

//Initialize port A
porta = 0x00;
//Initialize port B
portb = 0x00;
//Initialize port C
portc = 0x00;
//Initialize port D
portd = 0x00;
//Initialize port E
porte = 0x00;

//Endless loop
while( 1 )
	Code = porta;
	if (Code == 0b00001) goto Normal_1;
	else if (Code == 0b00010) goto Speed_1;	
	else if (Code == 0b00100) goto Automatic_1;		
	else if (Code == 0b01000) goto Sensor_1;	
	else if (Code == 0b01000) goto Mode_1;

portd = 0x00;
set_bit(portd, 0);
goto Main;

portd = 0x00;
set_bit(portd, 1);
goto Main;

portd = 0x00;
set_bit(portd, 2);
goto Main;

portd = 0x00;
set_bit(portd, 3);
goto Main;

portd = 0x00;
set_bit(portd, 4);
goto Main;


Share this post

Link to post
Share on other sites

Port A's default mode is as an analogue input, you have to switch off the adc function using adcon1


See page 43 of the PIC16F87xA data sheet

Share this post

Link to post
Share on other sites

Your content will need to be approved by a moderator

You are commenting as a guest. If you have an account, please sign in.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoticons maximum are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this