Continue to Site

# Input port checking according to different pins in PIC 16F877A with C

Status
Not open for further replies.

#### traxex

##### Newbie level 4
Hi. I have an issue about a program for my project. I am taking two inputs from RD0 and RD1. When RD0 is 1, the output at PORTB should be 1. When RD0 goes to 0, controller should check for RD1. When RD1 is 0 while RD0 is also 0, the output at PORTB should be 0. When RD1 goes to 1 while RD0 is 0, the output at PORTB should be 1. The program executes nicely at the first, but i cannot go out of the second while loop. When RD0 is 0 and RD1 is 1, the output is 1, and RD0 goes to 1 always when, so the program should look at the first loop, but it doesn't. I want to code it so that it always looks RD0 and looks at RD1 only when RD0 is 0, i.e. I want to enable RD1 only when RD0 is 0, like the priorities of them are different(RD0 is higher, RD1 is lower). How should i code it?

Code:
	while(1)
{
while(RD0==1)
{
PORTB = 0XFF;
}
while(RD0==0)
{
if(RD1==1)
{
PORTB = 0XFF;
}
if(RD1==0)
{
PORTB = 0X00;
}
}

}

#### srjth.m1

##### Full Member level 3
change the first while inside infinite loop with "if" second with "else if", 4th if to "else",then your program will work i think

- - - Updated - - -

Code:
while(1)
{
if(RD0==1)
{
PORTB = 0XFF;
}
else if(RD0==0)
{
if(RD1==1)
{
PORTB = 0XFF;
}
else
{

PORTB = 0X00;
}
}

}

#### bigdogguru

Hi. I have an issue about a program for my project. I am taking two inputs from RD0 and RD1. When RD0 is 1, the output at PORTB should be 1. When RD0 goes to 0, controller should check for RD1. When RD1 is 0 while RD0 is also 0, the output at PORTB should be 0. When RD1 goes to 1 while RD0 is 0, the output at PORTB should be 1. The program executes nicely at the first, but i cannot go out of the second while loop. When RD0 is 0 and RD1 is 1, the output is 1, and RD0 goes to 1 always when, so the program should look at the first loop, but it doesn't. I want to code it so that it always looks RD0 and looks at RD1 only when RD0 is 0, i.e. I want to enable RD1 only when RD0 is 0, like the priorities of them are different(RD0 is higher, RD1 is lower). How should i code it?

What you describe is commonly referred to as a State Machine.

There are several ways of coding a State Machine, much of it is based on personal preference.

The first step is usually to create an accurate state diagram, representing all possible states and flow, which will be the basis of your code.

Finite State Machine in C

Wiki - Finite-state machine

Wiki - Event-driven finite-state machine

If you switched the inputs to PORTB and the outputs to PORTD, you could also use the Interrupt-On-Change feature of the PIC16F to trigger an Interrupt Service Routine to handle any changes which occur on PORTB inputs.

Effectively changing the state of the machine, when the proper change sequence occurs.

BigDog

traxex

Points: 2