Jump to content

Pic16f877a + Using Port A As Input (help Please)


Recommended Posts

Hi..

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
#pragma DATA _CONFIG, _PWRTE_OFF & _BODEN_OFF & _WDT_OFF & _LVP_ON & _CPD_OFF & _DEBUG_OFF & _HS_OSC & _CP_OFF

//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 )
{
	Main:
	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;
}

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

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

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

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

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

}

Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji 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.

Loading...
×
×
  • Create New...