Jump to content


Pic16f877a + Using Port A As Input (help Please)

2 replies to this topic

#1 MrNobody



  • Members
  • 6 posts

Posted 03 July 2007 - 02:08 AM

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 &#40;eg 10000 is mode&#41;
int Direction = 0;
int AutoMode = 0;
int AutoCount = 0;
int Loop = 0;

void main&#40; void &#41;
	//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&#40; 1 &#41;
		Code = porta;
		if &#40;Code == 0b00001&#41; goto Normal_1;
		else if &#40;Code == 0b00010&#41; goto Speed_1;	
		else if &#40;Code == 0b00100&#41; goto Automatic_1;		
		else if &#40;Code == 0b01000&#41; goto Sensor_1;	
		else if &#40;Code == 0b01000&#41; goto Mode_1;
	portd = 0x00;
	set_bit&#40;portd, 0&#41;;
	goto Main;
	portd = 0x00;
	set_bit&#40;portd, 1&#41;;
	goto Main;
	portd = 0x00;
	set_bit&#40;portd, 2&#41;;
	goto Main;
	portd = 0x00;
	set_bit&#40;portd, 3&#41;;
	goto Main;
	portd = 0x00;
	set_bit&#40;portd, 4&#41;;
	goto Main;

#2 Picxie


    Super Enthusiast

  • Moderator
  • PipPipPip
  • 284 posts
  • Location:Townsville

Posted 03 July 2007 - 08:23 AM

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

#3 MrNobody



  • Members
  • 6 posts

Posted 03 July 2007 - 12:47 PM

thank u thank u thank u thank u.......

Reply to this topic


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users