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.

PIC C language having problem with loop

Status
Not open for further replies.

stuckkk

Newbie level 6
Joined
Dec 18, 2012
Messages
13
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,383
Hi,
I am having problem simulating the following program using PIC simulator IDE. Appreciate if someone could point out the errors in this prog.
Below is the coding

Code:
#include<htc.h>
unsigned char inbyte=0, input=0;
unsigned int counter=0;

void config ();
void function ();
void function_1 ();
void delay (int k);

void main()
{
	while(1)
	{
	config ();
	
	inbyte=PORTA;
	if (inbyte = inbyte & 0x80)				//read PORTA bit7
	{
		counter=!counter;				
		
		while (counter==1)
		{
			for (int a=0; a<10; a++)		//the program get stuck in this loop
			{
			PORTB= PORTB | 0x40;
			delay (1000);
			PORTB= PORTB & 0xBF;
			delay (1000);	
			}
		
			input=PORTA;				//read PORTA again
			switch(input)
			{
				case 0x01:
				function ();
				function_1 ();
				PORTB= PORTB | 0x01;
				break;
				
				case 0x02:
				function ();
				function_1 ();
				PORTB= PORTB | 0x02;
				break;
				
				case 0x04:
				function ();
				function_1 ();
				PORTB= PORTB | 0x04;
				break;
				
				case 0x08:
				function ();
				function_1 ();
				PORTB= PORTB | 0x08;
				break;
			
				case 0x10:
				function ();
				function_1 ();
				PORTB= PORTB | 0x10;
				break;
			}			
		}
			
		while (counter==0)				//if PORTA bit7 is pressed again, off all LEDs
		{
			PORTB=0x00;
		}
		}
}
}

void config ()
{
	CMCON=0x07;
	TRISA=0xFF;
	TRISB=0x00;
}

void delay (int k) 
{
	for (int i=0; i<k; i++);
}

The program get stuck in the first 'for' loop. I have tried several ways but still in my simulator(PIC simulator IDE) it continues blinking. I was thinking it should blink 10 times and stop according to the loop, but it does not come out of the loop.

And, after that how can I make switch-case part to be looped until the PORTA bit 7 is pressed for the second time which switches off all LEDs. I mean after each case is executed back to the start of the switch, so another case can be selected.

I havent written the functions I am calling for in this program as I suppose they are irrelevant to the errors I am having.

Thanks.
 

Your IF statement uses = instead of ==. Also, brackets would be a good idea.

Keith
 

I have checked with changing the IF statement to as you said but still the problem persists
 

IF for you in should be = = you into a =
 

Nothing actually seems to increment COUNTER so you are stuck in the while loop. The only part that counts to 10 is the FOR loop. How do you know it gets stuck there? You don't cover every possible value of PORTA in your SWITCH statement and have no DEFAULT. Put breakpoints in and watch the "a" counter value to see what is happening. Remove the delays and single step to check your logic while watching variables.

Keith
 
- When I simulate the program using PIC simulator IDE the LED inside the 'for loop' keeps on blinking

- Counter is used as a 2 state integer so as when the main button (PORTA bit7) is pressed the second time all the LED's switches off

- The switch-case is not needed for all the values of PORTA. I can deactivate the unused pins of PORTA using the TRISA register.
 

That still doesn't explain how you know it is stuck in the "for loop" rather than just going round the "while" loop. Add a default to the switch statement and "trace" while watching variables rather than simply running the simulation.

Keith
 

I still cannot find the problem with the program.. appreciate if some one could help
 

At the moment your code (after fixing the IF statement - "if (inbyte == (inbyte & 0x80))" and disabling the watchdog timer) will flash 10 times then check PORTA then flash 10 times etc. There is nothing in the code to stop it flashing.

If you want it to wait for something then you need to add something like while(PORTA == 0){ ... } after you have flashed 10 times.

Also, I would recommend a #pragma statement to set the config bits.

counter=!counter; is not a great idea, depending on what you are trying to achieve.

Also, as you have a "while (counter==1)" statement you will never get out of it if you never change the counter within while loop (or with an interrupt).

You have config() within a while(1) loop whereas configuration is usually only done once.

I am not sure what you are expecting "if (inbyte == (inbyte & 0x80))" to do but it will be true if the MSB is high and no other bits OR if no bits are high. I would not have thought that was what you wanted.

Keith
 
Thanks, Now I can get out of the for loop. But if I write a=0, it gets stuck in the loop.

In the switch-case I want to put function_1 in an infinite loop. But I want the switch case to be in a loop too.So, if function1 has been called it continues in its loop and come back to switch case loop
 

Code:
                        do{
			input=PORTA;				//read PORTA again
			switch(input)
			{
				case 0x01:
				function ();
				function_1 ();
				PORTB= PORTB | 0x01;
				break;
				
				case 0x02:
				function ();
				function_1 ();
				PORTB= PORTB | 0x02;
				break;
				
				case 0x04:
				function ();
				function_1 ();
				PORTB= PORTB | 0x04;
				break;
				
				case 0x08:
				function ();
				function_1 ();
				PORTB= PORTB | 0x08;
				break;
			
				case 0x10:
				function ();
				function_1 ();
				PORTB= PORTB | 0x10;
				break;
			}	
                        }while(inbyte==0x80);

When case 1 has been selected and it goes back to the start the program is not able to select another case unless, the case 1 has been switched off.

And, how can I make function_1 to be in an infinite loop and make it come back to main function
 

Post your full code. Your earlier code has a problem like this
Code:
while (counter==1)
		{
			for (int a=0; a<10; a++)		//the program get stuck in this loop
			{
			PORTB= PORTB | 0x40;
			delay (1000);
			PORTB= PORTB & 0xBF;
			delay (1000);	
			}
		
			input=PORTA;				//read PORTA again
			switch(input)
			{
				case 0x01:
				function ();
				function_1 ();
				PORTB= PORTB | 0x01;
				break;
				
				case 0x02:
				function ();
				function_1 ();
				PORTB= PORTB | 0x02;
				break;
				
				case 0x04:
				function ();
				function_1 ();
				PORTB= PORTB | 0x04;
				break;
				
				case 0x08:
				function ();
				function_1 ();
				PORTB= PORTB | 0x08;
				break;
			
				case 0x10:
				function ();
				function_1 ();
				PORTB= PORTB | 0x10;
				break;
			}			
		}
When counter becomes 1, while(counter==1) executes infinitely. So your for loop also gets executed infinitely and hence your led blinks infinitely. In the switch case statement try to clear input before breaking the case statement like
Code:
                                case 0x01:
				function ();
				function_1 ();
				PORTB= PORTB | 0x01;
                                input = 0x00;
				break;

Try this code
Code:
void main {
	do{
		input=PORTA;
                function_1 ();				//read PORTA again
		switch(input)
		{
			case 0x01:
			function ();
			PORTB= PORTB | 0x01;
			break;
				
			case 0x02:
			function ();
			PORTB= PORTB | 0x02;
			break;
				
			case 0x04:
			function ();
			PORTB= PORTB | 0x04;
			break;
				
			case 0x08:
			function ();
			PORTB= PORTB | 0x08;
			break;
			
			case 0x10:
			function ();
			PORTB= PORTB | 0x10;
			break;
		}	
        }while(inbyte==0x80);
}
function_1() will be executed infinitely until the value of inbyte is 0x80. If inbyte is not 0x80 then the execution comes out of the do...while(inbyte==0x80) loop to the main function. Post the full code, we have to see whether function_1() can be used before function() or not.
 
Last edited:

Thanks a lot.. that cleared things pretty much

Here is the full code

Code:
#include<htc.h>
unsigned char inbyte=0, input=0;
int counter=0;

void config ();
void function_1 ();
void function ();
void delay (int k);

void main()
{
	config ();
	
	inbyte=PORTA;
	if (inbyte==0x80 && counter==0)
	{
		for (int a; a<30; a++)
		{
			delay(1000);		
			PORTB = PORTB | 0x40;
			delay(1000);
			PORTB = PORTB & 0xBF;
		}	

	do
	{
		input=PORTA;
		switch(input)
		{
			case 0x81:
			{
				PORTB= PORTB | 0x01;
				function ();
                                function_1 ();
			}
			break;
					
			case 0x82:
			{
				PORTB= PORTB | 0x02;
				function ();
                                function_1 ();
			}
			break;
					
			case 0x84:
			{
				PORTB= PORTB | 0x04;
				function ();
                                function_1 ();
			}
			break;
					
			case 0x88:
			{
				PORTB= PORTB | 0x08;
				function ();
                                function_1 ();
			}
			break;
				
			case 0x90:
			{
				PORTB= PORTB | 0x10;
				function ();
                                function_1 ();
			}
			break;				
		}
		input=0;			
	}while(inbyte==0x80);
	}
	counter++;
}

void config ()
{
	CMCON=0x07;
	TRISA=0xFF;
	TRISB=0x00;
}

void delay (int k) 
{
	for (int i=0; i<k; i++);
}
		
void function_1 ()
{
	while(1)
	{
		delay(1000);		
		PORTB = PORTB | 0x40;
		delay(1000);
		PORTB = PORTB & 0xBF;
	}
        }	
}
	
}
 
Last edited:

You should not do like this
Code:
 case 0x81:
			{
				PORTB= PORTB | 0x01;
				function ();
                                function_1 ();
			}
			break;
break; should be inside the case statement. According to your code break will not be executed.

According to the below code
Code:
 void function_1 ()
{
	while(1)
	{
		delay(1000);		
		PORTB = PORTB | 0x40;
		delay(1000);
		PORTB = PORTB & 0xBF;
	}
        }	
}
when function_1() is called it enters the infinite while(1) loop. So, it never comes out of the function. tell exactly how the program flow should be.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top