how to read the input ports

For C and ASSEMBLY users to post questions and code snippets for programming in C and ASSEMBLY. And for any other C or ASM course related questions.

Moderators: Benj, Mods

Post Reply
saravana_3
Posts: 61
Joined: Thu Dec 20, 2007 4:23 pm
Location: singapore
Contact:

how to read the input ports

Post by saravana_3 »

Hi all
I am using the 16f877a, I set the port A as the input and PortB as the output,
in "c" 'how to scan when any of the portA buttons are pressed
please give some Idea, I want to control the PortB by the portA
thanks
Saran
saran

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: how to read the input ports

Post by Benj »

hello Saran

Essentially you can just read porta into a variable by doing the following.

Code: Select all

char input;
input = porta;
Then you can create a loop like this to detect when porta changes its value.

Code: Select all

char oldinput;
oldinput = input
while (oldinput == input)
{
    input = porta;
}
Testing a single bit or switch can can be done as follows.

Code: Select all

if (test_bit(porta, 0))
{
//RA0 Set
}
else
{
//RA0 Clear
}

Post Reply