Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

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

Status
Not open for further replies.

traxex

Newbie level 4
Newbie level 4
Joined
Jul 8, 2011
Messages
7
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,326
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;
			}
		}

	}
 

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;
			}
		}

	}
 

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
 
  • Like
Reactions: traxex

    traxex

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top